You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
1.2 KiB
57 lines
1.2 KiB
/* keyborad handling for text fields */
|
|
|
|
#include <SDL.h>
|
|
#include <string.h>
|
|
#include "keybinput.h"
|
|
|
|
static int keybinput_oldkey = 0;
|
|
|
|
void keybinput_new (_keybinput *ki) {
|
|
int i;
|
|
|
|
for (i = 0; i < 255; i++)
|
|
ki->text[i] = 0;
|
|
ki->curpos = 0;
|
|
ki->len = 0;
|
|
}
|
|
|
|
|
|
int keybinput_loop (_keybinput *ki, SDL_Event *event) {
|
|
int key = 0, keyu = 0;
|
|
|
|
ki->changed = 0;
|
|
|
|
if (event->type == SDL_KEYDOWN && keybinput_oldkey != event->key.keysym.sym) {
|
|
key = keybinput_oldkey = event->key.keysym.sym;
|
|
keyu = event->key.keysym.unicode;
|
|
|
|
if (key == 8) { // BACKSPACE
|
|
if (ki->curpos > 0) {
|
|
ki->curpos--;
|
|
ki->text[ki->curpos] = 0;
|
|
ki->changed = 1;
|
|
}
|
|
}
|
|
else if ((keyu >= 32 && keyu <= 126) || (keyu >= 128 && keyu <= 255)) { // International keyboard support
|
|
if (ki->curpos < 255) {
|
|
ki->text[ki->curpos++] = event->key.keysym.unicode;
|
|
ki->text[ki->curpos] = 0;
|
|
ki->changed = 1;
|
|
}
|
|
}
|
|
ki->len = strlen (ki->text);
|
|
}
|
|
|
|
if (keybinput_oldkey == SDLK_RETURN && event->type == SDL_KEYUP)
|
|
keyu = 1;
|
|
else if (keybinput_oldkey == SDLK_ESCAPE && event->type == SDL_KEYUP)
|
|
keyu = -1;
|
|
else
|
|
keyu = 0;
|
|
|
|
if (event->type == SDL_KEYUP)
|
|
keybinput_oldkey = 0;
|
|
|
|
return keyu;
|
|
}
|