SDL_GetKeyboardState(NULL);

this is the example they gave at sdl 1.2

1
2
3
4
5
Uint8 *keystate = SDL_GetKeyState(NULL);
if(keystate[SDLK_RETURN])
printf("Return Key Pressed.\n");
if(keystate[SDLK_RIGHT] && keystate[SDLK_UP])
printf("Right and Up Keys Pressed.\n");


if we make it sdl 2.0

1
2
3
4
5
[code]Uint8 *keystate = SDL_GetKeyboardState(NULL);
if(keystate[SDL_SCANCODE_RETURN])
printf("Return Key Pressed.\n");
if(keystate[SDL_SCANCODE__RIGHT] && keystate[SDL_SCANCODE__UP])
printf("Right and Up Keys Pressed.\n");



but i stuck at the point at making it with switch()

what i wanted
1
2
3
4
5
6
7
8
switch(???)//what should i put here
{
    case SDL_SCANCODE_DOWN:
        printf("Down Key Pressed\n");
        break;
    default:
        break;
}


i tried keystate[SDL_Scancode], keystate[NULL], keystate[] and even just keystate. and got nothing what should i use
You can't do it like that. switch makes decisions based on the value of a variable, but the only values that you can find in the array returned by that function are 0 and 1.
hmm thx for answering.
closed account (S6k9GNh0)
You don't need a switch. Here's what an example from one of my projects looks like:

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
32
33
34
35
36
37
38
39
40
SDL_PumpEvents(); 

const Uint8 * keys = SDL_GetKeyboardState(NULL);

if (SDL_QuitRequested()){
    bRunning = false;
    continue;
}

if (keys[SDL_GetScancodeFromKey(SDLK_w)]){
	...
}

if (keys[SDL_GetScancodeFromKey(SDLK_s)]) {
	...
}

if (keys[SDL_GetScancodeFromKey(SDLK_c)]) {
	...
}

if (keys[SDL_GetScancodeFromKey(SDLK_SPACE)]) {
	...
}

if (keys[SDL_GetScancodeFromKey(SDLK_a)]) {
	...
}

if (keys[SDL_GetScancodeFromKey(SDLK_d)]) {
	...
}

{
	int x, y;

	SDL_GetRelativeMouseState(&x, &y);

	...
}


Last edited on
i want to ask a question
why did you used:
if(keys[SDL_GetScancodeFromKey(SDLK_d)])

instead of:
if(keys[SDL_SCANCODE_D])

is there a thing that i should learn or both are fine??
Last edited on
closed account (S6k9GNh0)
A keycode is different from a scancode. A keycode corresponds to your locale where as your scancode corresponds to your hardware. SDL has fantastic documentation on it.

http://wiki.libsdl.org/MigrationGuide#Input

The only thing that's wrong with the migration guide is that it actually somewhat lies about how to grab input. It says you only need to call SDL_SetRelativeMouseMode(SDL_TRUE); which you do need to call but there's one other. That would be SDL_SetWindowGrab(pWindow, SDL_TRUE);.
Last edited on
ty @computerquip
Topic archived. No new replies allowed.