SDL - How to keyboard input

How can i declare SDLK_LCTRL and SDL_RCTRL + an character?
Can i write like this:
1
2
char character;
if(character == SDLK_LCTRL + 'a')

or
1
2
char character;
if(character == 'SDLK_LCTRL' + 'a')
Last edited on
I recommend const Uint8* SDL_GetKeyboardState(int* numkeys)
https://wiki.libsdl.org/SDL_GetKeyboardState

Then you could do something like:
1
2
3
if((state[SDL_SCANCODE_LCTRL] || state[SDL_SCANCODE_RCTRL]) && state[SDL_SCANCODE_A]) {
    //do stuff for ctrl+a
}
In SDL we normally use SDL_PollEvent to check for keypresses and other events.

1
2
3
4
5
6
7
8
9
10
11
SDL_Event event;
while (SDL_PollEvent(&event))
{
	if(event.type == SDL_KEYDOWN)
	{
		if (event.keysym.sym == SDLK_a)
		{
			// A has been pressed
		}
	}
}

https://wiki.libsdl.org/SDL_PollEvent
https://wiki.libsdl.org/SDL_Event
https://wiki.libsdl.org/SDL_KeyboardEvent


To check which modifier keys are being held down you can use SDL_GetModState.

1
2
3
4
5
SDL_Keymod modstates = SDL_GetModState();
if (modstates & KMOD_CTRL)
{
	// One of the Ctrl keys are being held down
}

https://wiki.libsdl.org/SDL_GetModState
https://wiki.libsdl.org/SDL_Keymod


If you don't want the code to execute multiple times for as long as you hold down the keys you should also check that the repeat value of the event is false.

1
2
3
4
if (!event.repeat)
{
	// This is not a repeated event.
}

https://wiki.libsdl.org/SDL_KeyboardEvent


Putting it all together we get something like this (untested).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
SDL_Event event;
while (SDL_PollEvent(&event))
{
	if(event.type == SDL_KEYDOWN)
	{
		if (event.keysym.sym == SDLK_a && !event.repeat)
		{
			SDL_Keymod modstates = SDL_GetModState();
			if (modstates & KMOD_CTRL)
			{
				// Ctrl+A has been pressed!
			}
		}
	}
}
Last edited on
@kevinkjt2000

error: 'Uint8' does not name a type
error: 'state' was not declared in this scope
error: 'SDL_SCANCODE_LCTRL' was not declared in this scope
error: 'SDL_SCANCODE_RCTRL' was not declared in this scope
error: 'SDL_SCANCODE_A' was not declared in this scope -> may it is because i cant install SDL library please help post -> http://www.cplusplus.com/forum/windows/182070/

where it says here
 
const Uint8* SDL_GetKeyboardState(int* numkeys)

does it has to be
 
const Uint8* SDL_GetKeyboardState(char* {and something else})

char -> because it is character
and {something else} -> because it isnt a numkey but it is character


Also want to ask about left click and right click?
Last edited on
Have you included the SDL.h header?

 
#include "SDL.h" 
@Peter87
Cant install SDL library see post -> http://www.cplusplus.com/forum/windows/182070/
The same problem with Codeblocks and Visual Studio.
Cant install SDL library see post

Then why do you expect that your program can actually use SDL?
See Lazy Foo's tutorial on how to install SDL for various IDEs and OSes:
http://lazyfoo.net/tutorials/SDL/01_hello_SDL/index.php

If you still can not install SDL properly, then you had better give up learning it for the time being and switch to a different graphics library. There is no sense in learning something that you are unable to use or practice.
@kevinkjt2000
I installed SDL library in console program format for now but now:
How to declare state?
|error: 'state' was not declared in this scope|

if ( character == (state[SDL_SCANCODE_LCTRL] || state[SDL_SCANCODE_RCTRL]) && state[SDL_SCANCODE_A] )
Last edited on
Did you not look at the example code from the link I posted earlier?
https://wiki.libsdl.org/SDL_GetKeyboardState
@kevinkjt2000
I looked and i wrote same like you said state was not declared.
Under the big bold Code Examples?
1
2
3
4
5
6
7
const Uint8 *state = SDL_GetKeyboardState(NULL);
if (state[SDL_SCANCODE_RETURN]) {
    printf("<RETURN> is pressed.\n");
}
if (state[SDL_SCANCODE_RIGHT] && state[SDL_SCANCODE_UP]) {
    printf("Right and Up Keys Pressed.\n");
}

See the first line? There is an example of declaring a variable called state

Edit: Here is a link to Lazy Foo's tutorial on keyboard state:
http://lazyfoo.net/tutorials/SDL/18_key_states/index.php
Last edited on
@kevinkjt2000

I installed sdl library properly. Doesnt work i get this error->
error: 'state' was not declared in this scope
Try this, go to the line in your code that the error is pointing to and add this line above it.

const Uint8 *state = SDL_GetKeyboardState(NULL);

@Moooce
Doesnt work
are you are saying it still says state is not defined? you need to help us to help you as we cannot see what you are seeing.
Topic archived. No new replies allowed.