SDL keypress problem

Because the offical SDL forum have some approval before you can write on their forum so I'll try here.

Is there any specific code with SDL so you can cancel a keypress? Cause a normal SDL keystate look like this

1
2
3
4
5
6
7
8
9
10
if( event.type == SDL_KEYDOWN ) {
         switch( event.key.keysym.sym )
        {
            case SDLK_LEFT: break;
            case SDLK_RIGHT: break;
            case SDLK_SPACE: break;
            default: break;
        }
}
etc...


but if I for example hold down SPACE the process will not cancel till I realese it. I just want to know if there is code by default in SDL that can cancel the keypress.
What do you mean by "cancel"? Do you mean you want to be able to stop the event that happens with a line of code? That can be done with a boolean.
1
2
3
4
5
6
7
8
9
10
bool DOLEFT = true;
if( event.type == SDL_KEYDOWN ) {
         switch( event.key.keysym.sym )
        {
            case SDLK_LEFT:
               while (DOLEFT) { /* write your code to do here */ }
               break;
            default: break;
        }
}

Now the event-handler won't do your code (when left key is pressed) unless the boolean DOLEFT is set to true. Make this boolean global if you want to be able to enable/disable the keypress throughout your application.
Not exactly, I'm trying to make a jump button and it looks like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
void PLAYER::handle_input()
{
   //If a key was pressed
    if( event.type == SDL_KEYDOWN )
    {

        switch( event.key.keysym.sym )
        {
            case SDLK_LEFT: xVel -= 1; break;
            case SDLK_RIGHT: xVel += 1; break;
            case SDLK_SPACE: JumpTimer.start(); jump = true; inAir = true; yVel -= 2; break;
            default: break;

        }

    }
    //If a key was released
    if( event.type == SDL_KEYUP )
    {
      switch( event.key.keysym.sym )
        {
            case SDLK_LEFT: xVel += 1; break;
            case SDLK_RIGHT: xVel -= 1; break;
            case SDLK_SPACE: yVel += 2; jump = false; break;

            default: break;
        }
    }


}


If i hold down SPACE the event wont be canceled until i release it. And i want a timer that stops the keypress because yVel; needs to be with case SDLK_SPACE: .

The keypress will only "update" if i press SPACE again so i can't put the timer into the PLAYER::handle_input().
Last edited on
The problem is that your logic is tied too tightly to the input method.
This would be much better:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
bool space_is_held=0;
//loop{
	bool space_changed_state=0;
	
	//handle SDL_KEYDOWN
		case SDLK_SPACE:
			space_changed_state=1;
			space_is_held=1;
			break;
	//handle SDL_KEYUP
		case SDLK_SPACE:
			space_changed_state=1;
			space_is_held=0;
			break;
	
	if (space_changed_state){
		if (space_is_held){
			//...
		}else{
			//...
		}
	}
}
Then you could more easily ignore things you don't care about.
Topic archived. No new replies allowed.