Skip to content

Commit c9aa143

Browse files
committed
Update keyboard events to use enums.
1 parent 8771ee0 commit c9aa143

File tree

2 files changed

+33
-44
lines changed

2 files changed

+33
-44
lines changed

CHANGELOG.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ v2.0.0
88

99
Unreleased
1010
------------------
11+
Changed
12+
- `KeyboardEvent`'s '`scancode`, `sym`, and `mod` attributes now use their respective enums.
1113

1214
12.4.0 - 2021-05-21
1315
-------------------
@@ -18,9 +20,7 @@ Added
1820

1921
Changed
2022
- Using `libtcod 1.18.1`.
21-
22-
Fixed
23-
- `tcod.event.KeySym` and `tcod.event.Scancode ` can now be hashed.
23+
- `tcod.event.KeySym` and `tcod.event.Scancode` can now be hashed.
2424

2525
12.3.2 - 2021-05-15
2626
-------------------

tcod/event.py

+30-41
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,26 @@ def _init_sdl_video() -> None:
122122

123123

124124
class Modifier(enum.IntFlag):
125-
"""Keyboard modifier flags.
125+
"""Keyboard modifier flags, a bitfield of held modifier keys.
126+
127+
Use `bitwise and` to check if a modifier key is held.
128+
129+
The following example shows some common ways of checking modifiers.
130+
All non-zero return values are considered true.
131+
132+
Example::
133+
134+
>>> mod = tcod.event.Modifier(4098)
135+
>>> mod
136+
<Modifier.NUM|RSHIFT: 4098>
137+
>>> mod & tcod.event.Modifier.SHIFT # Check if any shift key is held.
138+
<Modifier.RSHIFT: 2>
139+
>>> mod & tcod.event.Modifier.LSHIFT # Check if left shift key is held.
140+
<Modifier.NONE: 0>
141+
>>> not mod & tcod.event.Modifier.LSHIFT # Check if left shift key is NOT held.
142+
True
143+
>>> mod & tcod.event.Modifier.SHIFT and mod & tcod.event.Modifier.CTRL # Check if Shift+Control is held.
144+
<Modifier.NONE: 0>
126145
127146
.. versionadded:: 12.3
128147
"""
@@ -258,58 +277,28 @@ class KeyboardEvent(Event):
258277
"""
259278
Attributes:
260279
type (str): Will be "KEYDOWN" or "KEYUP", depending on the event.
261-
scancode (int): The keyboard scan-code, this is the physical location
280+
scancode (Scancode): The keyboard scan-code, this is the physical location
262281
of the key on the keyboard rather than the keys symbol.
263-
sym (int): The keyboard symbol.
264-
mod (int): A bitmask of the currently held modifier keys.
265-
266-
You can use the following to check if a modifier key is held:
267-
268-
* `tcod.event.KMOD_LSHIFT`
269-
Left shift bit.
270-
* `tcod.event.KMOD_RSHIFT`
271-
Right shift bit.
272-
* `tcod.event.KMOD_LCTRL`
273-
Left control bit.
274-
* `tcod.event.KMOD_RCTRL`
275-
Right control bit.
276-
* `tcod.event.KMOD_LALT`
277-
Left alt bit.
278-
* `tcod.event.KMOD_RALT`
279-
Right alt bit.
280-
* `tcod.event.KMOD_LGUI`
281-
Left meta key bit.
282-
* `tcod.event.KMOD_RGUI`
283-
Right meta key bit.
284-
* `tcod.event.KMOD_SHIFT`
285-
``tcod.event.KMOD_LSHIFT | tcod.event.KMOD_RSHIFT``
286-
* `tcod.event.KMOD_CTRL`
287-
``tcod.event.KMOD_LCTRL | tcod.event.KMOD_RCTRL``
288-
* `tcod.event.KMOD_ALT`
289-
``tcod.event.KMOD_LALT | tcod.event.KMOD_RALT``
290-
* `tcod.event.KMOD_GUI`
291-
``tcod.event.KMOD_LGUI | tcod.event.KMOD_RGUI``
292-
* `tcod.event.KMOD_NUM`
293-
Num lock bit.
294-
* `tcod.event.KMOD_CAPS`
295-
Caps lock bit.
296-
* `tcod.event.KMOD_MODE`
297-
AltGr key bit.
282+
sym (KeySym): The keyboard symbol.
283+
mod (Modifier): A bitmask of the currently held modifier keys.
298284
299285
For example, if shift is held then
300-
``event.mod & tcod.event.KMOD_SHIFT`` will evaluate to a true
286+
``event.mod & tcod.event.Modifier.SHIFT`` will evaluate to a true
301287
value.
302288
303289
repeat (bool): True if this event exists because of key repeat.
290+
291+
.. versionchanged:: 12.5
292+
`scancode`, `sym`, and `mod` now use their respective enums.
304293
"""
305294

306295
def __init__(
307296
self, scancode: int, sym: int, mod: int, repeat: bool = False
308297
):
309298
super().__init__()
310-
self.scancode = scancode
311-
self.sym = sym
312-
self.mod = mod
299+
self.scancode = Scancode(scancode)
300+
self.sym = KeySym(sym)
301+
self.mod = Modifier(mod)
313302
self.repeat = repeat
314303

315304
@classmethod

0 commit comments

Comments
 (0)