Sign in to follow this  
Uphoreum

[SDL] Function For Key Tests

Recommended Posts

Testing for key input with SDL events is kind of annoying to me to have to write all that code. I've been trying to figure out how I could make a function where the keycode could be passed as an argument and a boolean value as a return. I guess my problem is, I don't know what type of argument I would use. I don't know what: SDL_QUIT, SDL_KEYDOWN etc. are. Does anyone know?

Share this post


Link to post
Share on other sites
You don't have to create a function for this, you can use keystates.


Uint8 *keys = SDL_GetKeyState(NULL);

if (keys[SDLK_LEFT])
// left is down

if (keys[SDLK_i])
// i is down



Place it anywhere outside the event loop.

Share this post


Link to post
Share on other sites
This probably isn't the best of ways to do it, but I use this:
//Key_Input.h
#include <iostream>

#define KeyDown(vk_code) ((GetAsyncKeyState(vk_code)&0x8000)?1:0)

enum{KEY_UP = 1, KEY_DOWN, KEY_LEFT, KEY_RIGHT, KEY_LEFTMOUSE, KEY_RIGHTMOUSE, KEY_MIDDLEMOUSE, KEY_BACKSPACE,
KEY_TAB, KEY_ENTER, KEY_LEFTALT, KEY_RIGHTALT, KEY_ALT, KEY_CAPSLOCK, KEY_SCROLLLOCK, KEY_NUMLOCK, KEY_ESC,
KEY_SPACE, KEY_PAGEUP, KEY_PAGEDOWN, KEY_END, KEY_HOME, KEY_PRINTSCREEN, KEY_INS, KEY_DEL, KEY_0, KEY_1,
KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, KEY_7, KEY_8, KEY_9, KEY_A, KEY_B, KEY_C, KEY_D, KEY_E, KEY_F, KEY_G,
KEY_H, KEY_I, KEY_J, KEY_K, KEY_L, KEY_M, KEY_N, KEY_O, KEY_P, KEY_Q, KEY_R, KEY_S, KEY_T, KEY_U, KEY_V,
KEY_W, KEY_X, KEY_Y, KEY_Z, KEY_NUMPAD0, KEY_NUMPAD1, KEY_NUMPAD2, KEY_NUMPAD3, KEY_NUMPAD4, KEY_NUMPAD5,
KEY_NUMPAD6, KEY_NUMPAD7, KEY_NUMPAD8, KEY_NUMPAD9, KEY_F1, KEY_F2, KEY_F3, KEY_F4, KEY_F5, KEY_F6, KEY_F7,
KEY_F8, KEY_F9, KEY_F10, KEY_F11, KEY_F12, KEY_LEFTSHIFT, KEY_RIGHTSHIFT, KEY_SHIFT, KEY_LEFTCONTROL,
KEY_RIGHTCONTROL, KEY_CONTROL};


