SDL Geting text input
Posted: Thu May 20, 2010 7:00 am
I'm trying to get text input for a console... but it doesn't seem to be working, and I'm pretty lost to figure out what the problem.
Here is some code. The console is not receiving any input. I mean... c = getKey() is 0 every time.
Here is the function that calls the console::update() function:
renderConsole() gets executed every about 40ms.
Any idea on why the console is not getting any input whatsoever?
Here is some code. The console is not receiving any input. I mean... c = getKey() is 0 every time.
Code: Select all
void Console::handle_input(SDL_Event& event)
{
SDL_EnableUNICODE(SDL_ENABLE);
if(event.type == SDL_KEYDOWN) {
handleKey(event.key);
}
SDL_EnableUNICODE(SDL_DISABLE);
}
void Console::handleKey(const SDL_KeyboardEvent &event)
{
switch(event.keysym.sym) {
case SDLK_BACKSPACE:
if(console_input.size()) {
console_input.erase(--index,1);
} break;
case SDLK_DELETE:
if(index < console_input.size()) {
console_input.erase(index,1);
} break;
case SDLK_LEFT:
if(index <= 0) index = 0;
else index--;
break;
case SDLK_RIGHT:
if(index >= console_input.size()) index = console_input.size();
else index++;
break;
default:
char c = getKey(event);
if(c) console_input += c;
break;
}
}
char Console::getKey(const SDL_KeyboardEvent& event)
{
if(SDL_EnableUNICODE(SDL_QUERY) == SDL_ENABLE) {
Singleton<Log>::getInstance()->print("SDL_EnableUNICODE == SDL_ENABLE)");
} else {
Singleton<Log>::getInstance()->print("SDL_EnableUNICODE == SDL_DISABLED returning)");
return '0';
}
const int INTERNATIONAL_MASK = 0xFF80, UNICODE_MASK = 0x7F;
int uni = event.keysym.unicode;
if(!uni) {
return 0; //this always gets executed
} else if( ( uni & INTERNATIONAL_MASK ) == 0 ) {
return static_cast<char>(uni & UNICODE_MASK); //this never gets executed
} else // we have a funky international character. one we can't read :(
{
// we could do nothing, or we can just show some sign of input, like so:
return 0;
}
}
renderConsole() gets executed every about 40ms.
Code: Select all
void Game::renderConsole()
{
SDL_Event event;
if(SDL_PollEvent(&event)) {
if(event.type == SDL_QUIT) gameState->setState(EXIT);
else if(event.type == SDL_KEYDOWN) {
if(event.key.keysym.sym == SDLK_BACKQUOTE) {
gameState->setState(GAMEOVER);
}
}
}
Log* log = Singleton<Log>::getInstance();
Console* console = Singleton<Console>::getInstance();
console->update(event);
}