prevent DF interface events for handled hotkeys

develop
Myk Taylor 2023-05-24 13:13:42 -07:00
parent 88732d724c
commit f987bca8f9
No known key found for this signature in database
GPG Key ID: 8A39CA0FA0C16E78
2 changed files with 28 additions and 10 deletions

@ -2359,6 +2359,8 @@ bool Core::DFH_ncurses_key(int key)
// returns true if the event is handled // returns true if the event is handled
bool Core::DFH_SDL_Event(SDL_Event* ev) bool Core::DFH_SDL_Event(SDL_Event* ev)
{ {
static std::map<int, bool> hotkey_states;
// do NOT process events before we are ready. // do NOT process events before we are ready.
if (!started || !ev) if (!started || !ev)
return false; return false;
@ -2372,23 +2374,40 @@ bool Core::DFH_SDL_Event(SDL_Event* ev)
if (ev->type == SDL_KEYDOWN || ev->type == SDL_KEYUP) { if (ev->type == SDL_KEYDOWN || ev->type == SDL_KEYUP) {
auto &ke = ev->key; auto &ke = ev->key;
auto &sym = ke.keysym.sym;
if (ke.keysym.sym == SDLK_LSHIFT || ke.keysym.sym == SDLK_RSHIFT) if (sym == SDLK_LSHIFT || sym == SDLK_RSHIFT)
modstate = (ev->type == SDL_KEYDOWN) ? modstate | DFH_MOD_SHIFT : modstate & ~DFH_MOD_SHIFT; modstate = (ev->type == SDL_KEYDOWN) ? modstate | DFH_MOD_SHIFT : modstate & ~DFH_MOD_SHIFT;
else if (ke.keysym.sym == SDLK_LCTRL || ke.keysym.sym == SDLK_RCTRL) else if (sym == SDLK_LCTRL || sym == SDLK_RCTRL)
modstate = (ev->type == SDL_KEYDOWN) ? modstate | DFH_MOD_CTRL : modstate & ~DFH_MOD_CTRL; modstate = (ev->type == SDL_KEYDOWN) ? modstate | DFH_MOD_CTRL : modstate & ~DFH_MOD_CTRL;
else if (ke.keysym.sym == SDLK_LALT || ke.keysym.sym == SDLK_RALT) else if (sym == SDLK_LALT || sym == SDLK_RALT)
modstate = (ev->type == SDL_KEYDOWN) ? modstate | DFH_MOD_ALT : modstate & ~DFH_MOD_ALT; modstate = (ev->type == SDL_KEYDOWN) ? modstate | DFH_MOD_ALT : modstate & ~DFH_MOD_ALT;
else if (ke.state == SDL_PRESSED && !hotkey_states[ke.keysym.sym]) else if (ke.state == SDL_PRESSED && !hotkey_states[sym])
{ {
hotkey_states[ke.keysym.sym] = true; // the check against hotkey_states[sym] ensures we only process keybindings once per keypress
SelectHotkey(ke.keysym.sym, modstate); DEBUG(keybinding).print("key down: sym=%d (%c)\n", sym, sym);
bool handled = SelectHotkey(sym, modstate);
if (handled) {
DEBUG(keybinding).print("inhibiting SDL key down event\n");
hotkey_states[sym] = true;
return true;
}
} }
else if (ke.state == SDL_RELEASED) else if (ke.state == SDL_RELEASED)
{ {
hotkey_states[ke.keysym.sym] = false; DEBUG(keybinding).print("key up: sym=%d (%c)\n", sym, sym);
hotkey_states[sym] = false;
} }
} }
else if (ev->type == SDL_TEXTINPUT) {
auto &te = ev->text;
DEBUG(keybinding).print("text input: '%s'\n", te.text);
if (strlen(te.text) == 1 && hotkey_states[te.text[0]]) {
DEBUG(keybinding).print("inhibiting SDL text event\n");
return true;
}
}
return false; return false;
} }

@ -250,7 +250,6 @@ namespace DFHack
int8_t modstate; int8_t modstate;
std::map<int, std::vector<KeyBinding> > key_bindings; std::map<int, std::vector<KeyBinding> > key_bindings;
std::map<int, bool> hotkey_states;
std::string hotkey_cmd; std::string hotkey_cmd;
enum hotkey_set_t { enum hotkey_set_t {
NO, NO,