Archived

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

MPG

How to specify the key you want wwith sdl.

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 == .

Share this post


Link to post
Share on other sites