Skip to content

Commit a47b52a

Browse files
committed
Added keyboard functions.
Includes a frequently requested keyboard state function.
1 parent 8113485 commit a47b52a

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

CHANGELOG.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ v2.0.0
88

99
Unreleased
1010
------------------
11+
- Added some keyboard functions:
12+
- `tcod.event.get_keyboard_state`
13+
- `tcod.event.get_modifier_state`
14+
- `tcod.event.key_from_scancode`
15+
- `tcod.event.scancode_from_key`
16+
- `tcod.event.get_key_name`
1117

1218
12.2.0 - 2021-04-09
1319
-------------------

tcod/event.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
TypeVar,
3434
)
3535

36+
import numpy as np
37+
3638
import tcod.event_constants
3739
from tcod.event_constants import * # noqa: F4
3840
from tcod.event_constants import KMOD_ALT, KMOD_CTRL, KMOD_GUI, KMOD_SHIFT
@@ -1046,6 +1048,69 @@ def _pycall_event_watch(userdata: Any, sdl_event: Any) -> int:
10461048
return 0
10471049

10481050

1051+
def get_keyboard_state() -> np.ndarray:
1052+
"""Return a boolean array with the current keyboard state.
1053+
1054+
Index this array with a scancode. The value will be True if the key is
1055+
currently held.
1056+
1057+
Example::
1058+
1059+
state = tcod.event.get_keyboard_state()
1060+
is_w_held = state[tcod.event.SCANCODE_W]
1061+
1062+
.. versionadded:: 12.3
1063+
"""
1064+
numkeys = ffi.new("int[1]")
1065+
keyboard_state = lib.SDL_GetKeyboardState(numkeys)
1066+
out: np.ndarray = np.frombuffer(
1067+
ffi.buffer(keyboard_state[0 : numkeys[0]]), dtype=bool
1068+
)
1069+
out.flags["WRITEABLE"] = False # This buffer is supposed to be const.
1070+
return out
1071+
1072+
1073+
def get_modifier_state() -> int:
1074+
"""Return a bitmask of the active keyboard modifiers.
1075+
1076+
.. versionadded:: 12.3
1077+
"""
1078+
return int(lib.SDL_GetModState())
1079+
1080+
1081+
def key_from_scancode(scancode: int) -> int:
1082+
"""Return a keycode from a scancode. Based on the current keyboard layout.
1083+
1084+
.. versionadded:: 12.3
1085+
"""
1086+
return int(lib.SDL_GetKeyFromScancode(scancode))
1087+
1088+
1089+
def scancode_from_key(keycode: int) -> int:
1090+
"""Return a scancode from a keycode. Based on the current keyboard layout.
1091+
1092+
.. versionadded:: 12.3
1093+
"""
1094+
return int(lib.SDL_GetScancodeFromKey(keycode))
1095+
1096+
1097+
def get_key_name(keycode: int) -> str:
1098+
"""Return a human-readable name of a keycode.
1099+
1100+
Returns "" if the keycode doesn't have a name.
1101+
1102+
Example::
1103+
1104+
>>> tcod.event.get_key_name(tcod.event.K_F1)
1105+
'F1'
1106+
>>> tcod.event.get_key_name(tcod.event.K_BACKSPACE)
1107+
'Backspace'
1108+
1109+
.. versionadded:: 12.3
1110+
"""
1111+
return str(ffi.string(lib.SDL_GetKeyName(keycode)), encoding="utf-8")
1112+
1113+
10491114
__all__ = [ # noqa: F405
10501115
"Point",
10511116
"BUTTON_LEFT",
@@ -1077,6 +1142,11 @@ def _pycall_event_watch(userdata: Any, sdl_event: Any) -> int:
10771142
"wait",
10781143
"get_mouse_state",
10791144
"EventDispatch",
1145+
"get_keyboard_state",
1146+
"get_modifier_state",
1147+
"key_from_scancode",
1148+
"scancode_from_key",
1149+
"get_key_name",
10801150
# --- From event_constants.py ---
10811151
"SCANCODE_UNKNOWN",
10821152
"SCANCODE_A",

0 commit comments

Comments
 (0)