SDL Text Input
I was wondering if anyone has made a SDL keyboard input library? If not, I will code one myself. However I can see a problem. I was going to track key releases and record it into a char*. However, if a user holds the key, instead of returning `aaaaaaaaaaaaa`, I believe it will return `a`. `aaaaaaaaaa` is the desired result for holding a key for an extended period of time (such as any onther input). The other side of this, how do I stop users from typing `hhhhheeeeellllllllllllooo`
Why would you want to write a keyboard input library when SDL already has one built in?
Maybe he meant wrapper for easy using SDL keyboard functions? In such case it could be useful.
That's what SDL event queue is doing for you if you call SDL_EnableKeyRepeat(...), just find out good values (default ones are good) and that's it.
(btw, if you want I can share code from my (working and tested) Input manager)
Quote:
However, if a user holds the key, instead of returning `aaaaaaaaaaaaa`, I believe it will return `a`. `aaaaaaaaaa` is the desired result for holding a key for an extended period of time (such as any onther input). The other side of this, how do I stop users from typing `hhhhheeeeellllllllllllooo`
That's what SDL event queue is doing for you if you call SDL_EnableKeyRepeat(...), just find out good values (default ones are good) and that's it.
(btw, if you want I can share code from my (working and tested) Input manager)
Quote:
Original post by Zeophlite
I was wondering if anyone has made a SDL keyboard input library?
If not, I will code one myself. However I can see a problem.
I was going to track key releases and record it into a char*.
However, if a user holds the key, instead of returning `aaaaaaaaaaaaa`, I believe it will return `a`. `aaaaaaaaaa` is the desired result for holding a key for an extended period of time (such as any onther input). The other side of this, how do I stop users from typing `hhhhheeeeellllllllllllooo`
I actually have in my engine. It is based on this tutorial: http://cone3d.gamedev.net/cgi-bin/index.pl?page=tutorials/gfxsdl/tut6 I just wrapped up the concepts used, and made a few modifications myself.
This is my modified code, just using keyboard ( I took out the mouse ),credits given to Marius Andra for the concepts [wink]:
// SDL_Input.cppCSDL_Input::CSDL_Input(){ current_key = -1; keys = 0;}CSDL_Input::~CSDL_Input(){}bool CSDL_Input::KeyDown(int key){ current_key = key; return (keys[key] == 1);}void CSDL_Input::ClearKey(){ if( current_key > 0) { keys[current_key] = 0; current_key = -1; }}void CSDL_Input::ClearKey(int key){ keys[key] = 0; current_key = -1;}void CSDL_Input::Update(){ keys = SDL_GetKeyState(NULL);}
// SDL_Input.hclass CSDL_Input{private: Uint8* keys; int current_key;public: CSDL_Input(); ~CSDL_Input(); bool KeyDown(int key); void ClearKey(int key); void ClearKey();};
Here is a simpe example of how to use it:
CSDL_Input Input;...void Update(){ Input.Update(); if( Input.KeyDown(SDLK_a) ) { // process stuff here Input.ClearKey(); // Clears the last key you checked in KeyDown() // you could also have called Input.ClearKey(SDLK_a); }}
Of course you would have to customized the processing for any key pressed, but that should give you a great start. Good luck!
PS. Here is an example of getting input to a string (since I feel like helping [WINK]):
CSDL_Input Input;char buffer[256];int cur = 0;void Update(){ Input.Update(); for(int x=SDLK_a;x<SDLK_z;x++) { if( Input.KeyDown(x) ) { buffer[cur] = x; cur++; break; } } if( Input.KeyDown(SDLK_BACKSPACE) ) { if(cur > 0) { buffer[cur] = ' '; cur--; } } // Handle tab,space,etc as well}
Kind of inefficient, but you won't continously be running it throughout your program, hopefully just at one spot.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement