Public Group

#### Archived

This topic is now archived and is closed to further replies.

# How to specify the key you want wwith sdl.

This topic is 5203 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I know how to check if a key is pressed,but I donnt know how to specify which key I want...I try to look in the docs, but half the time they don''t work HELP!!!!!

##### Share on other sites
if(Event.key.keysym.sym == SDLK_a) {
std::cout << "You pressed a!\n";
}

Of course you'll need to distinguish between a key press and a key release.

You should have something like this:

An event function like this:
void OnEvent(SDL_Event* Event) {    switch(Event->type) {        case SDL_KEYDOWN: {	    OnKeyDown(Event->key.keysym.sym);	    break;        }        case SDL_KEYUP: {	    OnKeyUp(Event->key.keysym.sym);    	    break;        }    }}

And then:

   void OnKeyDown(SDLKey sym) {    switch(sym) {	case SDLK_ESCAPE: {            //Escape            break;        }    }}void OnKeyUp(SDLKey sym) {    switch(sym) {    }}

[edited by - MetaCipher on May 18, 2004 2:40:49 PM]

##### Share on other sites
Oh, look at SDL_keysym.h to see a list of keyboard keys. (Also note, you can check for unicode keys and mod keys by using .mod or .unicode instead of .sym at the end).

##### Share on other sites
can you explain how to use those funcion in the if statment please

##### Share on other sites
It''s not a function. I don''t know if you have done any OOP or used structures before, but the .sym at the end is a variable (int I believe) and you can just check the key with an equal == .

1. 1
2. 2
3. 3
4. 4
frob
15
5. 5

• 16
• 12
• 20
• 12
• 14
• ### Forum Statistics

• Total Topics
632155
• Total Posts
3004477

×