Skip to content

Commit 87ecd62

Browse files
committed
Fix lowercase letter KeySym regressions
Add single number aliases to KeySym Partially update KeySym docs
1 parent f88862b commit 87ecd62

File tree

2 files changed

+93
-28
lines changed

2 files changed

+93
-28
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@ This project adheres to [Semantic Versioning](https://semver.org/) since version
66

77
## [Unreleased]
88

9+
### Added
10+
11+
- Alternative syntax for number symbols with `KeySym`, can now specify `KeySym["3"]`, etc.
12+
Only available on Python 3.13 or later.
13+
14+
### Fixed
15+
16+
- Fixed regression with lowercase key symbols with `tcod.event.K_*` and `KeySym.*` constants, these are still deprecated.
17+
Event constants are only fixed for `tcod.event.K_*`, not the undocumented `tcod.event_constants` module.
18+
Lowercase `KeySym.*` constants are only available on Python 3.13 or later.
19+
920
## [19.5.0] - 2025-09-13
1021

1122
### Changed

tcod/event.py

Lines changed: 82 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484

8585
import enum
8686
import functools
87+
import sys
8788
import warnings
8889
from collections.abc import Callable, Iterator, Mapping
8990
from typing import TYPE_CHECKING, Any, Final, Generic, Literal, NamedTuple, TypeVar
@@ -2230,11 +2231,19 @@ def __repr__(self) -> str:
22302231
class KeySym(enum.IntEnum):
22312232
"""Keyboard constants based on their symbol.
22322233
2233-
These names are derived from SDL except for the numbers which are prefixed
2234-
with ``N`` (since raw numbers can not be a Python name.)
2234+
These names are derived from SDL except for numbers which are prefixed with ``N`` (since raw numbers can not be a Python name).
2235+
Alternatively ``KeySym["9"]`` can be used to represent numbers (since Python 3.13).
22352236
22362237
.. versionadded:: 12.3
22372238
2239+
.. versionchanged:: 19.0
2240+
SDL backend was updated to 3.x, which means some enums have been renamed.
2241+
Single letters are now uppercase.
2242+
2243+
.. versionchanged:: Unreleased
2244+
Number symbols can now be fetched with ``KeySym["9"]``, etc.
2245+
With Python 3.13 or later.
2246+
22382247
================== ==========
22392248
UNKNOWN 0
22402249
BACKSPACE 8
@@ -2280,32 +2289,32 @@ class KeySym(enum.IntEnum):
22802289
CARET 94
22812290
UNDERSCORE 95
22822291
BACKQUOTE 96
2283-
a 97
2284-
b 98
2285-
c 99
2286-
d 100
2287-
e 101
2288-
f 102
2289-
g 103
2290-
h 104
2291-
i 105
2292-
j 106
2293-
k 107
2294-
l 108
2295-
m 109
2296-
n 110
2297-
o 111
2298-
p 112
2299-
q 113
2300-
r 114
2301-
s 115
2302-
t 116
2303-
u 117
2304-
v 118
2305-
w 119
2306-
x 120
2307-
y 121
2308-
z 122
2292+
A 97
2293+
B 98
2294+
C 99
2295+
D 100
2296+
E 101
2297+
F 102
2298+
G 103
2299+
H 104
2300+
I 105
2301+
J 106
2302+
K 107
2303+
L 108
2304+
M 109
2305+
N 110
2306+
O 111
2307+
P 112
2308+
Q 113
2309+
R 114
2310+
S 115
2311+
T 116
2312+
U 117
2313+
V 118
2314+
W 119
2315+
X 120
2316+
Y 121
2317+
Z 122
23092318
DELETE 127
23102319
SCANCODE_MASK 1073741824
23112320
CAPSLOCK 1073741881
@@ -2799,6 +2808,48 @@ def __repr__(self) -> str:
27992808
return f"tcod.event.{self.__class__.__name__}.{self.name}"
28002809

28012810

2811+
if sys.version_info >= (3, 13):
2812+
# Alias for lower case letters removed from SDL3
2813+
KeySym.A._add_alias_("a")
2814+
KeySym.B._add_alias_("b")
2815+
KeySym.C._add_alias_("c")
2816+
KeySym.D._add_alias_("d")
2817+
KeySym.E._add_alias_("e")
2818+
KeySym.F._add_alias_("f")
2819+
KeySym.G._add_alias_("g")
2820+
KeySym.H._add_alias_("h")
2821+
KeySym.I._add_alias_("i")
2822+
KeySym.J._add_alias_("j")
2823+
KeySym.K._add_alias_("k")
2824+
KeySym.L._add_alias_("l")
2825+
KeySym.M._add_alias_("m")
2826+
KeySym.N._add_alias_("n")
2827+
KeySym.O._add_alias_("o")
2828+
KeySym.P._add_alias_("p")
2829+
KeySym.Q._add_alias_("q")
2830+
KeySym.R._add_alias_("r")
2831+
KeySym.S._add_alias_("s")
2832+
KeySym.T._add_alias_("t")
2833+
KeySym.U._add_alias_("u")
2834+
KeySym.V._add_alias_("v")
2835+
KeySym.W._add_alias_("w")
2836+
KeySym.X._add_alias_("x")
2837+
KeySym.Y._add_alias_("y")
2838+
KeySym.Z._add_alias_("z")
2839+
2840+
# Alias for numbers, since Python enum names can not be number literals
2841+
KeySym.N0._add_alias_("0")
2842+
KeySym.N1._add_alias_("1")
2843+
KeySym.N2._add_alias_("2")
2844+
KeySym.N3._add_alias_("3")
2845+
KeySym.N4._add_alias_("4")
2846+
KeySym.N5._add_alias_("5")
2847+
KeySym.N6._add_alias_("6")
2848+
KeySym.N7._add_alias_("7")
2849+
KeySym.N8._add_alias_("8")
2850+
KeySym.N9._add_alias_("9")
2851+
2852+
28022853
def __getattr__(name: str) -> int:
28032854
"""Migrate deprecated access of event constants."""
28042855
if name.startswith("BUTTON_"):
@@ -2822,6 +2873,9 @@ def __getattr__(name: str) -> int:
28222873
)
28232874
return replacement
28242875

2876+
if name.startswith("K_") and len(name) == 3: # noqa: PLR2004
2877+
name = name.upper() # Silently fix single letter key symbols removed from SDL3, these are still deprecated
2878+
28252879
value: int | None = getattr(tcod.event_constants, name, None)
28262880
if not value:
28272881
msg = f"module {__name__!r} has no attribute {name!r}"

0 commit comments

Comments
 (0)