Skip to content

Commit 2ee9d7e

Browse files
committed
Initial documentation pass, and improvements to lua api ergonomics
1 parent 43ecdad commit 2ee9d7e

File tree

3 files changed

+54
-8
lines changed

3 files changed

+54
-8
lines changed

docs/builtins/keybinding.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ Like any other command, it can be used at any time from the console, but
99
bindings are not remembered between runs of the game unless re-created in
1010
:file:`dfhack-config/init/dfhack.init`.
1111

12-
Hotkeys can be any combinations of Ctrl/Alt/Shift with A-Z, 0-9, F1-F12, or `
13-
(the key below the :kbd:`Esc` key on most keyboards). You can also represent
14-
mouse buttons beyond the first three with ``MOUSE4`` through ``MOUSE15``.
12+
Hotkeys can be any combinations of Ctrl/Alt/Shift with any key recognized by SDL.
13+
You can also represent mouse buttons beyond the first three with ``MOUSE4``
14+
through ``MOUSE15``.
1515

1616
Usage
1717
-----
@@ -27,7 +27,7 @@ Usage
2727
``keybinding set <key> "<cmdline>" ["<cmdline>" ...]``
2828
Clear, and then add bindings for the specified key.
2929

30-
The ``<key>`` parameter above has the following **case-sensitive** syntax::
30+
The ``<key>`` parameter above has the following case-insensitive syntax::
3131

3232
[Ctrl-][Alt-][Shift-]KEY[@context[|context...]]
3333

docs/dev/Lua API.rst

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,6 +1430,46 @@ Job module
14301430

14311431
Returns the job's description, as seen in the Units and Jobs screens.
14321432

1433+
Hotkey module
1434+
-------------
1435+
1436+
* ``dfhack.hotkey.addKeybind(keyspec, command)``
1437+
1438+
Creates a new keybind with the provided keyspec (see the `keybinding` documentation
1439+
for details on format).
1440+
Returns false on failure to create keybind.
1441+
1442+
* ``dfhack.hotkey.clearKeybind(keyspec, any_focus, command)``
1443+
1444+
Removes keybinds matching the provided keyspec.
1445+
If any_focus is true, the focus portion of the keyspec is ignored.
1446+
If command is not an empty string, the command is matched against as well.
1447+
Returns false if no keybinds were removed.
1448+
1449+
* ``dfhack.hotkey.listActiveKeybinds()``
1450+
1451+
Returns a list of keybinds active within the current context.
1452+
The items are tables with the following attributes:
1453+
:spec: The keyspec for the hotkey
1454+
:command: The command the hotkey runs when pressed
1455+
1456+
* ``dfhack.hotkey.listAllKeybinds()``
1457+
1458+
Returns a list of all keybinds currently registered.
1459+
The items are tables with the following attributes:
1460+
:spec: The keyspec for the hotkey
1461+
:command: The command the hotkey runs when pressed
1462+
1463+
* ``dfhack.hotkey.requestKeybindInput()``
1464+
1465+
Requests that the next hotkey-compatible input is saved and not processed,
1466+
retrievable with ``dfhack.hotkey.readKeybindInput()``.
1467+
1468+
* ``dfhack.hotkey.readKeybindInput()``
1469+
1470+
Reads the latest saved keybind input that was requested.
1471+
Returns a keyspec string for the input, or nil if no input is saved.
1472+
14331473
Units module
14341474
------------
14351475

library/LuaApi.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1885,10 +1885,16 @@ static void hotkey_requestKeybindingInput() {
18851885
if (hotkey_mgr) hotkey_mgr->requestKeybindInput();
18861886
}
18871887

1888-
static std::string hotkey_readKeybindInput() {
1888+
static int hotkey_readKeybindInput(lua_State *L) {
18891889
auto hotkey_mgr = Core::getInstance().getHotkeyManager();
1890-
if (!hotkey_mgr) return "";
1891-
return hotkey_mgr->readKeybindInput();
1890+
auto input = hotkey_mgr->readKeybindInput();
1891+
1892+
if (input.empty()) {
1893+
lua_pushnil(L);
1894+
} else {
1895+
lua_pushlstring(L, input.data(), input.size());
1896+
}
1897+
return 1;
18921898
}
18931899

18941900
void hotkey_pushBindArray(lua_State *L, const std::vector<Hotkey::KeyBinding>& binds) {
@@ -1928,14 +1934,14 @@ static int hotkey_listAllKeybinds(lua_State *L) {
19281934
static const luaL_Reg dfhack_hotkey_funcs[] = {
19291935
{ "listActiveKeybinds", hotkey_listActiveKeybinds },
19301936
{ "listAllKeybinds", hotkey_listAllKeybinds },
1937+
{ "readKeybindInput", hotkey_readKeybindInput },
19311938
{ NULL, NULL }
19321939
};
19331940

19341941
static const LuaWrapper::FunctionReg dfhack_hotkey_module[] = {
19351942
WRAPN(addKeybind, hotkey_addKeybind),
19361943
WRAPN(clearKeybind, hotkey_clearKeybind),
19371944
WRAPN(requestKeybindInput, hotkey_requestKeybindingInput),
1938-
WRAPN(readKeybindInput, hotkey_readKeybindInput),
19391945
{ NULL, NULL }
19401946
};
19411947

0 commit comments

Comments
 (0)