short Key()
{
if(KeyDown(VK_UP)){return KEY_UP;}
else if(KeyDown(VK_DOWN)){return KEY_DOWN;}
else if(KeyDown(VK_LEFT)){return KEY_LEFT;}
else if(KeyDown(VK_RIGHT)){return KEY_RIGHT;}
else if(KeyDown(VK_SPACE)){return KEY_SPACE;}
else if(KeyDown(VK_LSHIFT)){return KEY_LEFTSHIFT;}
else if(KeyDown(VK_RSHIFT)){return KEY_RIGHTSHIFT;}
else if(KeyDown(VK_SHIFT)){return KEY_SHIFT;}
else if(KeyDown(VK_LCONTROL)){return KEY_LEFTCONTROL;}
else if(KeyDown(VK_RCONTROL)){return KEY_RIGHTCONTROL;}
else if(KeyDown(VK_LBUTTON)){return KEY_LEFTMOUSE;}
else if(KeyDown(VK_RBUTTON)){return KEY_RIGHTMOUSE;}
else if(KeyDown(VK_MBUTTON)){return KEY_MIDDLEMOUSE;}
else if(KeyDown(VK_SPACE)){return KEY_SPACE;}
else if(KeyDown(VK_ESCAPE)){return KEY_ESC;}
else if(KeyDown(VK_RETURN)){return KEY_ENTER;}
else if(KeyDown(VK_LMENU)){return KEY_LEFTALT;}
else if(KeyDown(VK_RMENU)){return KEY_RIGHTALT;}
else if(KeyDown(VK_MENU)){return KEY_ALT;}
else if(KeyDown(VK_CAPITAL)){return KEY_CAPSLOCK;}
else if(KeyDown(VK_TAB)){return KEY_TAB;}
else if(KeyDown(VK_BACK)){return KEY_BACKSPACE;}
else if(KeyDown(VK_PRIOR)){return KEY_PAGEUP;}
else if(KeyDown(VK_NEXT)){return KEY_PAGEDOWN;}
else if(KeyDown(VK_END)){return KEY_END;}
else if(KeyDown(VK_HOME)){return KEY_HOME;}
else if(KeyDown(VK_SNAPSHOT)){return KEY_PRINTSCREEN;}
else if(KeyDown(VK_INSERT)){return KEY_INS;}
else if(KeyDown(VK_DELETE)){return KEY_DEL;}
else if(KeyDown(VK_SCROLL)){return KEY_SCROLLLOCK;}
else if(KeyDown(VK_NUMLOCK)){return KEY_NUMLOCK;}
else if(KeyDown(VK_F1)){return KEY_F1;}
else if(KeyDown(VK_F2)){return KEY_F2;}
else if(KeyDown(VK_F3)){return KEY_F3;}
else if(KeyDown(VK_F4)){return KEY_F4;}
else if(KeyDown(VK_F5)){return KEY_F5;}
else if(KeyDown(VK_F6)){return KEY_F6;}
else if(KeyDown(VK_F7)){return KEY_F7;}
else if(KeyDown(VK_F8)){return KEY_F8;}
else if(KeyDown(VK_F9)){return KEY_F9;}
else if(KeyDown(VK_F10)){return KEY_F10;}
else if(KeyDown(VK_F11)){return KEY_F11;}
else if(KeyDown(VK_F12)){return KEY_F12;}
else if(KeyDown(VK_NUMPAD0)){return KEY_NUMPAD0;}
else if(KeyDown(VK_NUMPAD1)){return KEY_NUMPAD1;}
else if(KeyDown(VK_NUMPAD2)){return KEY_NUMPAD2;}
else if(KeyDown(VK_NUMPAD3)){return KEY_NUMPAD3;}
else if(KeyDown(VK_NUMPAD4)){return KEY_NUMPAD4;}
else if(KeyDown(VK_NUMPAD5)){return KEY_NUMPAD5;}
else if(KeyDown(VK_NUMPAD6)){return KEY_NUMPAD6;}
else if(KeyDown(VK_NUMPAD7)){return KEY_NUMPAD7;}
else if(KeyDown(VK_NUMPAD8)){return KEY_NUMPAD8;}
else if(KeyDown(VK_NUMPAD9)){return KEY_NUMPAD9;}
else if(KeyDown(0x30)){return KEY_0;}
else if(KeyDown(0x31)){return KEY_1;}
else if(KeyDown(0x32)){return KEY_2;}
else if(KeyDown(0x33)){return KEY_3;}
else if(KeyDown(0x34)){return KEY_4;}
else if(KeyDown(0x35)){return KEY_5;}
else if(KeyDown(0x36)){return KEY_6;}
else if(KeyDown(0x37)){return KEY_7;}
else if(KeyDown(0x38)){return KEY_8;}
else if(KeyDown(0x39)){return KEY_9;}
else if(KeyDown(0x41)){return KEY_A;}
else if(KeyDown(0x42)){return KEY_B;}
else if(KeyDown(0x43)){return KEY_C;}
else if(KeyDown(0x44)){return KEY_D;}
else if(KeyDown(0x45)){return KEY_E;}
else if(KeyDown(0x46)){return KEY_F;}
else if(KeyDown(0x47)){return KEY_G;}
else if(KeyDown(0x48)){return KEY_H;}
else if(KeyDown(0x49)){return KEY_I;}
else if(KeyDown(0x4A)){return KEY_J;}
else if(KeyDown(0x4B)){return KEY_K;}
else if(KeyDown(0x4C)){return KEY_L;}
else if(KeyDown(0x4D)){return KEY_M;}
else if(KeyDown(0x4E)){return KEY_N;}
else if(KeyDown(0x4F)){return KEY_O;}
else if(KeyDown(0x50)){return KEY_P;}
else if(KeyDown(0x51)){return KEY_Q;}
else if(KeyDown(0x52)){return KEY_R;}
else if(KeyDown(0x53)){return KEY_S;}
else if(KeyDown(0x54)){return KEY_T;}
else if(KeyDown(0x55)){return KEY_U;}
else if(KeyDown(0x56)){return KEY_V;}
else if(KeyDown(0x57)){return KEY_W;}
else if(KeyDown(0x58)){return KEY_X;}
else if(KeyDown(0x59)){return KEY_Y;}
else if(KeyDown(0x5A)){return KEY_Z;}
else{return 0;}
}


I just have it in a header program, and include it in the projects that need it. To use it I go like:

if(Key() == KEY_ESC){//Exit the program, or do whatever}

Or like this:

switch(Key())
{
case KEY_UP: /*Up arrow was pressed*/ break;
case KEY_DOWN: /*Up arrow was pressed*/ break;
case KEY_LEFT: /*Up arrow was pressed*/ break;
case KEY_RIGHT: /*Up arrow was pressed*/ break;
case KEY_ENTER: /*enter was pressed*/ break;
}


Hmm, I'm not sure whether it'll hold up under multiple keypresses at the same time though.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this