11#include " modules/Hotkey.h"
22
3+ #include < ranges>
4+ #include < SDL_keycode.h>
5+
36#include " Core.h"
47#include " ColorText.h"
58#include " MiscUtils.h"
1215#include " df/viewscreen.h"
1316#include " df/interfacest.h"
1417
15- #include < ranges>
16- #include < SDL_keycode.h>
1718
1819using namespace DFHack ;
1920using Hotkey::KeySpec;
@@ -103,7 +104,7 @@ std::optional<KeySpec> KeySpec::parse(std::string spec, std::string* err) {
103104 // Attempt to parse as a mouse binding
104105 if (spec.starts_with (" mouse" )) {
105106 spec.erase (0 , 5 );
106- // Read button number, ensuring between 1 and 15 inclusive
107+ // Read button number, ensuring between 4 and 15 inclusive
107108 try {
108109 int mbutton = std::stoi (spec);
109110 if (mbutton >= 4 && mbutton <= 15 ) {
@@ -126,14 +127,15 @@ std::optional<KeySpec> KeySpec::parse(std::string spec, std::string* err) {
126127}
127128
128129bool KeySpec::isDisruptive () const {
129- // Miscellaneous essential keys
130+ // SDLK enum uses the actual characters for a key as its value.
131+ // Escaped values included are Return, Escape, Backspace, and Tab
130132 const std::string essential_key_set = " \r\x1B\b\t -=[]\\ ;',./" ;
131133
132134 // Letters A-Z, 0-9, and other special keys such as return/escape, and other general typing keys
133- bool is_essential_key = (this ->sym >= SDLK_a && this ->sym <= SDLK_z)
134- || (this ->sym >= SDLK_0 && this ->sym <= SDLK_9)
135+ bool is_essential_key = (this ->sym >= SDLK_a && this ->sym <= SDLK_z) // A-Z
136+ || (this ->sym >= SDLK_0 && this ->sym <= SDLK_9) // 0-9
135137 || essential_key_set.find (this ->sym ) != std::string::npos
136- || (this ->sym >= SDLK_LEFT && this ->sym <= SDLK_UP);
138+ || (this ->sym >= SDLK_LEFT && this ->sym <= SDLK_UP); // Arrow keys
137139
138140 // Essential keys are safe, so long as they have a modifier that isn't Shift
139141 if (is_essential_key && !(this ->modifiers & ~DFH_MOD_SHIFT))
@@ -306,11 +308,12 @@ bool HotkeyManager::handleKeybind(int sym, int modifiers) {
306308 if (!df::global::gview || !df::global::plotinfo)
307309 return false ;
308310
309- // Get bottommost active screen
311+ // Get topmost active screen
310312 df::viewscreen *screen = &df::global::gview->view ;
311313 while (screen->child )
312314 screen = screen->child ;
313315
316+ // Map keypad return to return
314317 if (sym == SDLK_KP_ENTER)
315318 sym = SDLK_RETURN;
316319
@@ -333,16 +336,15 @@ bool HotkeyManager::handleKeybind(int sym, int modifiers) {
333336 auto & core = Core::getInstance ();
334337 bool mortal_mode = core.getMortalMode ();
335338
339+ // Iterate in reverse, prioritizing the last added keybinds
336340 for (const auto & bind : binds | std::views::reverse) {
337341 if (bind.spec .modifiers != modifiers)
338342 continue ;
339343
340344 if (!bind.spec .focus .empty ()) {
341345 bool matched = false ;
342346 for (const auto & focus : bind.spec .focus ) {
343- printf (" Focus check for: %s" , focus.c_str ());
344347 if (Gui::matchFocusString (focus)) {
345- printf (" Matched\n " );
346348 matched = true ;
347349 break ;
348350 }
@@ -351,7 +353,7 @@ bool HotkeyManager::handleKeybind(int sym, int modifiers) {
351353 continue ;
352354 }
353355
354- if (!Core::getInstance () .getPluginManager ()->CanInvokeHotkey (bind.command , screen))
356+ if (!core .getPluginManager ()->CanInvokeHotkey (bind.command , screen))
355357 continue ;
356358
357359 if (mortal_mode && core.isArmokTool (bind.command ))
@@ -435,7 +437,7 @@ void HotkeyManager::handleKeybindingCommand(color_ostream &con, const std::vecto
435437 << " keybinding set <key>[@context] \" cmdline\" \" cmdline\" ..." << std::endl
436438 << " keybinding add <key>[@context] \" cmdline\" \" cmdline\" ..." << std::endl
437439 << " Later adds, and earlier items within one command have priority." << std::endl
438- << " Supported keys : [Ctrl-][Alt-][Super-][Shift-](A-Z, 0-9, F1-F12, `, etc.)." << std::endl
440+ << " Key format : [Ctrl-][Alt-][Super-][Shift-](A-Z, 0-9, F1-F12, `, etc.)." << std::endl
439441 << " Context may be used to limit the scope of the binding, by" << std::endl
440442 << " requiring the current context to have a certain prefix." << std::endl
441443 << " Current UI context is: " << std::endl
0 commit comments