|
33 | 33 | TypeVar, |
34 | 34 | ) |
35 | 35 |
|
| 36 | +import numpy as np |
| 37 | + |
36 | 38 | import tcod.event_constants |
37 | 39 | from tcod.event_constants import * # noqa: F4 |
38 | 40 | 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: |
1046 | 1048 | return 0 |
1047 | 1049 |
|
1048 | 1050 |
|
| 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 | + |
1049 | 1114 | __all__ = [ # noqa: F405 |
1050 | 1115 | "Point", |
1051 | 1116 | "BUTTON_LEFT", |
@@ -1077,6 +1142,11 @@ def _pycall_event_watch(userdata: Any, sdl_event: Any) -> int: |
1077 | 1142 | "wait", |
1078 | 1143 | "get_mouse_state", |
1079 | 1144 | "EventDispatch", |
| 1145 | + "get_keyboard_state", |
| 1146 | + "get_modifier_state", |
| 1147 | + "key_from_scancode", |
| 1148 | + "scancode_from_key", |
| 1149 | + "get_key_name", |
1080 | 1150 | # --- From event_constants.py --- |
1081 | 1151 | "SCANCODE_UNKNOWN", |
1082 | 1152 | "SCANCODE_A", |
|
0 commit comments