diff --git a/docs/Core.rst b/docs/Core.rst index ac17401dc..a8991147f 100644 --- a/docs/Core.rst +++ b/docs/Core.rst @@ -394,9 +394,6 @@ restarting DF. - ``dfhack.HIDE_ARMOK_TOOLS``: Whether to hide "armok" tools in command lists. -- ``dfhack.SUPPRESS_DUPLICATE_KEYBOARD_EVENTS``: Whether to prevent DFHack - keybindings from producing DF key events. - Miscellaneous notes =================== This section is for odd but important notes that don't fit anywhere else. diff --git a/docs/dev/Lua API.rst b/docs/dev/Lua API.rst index b52bf3c76..2cf1df52a 100644 --- a/docs/dev/Lua API.rst +++ b/docs/dev/Lua API.rst @@ -2857,6 +2857,12 @@ and are only documented here for completeness: Sets the system clipboard text from a CP437 string. +* ``dfhack.internal.getSuppressDuplicateKeyboardEvents()`` +* ``dfhack.internal.setSuppressDuplicateKeyboardEvents(suppress)`` + + Gets and sets the flag for whether to suppress DF key events when a DFHack + keybinding is matched and a command is launched. + .. _lua-core-context: Core interpreter context diff --git a/library/Core.cpp b/library/Core.cpp index 0a1c19351..c4b03a7ac 100644 --- a/library/Core.cpp +++ b/library/Core.cpp @@ -1423,6 +1423,7 @@ Core::Core() : memset(&(s_mods), 0, sizeof(s_mods)); // set up hotkey capture + suppress_duplicate_keyboard_events = true; hotkey_set = NO; last_world_data_ptr = NULL; last_local_map_ptr = NULL; @@ -1430,7 +1431,6 @@ Core::Core() : top_viewscreen = NULL; color_ostream::log_errors_to_stderr = true; - }; void Core::fatal (std::string output) @@ -2356,12 +2356,14 @@ bool Core::DFH_ncurses_key(int key) return ncurses_wgetch(key, dummy); } -static bool getSuppressDuplicateKeyboardEvents() { - auto L = Lua::Core::State; - color_ostream_proxy out(Core::getInstance().getConsole()); - Lua::StackUnwinder top(L); - return DFHack::Lua::PushModulePublic(out, L, "dfhack", "SUPPRESS_DUPLICATE_KEYBOARD_EVENTS") && - lua_toboolean(L, -1); +bool Core::getSuppressDuplicateKeyboardEvents() { + return suppress_duplicate_keyboard_events; +} + +void Core::setSuppressDuplicateKeyboardEvents(bool suppress) { + DEBUG(keybinding).print("setting suppress_duplicate_keyboard_events to %s\n", + suppress ? "true" : "false"); + suppress_duplicate_keyboard_events = suppress; } // returns true if the event is handled @@ -2396,9 +2398,10 @@ bool Core::DFH_SDL_Event(SDL_Event* ev) 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"); + DEBUG(keybinding).print("%sinhibiting SDL key down event\n", + suppress_duplicate_keyboard_events ? "" : "not "); hotkey_states[sym] = true; - return getSuppressDuplicateKeyboardEvents(); + return suppress_duplicate_keyboard_events; } } else if (ke.state == SDL_RELEASED) @@ -2411,8 +2414,9 @@ bool Core::DFH_SDL_Event(SDL_Event* ev) 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; + DEBUG(keybinding).print("%sinhibiting SDL text event\n", + suppress_duplicate_keyboard_events ? "" : "not "); + return suppress_duplicate_keyboard_events; } } diff --git a/library/LuaApi.cpp b/library/LuaApi.cpp index 2cd900934..050c99b86 100644 --- a/library/LuaApi.cpp +++ b/library/LuaApi.cpp @@ -3611,13 +3611,23 @@ static int internal_md5file(lua_State *L) } } +static int internal_getSuppressDuplicateKeyboardEvents(lua_State *L) { + Lua::Push(L, Core::getInstance().getSuppressDuplicateKeyboardEvents()); + return 1; +} + +static int internal_setSuppressDuplicateKeyboardEvents(lua_State *L) { + bool suppress = lua_toboolean(L, 1); + Core::getInstance().setSuppressDuplicateKeyboardEvents(suppress); + return 0; +} + static const luaL_Reg dfhack_internal_funcs[] = { { "getPE", internal_getPE }, { "getMD5", internal_getmd5 }, { "getAddress", internal_getAddress }, { "setAddress", internal_setAddress }, { "getVTable", internal_getVTable }, - { "adjustOffset", internal_adjustOffset }, { "getMemRanges", internal_getMemRanges }, { "patchMemory", internal_patchMemory }, @@ -3639,6 +3649,8 @@ static const luaL_Reg dfhack_internal_funcs[] = { { "getCommandDescription", internal_getCommandDescription }, { "threadid", internal_threadid }, { "md5File", internal_md5file }, + { "getSuppressDuplicateKeyboardEvents", internal_getSuppressDuplicateKeyboardEvents }, + { "setSuppressDuplicateKeyboardEvents", internal_setSuppressDuplicateKeyboardEvents }, { NULL, NULL } }; diff --git a/library/include/Core.h b/library/include/Core.h index 180b5cc7b..b470ebbde 100644 --- a/library/include/Core.h +++ b/library/include/Core.h @@ -159,6 +159,9 @@ namespace DFHack std::string findScript(std::string name); void getScriptPaths(std::vector *dest); + bool getSuppressDuplicateKeyboardEvents(); + void setSuppressDuplicateKeyboardEvents(bool suppress); + bool ClearKeyBindings(std::string keyspec); bool AddKeyBinding(std::string keyspec, std::string cmdline); std::vector ListKeyBindings(std::string keyspec); @@ -249,6 +252,7 @@ namespace DFHack }; int8_t modstate; + bool suppress_duplicate_keyboard_events; std::map > key_bindings; std::string hotkey_cmd; enum hotkey_set_t { diff --git a/library/lua/dfhack.lua b/library/lua/dfhack.lua index 860b56cc1..8ea5e9dac 100644 --- a/library/lua/dfhack.lua +++ b/library/lua/dfhack.lua @@ -63,11 +63,6 @@ function dfhack.getHideArmokTools() return dfhack.HIDE_ARMOK_TOOLS end -dfhack.SUPPRESS_DUPLICATE_KEYBOARD_EVENTS = true -function dfhack.getSuppressDuplicateKeyboardEvents() - return dfhack.SUPPRESS_DUPLICATE_KEYBOARD_EVENTS -end - -- Error handling safecall = dfhack.safecall