Skip to content

Commit d1f37df

Browse files
committed
Allow keyboard conversions before video is initialized.
Update docs for key enums.
1 parent 3a3d746 commit d1f37df

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

tcod/event.py

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,23 @@ def _verify_tile_coordinates(xy: Optional[Point]) -> Point:
109109
return Point(0, 0)
110110

111111

112+
_is_sdl_video_initialized = False
113+
114+
115+
def _init_sdl_video() -> None:
116+
"""Keyboard layout stuff needs SDL to be initialized first."""
117+
global _is_sdl_video_initialized
118+
if _is_sdl_video_initialized:
119+
return
120+
lib.SDL_InitSubSystem(lib.SDL_INIT_VIDEO)
121+
_is_sdl_video_initialized = True
122+
123+
112124
class Modifier(enum.IntFlag):
113-
"""Keyboard modifiers."""
125+
"""Keyboard modifier flags.
126+
127+
.. versionadded:: 12.3
128+
"""
114129

115130
NONE = 0
116131
LSHIFT = 1
@@ -1131,6 +1146,8 @@ class Scancode(enum.IntEnum):
11311146
These names are derived from SDL expect for the numbers which are prefixed
11321147
with ``N`` (since raw numbers can not be a Python name.)
11331148
1149+
.. versionadded:: 12.3
1150+
11341151
================== ===
11351152
UNKNOWN 0
11361153
A 4
@@ -1623,12 +1640,13 @@ class Scancode(enum.IntEnum):
16231640

16241641
@property
16251642
def label(self) -> str:
1626-
"""Return a human-readable name of a key based on a scancode.
1643+
"""Return a human-readable name of a key based on its scancode.
1644+
1645+
Be sure not to confuse this with ``.name``, which will return the enum
1646+
name rather than the human-readable name.
16271647
16281648
.. seealso::
16291649
:any:`KeySym.label`
1630-
1631-
.. versionadded:: 12.3
16321650
"""
16331651
return self.keysym.label
16341652

@@ -1637,9 +1655,8 @@ def keysym(self) -> "KeySym":
16371655
"""Return a :class:`KeySym` from a scancode.
16381656
16391657
Based on the current keyboard layout.
1640-
1641-
.. versionadded:: 12.3
16421658
"""
1659+
_init_sdl_video()
16431660
return KeySym(lib.SDL_GetKeyFromScancode(self.value))
16441661

16451662
@property
@@ -1650,8 +1667,6 @@ def scancode(self) -> "Scancode":
16501667
16511668
.. seealso::
16521669
:any:`KeySym.scancode`
1653-
1654-
.. versionadded:: 12.3
16551670
"""
16561671
return self
16571672

@@ -1665,7 +1680,12 @@ def __eq__(self, other: Any) -> bool:
16651680

16661681

16671682
class KeySym(enum.IntEnum):
1668-
"""Key syms
1683+
"""Keyboard constants based on their symbol.
1684+
1685+
These names are derived from SDL expect for the numbers which are prefixed
1686+
with ``N`` (since raw numbers can not be a Python name.)
1687+
1688+
.. versionadded:: 12.3
16691689
16701690
================== ==========
16711691
UNKNOWN 0
@@ -2154,14 +2174,15 @@ def label(self) -> str:
21542174
21552175
Returns "" if the keycode doesn't have a name.
21562176
2177+
Be sure not to confuse this with ``.name``, which will return the enum
2178+
name rather than the human-readable name.
2179+
21572180
Example::
21582181
21592182
>>> tcod.event.KeySym.F1.label
21602183
'F1'
21612184
>>> tcod.event.KeySym.BACKSPACE.label
21622185
'Backspace'
2163-
2164-
.. versionadded:: 12.3
21652186
"""
21662187
return str(
21672188
ffi.string(lib.SDL_GetKeyName(self.value)), encoding="utf-8"
@@ -2175,8 +2196,6 @@ def keysym(self) -> "KeySym":
21752196
21762197
.. seealso::
21772198
:any:`Scancode.keysym`
2178-
2179-
.. versionadded:: 12.3
21802199
"""
21812200
return self
21822201

@@ -2185,9 +2204,8 @@ def scancode(self) -> Scancode:
21852204
"""Return a scancode from a keycode.
21862205
21872206
Based on the current keyboard layout.
2188-
2189-
.. versionadded:: 12.3
21902207
"""
2208+
_init_sdl_video()
21912209
return Scancode(lib.SDL_GetScancodeFromKey(self.value))
21922210

21932211
def __eq__(self, other: Any) -> bool:

0 commit comments

Comments
 (0)