Skip to content

Commit b89d747

Browse files
committed
Cleanup pass
1 parent 05f6440 commit b89d747

File tree

5 files changed

+18
-15
lines changed

5 files changed

+18
-15
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# linux backup files
22
*~
3-
.cache
3+
44
# Kdevelop project files
55
*.kdev4
66
.kdev4

docs/builtins/keybinding.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ The ``<key>`` parameter above has the following case-insensitive syntax::
3232
[Ctrl-][Alt-][Super-][Shift-]KEY[@context[|context...]]
3333

3434
where the ``KEY`` part can be any recognized key and :kbd:`[`:kbd:`]` denote
35-
optional parts.
35+
optional parts. It is important to note that the key is the non-shifted version
36+
of the key. For example ``!`` would be defined as ``Shift-0``.
3637

3738
DFHack commands can advertise the contexts in which they can be usefully run.
3839
For example, a command that acts on a selected unit can tell `keybinding` that

docs/changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Template for new versions:
6161
## Fixes
6262

6363
## Misc Improvements
64+
- `keybinding`: keybinds may now include the super key, and are no longer limited to particular keys ranges of keys, allowing any recognized by SDL.
6465

6566
## Documentation
6667

library/include/Core.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,6 @@ namespace DFHack
270270
// Hotkey Manager
271271
DFHack::HotkeyManager *hotkey_mgr;
272272

273-
// FIXME: remove all this junk (hotkey related)
274273
std::vector<std::filesystem::path> script_paths[3];
275274
std::mutex script_path_mutex;
276275

library/modules/Hotkey.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
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"
@@ -12,8 +15,6 @@
1215
#include "df/viewscreen.h"
1316
#include "df/interfacest.h"
1417

15-
#include <ranges>
16-
#include <SDL_keycode.h>
1718

1819
using namespace DFHack;
1920
using 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

128129
bool 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

Comments
 (0)