how get ascii from Keyboard event
I need to know the ascii value of a key being pressed or released.
So what in the SDL_keysym structure do I need to read?
typedef struct{
Uint8 scancode;
SDLKey sym;
SDLMod mod;
Uint16 unicode;
} SDL_keysym;
When using the SDL_keysym structure, you will need to use the sym field, which is the SDL virtual keysym. Note that you cannot get a pure ascii value using this. You will get a SDLK_a for an A or an a, so you must work with the mod field to see if caps lock or shift is being pressed.
Alternatively, you can just use Unicode and it will contain the ascii key that is being pressed. In otherwords, if caps lock is down, and you hit a, then 'A' is outputted, and so on. Here's a little example I've thrown together showing how you can go about that.
Make sure that you call that SDL_EnableUNICODE(1); If you run into any troubles, let us know. Good luck!
One more thing I should note is that the Unicode will not be there for released keys. See this thread on one way to remedy that.
Alternatively, you can just use Unicode and it will contain the ascii key that is being pressed. In otherwords, if caps lock is down, and you hit a, then 'A' is outputted, and so on. Here's a little example I've thrown together showing how you can go about that.
#include <SDL.h>#pragma comment (lib, "SDL.lib")#pragma comment (lib, "SDLmain.lib")int main( int argc, char *argv[] ){ SDL_Init(SDL_INIT_VIDEO); SDL_Surface *screen = SDL_SetVideoMode(640, 480, 32, 0); // Enable unicode first! SDL_EnableUNICODE(1); int done = 0; while(!done) { SDL_Event event; while(SDL_PollEvent(&event)) { if(event.type == SDL_QUIT) done = 1; if(event.type == SDL_KEYDOWN) { char asciiChar; if((event.key.keysym.unicode & 0xFF80) == 0) { asciiChar = event.key.keysym.unicode & 0x7F; printf("%c", asciiChar); } else printf("An International Character.\n"); } } } return 0;}
Make sure that you call that SDL_EnableUNICODE(1); If you run into any troubles, let us know. Good luck!
One more thing I should note is that the Unicode will not be there for released keys. See this thread on one way to remedy that.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement