Skip to content

Commit 14a80d4

Browse files
committed
Update keyboard examples to use enums.
1 parent 849ac8f commit 14a80d4

File tree

2 files changed

+77
-77
lines changed

2 files changed

+77
-77
lines changed

examples/samples_tcod.py

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -70,25 +70,25 @@ def on_draw(self) -> None:
7070

7171
def ev_keydown(self, event: tcod.event.KeyDown) -> None:
7272
global cur_sample
73-
if event.sym == tcod.event.K_DOWN:
73+
if event.sym == tcod.event.KeySym.DOWN:
7474
cur_sample = (cur_sample + 1) % len(SAMPLES)
7575
SAMPLES[cur_sample].on_enter()
7676
draw_samples_menu()
77-
elif event.sym == tcod.event.K_UP:
77+
elif event.sym == tcod.event.KeySym.UP:
7878
cur_sample = (cur_sample - 1) % len(SAMPLES)
7979
SAMPLES[cur_sample].on_enter()
8080
draw_samples_menu()
81-
elif event.sym == tcod.event.K_RETURN and event.mod & tcod.event.KMOD_LALT:
81+
elif event.sym == tcod.event.KeySym.RETURN and event.mod & tcod.event.KMOD_LALT:
8282
tcod.console_set_fullscreen(not tcod.console_is_fullscreen())
83-
elif event.sym == tcod.event.K_PRINTSCREEN or event.sym == ord("p"):
83+
elif event.sym == tcod.event.KeySym.PRINTSCREEN or event.sym == tcod.event.KeySym.p:
8484
print("screenshot")
8585
if event.mod & tcod.event.KMOD_LALT:
8686
tcod.console_save_apf(root_console, "samples.apf")
8787
print("apf")
8888
else:
8989
tcod.sys_save_screenshot()
9090
print("png")
91-
elif event.sym == tcod.event.K_ESCAPE:
91+
elif event.sym == tcod.event.KeySym.ESCAPE:
9292
raise SystemExit()
9393
elif event.sym in RENDERER_KEYS:
9494
# Swap the active context for one with a different renderer.
@@ -259,7 +259,7 @@ def __init__(self) -> None:
259259
self.bk.ch[:] = ord(" ")
260260

261261
def ev_keydown(self, event: tcod.event.KeyDown) -> None:
262-
if event.sym in (tcod.event.K_RETURN, tcod.event.K_KP_ENTER):
262+
if event.sym in (tcod.event.KeySym.RETURN, tcod.event.KeySym.KP_ENTER):
263263
self.bk_flag += 1
264264
if (self.bk_flag & 0xFF) > tcod.BKGND_ALPH:
265265
self.bk_flag = tcod.BKGND_NONE
@@ -449,30 +449,30 @@ def on_draw(self) -> None:
449449
)
450450

451451
def ev_keydown(self, event: tcod.event.KeyDown) -> None:
452-
if ord("9") >= event.sym >= ord("1"):
453-
self.func = event.sym - ord("1")
452+
if tcod.event.KeySym.N9 >= event.sym >= tcod.event.KeySym.N1:
453+
self.func = event.sym - tcod.event.KeySym.N1
454454
self.noise = self.get_noise()
455-
elif event.sym == ord("e"):
455+
elif event.sym == tcod.event.KeySym.e:
456456
self.hurst += 0.1
457457
self.noise = self.get_noise()
458-
elif event.sym == ord("d"):
458+
elif event.sym == tcod.event.KeySym.d:
459459
self.hurst -= 0.1
460460
self.noise = self.get_noise()
461-
elif event.sym == ord("r"):
461+
elif event.sym == tcod.event.KeySym.r:
462462
self.lacunarity += 0.5
463463
self.noise = self.get_noise()
464-
elif event.sym == ord("f"):
464+
elif event.sym == tcod.event.KeySym.f:
465465
self.lacunarity -= 0.5
466466
self.noise = self.get_noise()
467-
elif event.sym == ord("t"):
467+
elif event.sym == tcod.event.KeySym.t:
468468
self.octaves += 0.5
469469
self.noise.octaves = self.octaves
470-
elif event.sym == ord("g"):
470+
elif event.sym == tcod.event.KeySym.g:
471471
self.octaves -= 0.5
472472
self.noise.octaves = self.octaves
473-
elif event.sym == ord("y"):
473+
elif event.sym == tcod.event.KeySym.y:
474474
self.zoom += 0.2
475-
elif event.sym == ord("h"):
475+
elif event.sym == tcod.event.KeySym.h:
476476
self.zoom -= 0.2
477477
else:
478478
super().ev_keydown(event)
@@ -631,25 +631,25 @@ def on_draw(self) -> None:
631631

632632
def ev_keydown(self, event: tcod.event.KeyDown) -> None:
633633
MOVE_KEYS = {
634-
ord("i"): (0, -1),
635-
ord("j"): (-1, 0),
636-
ord("k"): (0, 1),
637-
ord("l"): (1, 0),
634+
tcod.event.KeySym.i: (0, -1),
635+
tcod.event.KeySym.j: (-1, 0),
636+
tcod.event.KeySym.k: (0, 1),
637+
tcod.event.KeySym.l: (1, 0),
638638
}
639639
FOV_SELECT_KEYS = {
640-
ord("-"): -1,
641-
ord("="): 1,
642-
tcod.event.K_KP_MINUS: -1,
643-
tcod.event.K_KP_PLUS: 1,
640+
tcod.event.KeySym.MINUS: -1,
641+
tcod.event.KeySym.EQUALS: 1,
642+
tcod.event.KeySym.KP_MINUS: -1,
643+
tcod.event.KeySym.KP_PLUS: 1,
644644
}
645645
if event.sym in MOVE_KEYS:
646646
x, y = MOVE_KEYS[event.sym]
647647
if self.walkable[self.player_x + x, self.player_y + y]:
648648
self.player_x += x
649649
self.player_y += y
650-
elif event.sym == ord("t"):
650+
elif event.sym == tcod.event.KeySym.t:
651651
self.torch = not self.torch
652-
elif event.sym == ord("w"):
652+
elif event.sym == tcod.event.KeySym.w:
653653
self.light_walls = not self.light_walls
654654
elif event.sym in FOV_SELECT_KEYS:
655655
self.algo_num += FOV_SELECT_KEYS[event.sym]
@@ -775,39 +775,39 @@ def on_draw(self) -> None:
775775
self.recalculate = True
776776

777777
def ev_keydown(self, event: tcod.event.KeyDown) -> None:
778-
if event.sym == ord("i") and self.dy > 0:
778+
if event.sym == tcod.event.KeySym.i and self.dy > 0:
779779
# destination move north
780780
tcod.console_put_char(sample_console, self.dx, self.dy, self.oldchar, tcod.BKGND_NONE)
781781
self.dy -= 1
782782
self.oldchar = sample_console.ch[self.dx, self.dy]
783783
tcod.console_put_char(sample_console, self.dx, self.dy, "+", tcod.BKGND_NONE)
784784
if SAMPLE_MAP[self.dx, self.dy] == " ":
785785
self.recalculate = True
786-
elif event.sym == ord("k") and self.dy < SAMPLE_SCREEN_HEIGHT - 1:
786+
elif event.sym == tcod.event.KeySym.k and self.dy < SAMPLE_SCREEN_HEIGHT - 1:
787787
# destination move south
788788
tcod.console_put_char(sample_console, self.dx, self.dy, self.oldchar, tcod.BKGND_NONE)
789789
self.dy += 1
790790
self.oldchar = sample_console.ch[self.dx, self.dy]
791791
tcod.console_put_char(sample_console, self.dx, self.dy, "+", tcod.BKGND_NONE)
792792
if SAMPLE_MAP[self.dx, self.dy] == " ":
793793
self.recalculate = True
794-
elif event.sym == ord("j") and self.dx > 0:
794+
elif event.sym == tcod.event.KeySym.j and self.dx > 0:
795795
# destination move west
796796
tcod.console_put_char(sample_console, self.dx, self.dy, self.oldchar, tcod.BKGND_NONE)
797797
self.dx -= 1
798798
self.oldchar = sample_console.ch[self.dx, self.dy]
799799
tcod.console_put_char(sample_console, self.dx, self.dy, "+", tcod.BKGND_NONE)
800800
if SAMPLE_MAP[self.dx, self.dy] == " ":
801801
self.recalculate = True
802-
elif event.sym == ord("l") and self.dx < SAMPLE_SCREEN_WIDTH - 1:
802+
elif event.sym == tcod.event.KeySym.l and self.dx < SAMPLE_SCREEN_WIDTH - 1:
803803
# destination move east
804804
tcod.console_put_char(sample_console, self.dx, self.dy, self.oldchar, tcod.BKGND_NONE)
805805
self.dx += 1
806806
self.oldchar = sample_console.ch[self.dx, self.dy]
807807
tcod.console_put_char(sample_console, self.dx, self.dy, "+", tcod.BKGND_NONE)
808808
if SAMPLE_MAP[self.dx, self.dy] == " ":
809809
self.recalculate = True
810-
elif event.sym == tcod.event.K_TAB:
810+
elif event.sym == tcod.event.KeySym.TAB:
811811
self.using_astar = not self.using_astar
812812
if self.using_astar:
813813
tcod.console_print(sample_console, 1, 4, "Using : A* ")
@@ -999,28 +999,28 @@ def on_draw(self) -> None:
999999

10001000
def ev_keydown(self, event: tcod.event.KeyDown) -> None:
10011001
global bsp_random_room, bsp_room_walls, bsp_depth, bsp_min_room_size
1002-
if event.sym in (tcod.event.K_RETURN, tcod.event.K_KP_ENTER):
1002+
if event.sym in (tcod.event.KeySym.RETURN, tcod.event.KeySym.KP_ENTER):
10031003
self.bsp_generate()
1004-
elif event.sym == ord(" "):
1004+
elif event.sym == tcod.event.KeySym.SPACE:
10051005
self.bsp_refresh()
1006-
elif event.sym in (tcod.event.K_EQUALS, tcod.event.K_KP_PLUS):
1006+
elif event.sym in (tcod.event.KeySym.EQUALS, tcod.event.KeySym.KP_PLUS):
10071007
bsp_depth += 1
10081008
self.bsp_generate()
1009-
elif event.sym in (tcod.event.K_MINUS, tcod.event.K_KP_MINUS):
1009+
elif event.sym in (tcod.event.KeySym.MINUS, tcod.event.KeySym.KP_MINUS):
10101010
bsp_depth = max(1, bsp_depth - 1)
10111011
self.bsp_generate()
1012-
elif event.sym in (tcod.event.K_8, tcod.event.K_KP_MULTIPLY):
1012+
elif event.sym in (tcod.event.KeySym.N8, tcod.event.KeySym.KP_MULTIPLY):
10131013
bsp_min_room_size += 1
10141014
self.bsp_generate()
1015-
elif event.sym in (tcod.event.K_SLASH, tcod.event.K_KP_DIVIDE):
1015+
elif event.sym in (tcod.event.KeySym.SLASH, tcod.event.KeySym.KP_DIVIDE):
10161016
bsp_min_room_size = max(2, bsp_min_room_size - 1)
10171017
self.bsp_generate()
1018-
elif event.sym in (tcod.event.K_1, tcod.event.K_KP_1):
1018+
elif event.sym in (tcod.event.KeySym.N1, tcod.event.KeySym.KP_1):
10191019
bsp_random_room = not bsp_random_room
10201020
if not bsp_random_room:
10211021
bsp_room_walls = True
10221022
self.bsp_refresh()
1023-
elif event.sym in (tcod.event.K_2, tcod.event.K_KP_2):
1023+
elif event.sym in (tcod.event.KeySym.N2, tcod.event.KeySym.KP_2):
10241024
bsp_room_walls = not bsp_room_walls
10251025
self.bsp_refresh()
10261026
else:
@@ -1126,9 +1126,9 @@ def on_draw(self) -> None:
11261126
)
11271127

11281128
def ev_keydown(self, event: tcod.event.KeyDown) -> None:
1129-
if event.sym == ord("1"):
1129+
if event.sym == tcod.event.KeySym.N1:
11301130
tcod.mouse_show_cursor(False)
1131-
elif event.sym == ord("2"):
1131+
elif event.sym == tcod.event.KeySym.N2:
11321132
tcod.mouse_show_cursor(True)
11331133
else:
11341134
super().ev_keydown(event)
@@ -1177,10 +1177,10 @@ def on_draw(self) -> None:
11771177
self.names.append(tcod.namegen_generate(self.sets[self.curset]))
11781178

11791179
def ev_keydown(self, event: tcod.event.KeyDown) -> None:
1180-
if event.sym == ord("="):
1180+
if event.sym == tcod.event.KeySym.EQUALS:
11811181
self.curset += 1
11821182
self.names.append("======")
1183-
elif event.sym == ord("-"):
1183+
elif event.sym == tcod.event.KeySym.MINUS:
11841184
self.curset -= 1
11851185
self.names.append("======")
11861186
else:
@@ -1363,11 +1363,11 @@ def on_draw(self) -> None:
13631363
#############################################
13641364

13651365
RENDERER_KEYS = {
1366-
tcod.event.K_F1: tcod.RENDERER_GLSL,
1367-
tcod.event.K_F2: tcod.RENDERER_OPENGL,
1368-
tcod.event.K_F3: tcod.RENDERER_SDL,
1369-
tcod.event.K_F4: tcod.RENDERER_SDL2,
1370-
tcod.event.K_F5: tcod.RENDERER_OPENGL2,
1366+
tcod.event.KeySym.F1: tcod.RENDERER_GLSL,
1367+
tcod.event.KeySym.F2: tcod.RENDERER_OPENGL,
1368+
tcod.event.KeySym.F3: tcod.RENDERER_SDL,
1369+
tcod.event.KeySym.F4: tcod.RENDERER_SDL2,
1370+
tcod.event.KeySym.F5: tcod.RENDERER_OPENGL2,
13711371
}
13721372

13731373
RENDERER_NAMES = (

tcod/event.py

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""A light-weight implementation of event handling built on calls to SDL.
22
33
Many event constants are derived directly from SDL.
4-
For example: ``tcod.event.K_UP`` and ``tcod.event.SCANCODE_A`` refer to
4+
For example: ``tcod.event.KeySym.UP`` and ``tcod.event.Scancode.A`` refer to
55
SDL's ``SDLK_UP`` and ``SDL_SCANCODE_A`` respectfully.
66
`See this table for all of SDL's keyboard constants.
77
<https://wiki.libsdl.org/SDL_Keycode>`_
@@ -1279,35 +1279,35 @@ class EventDispatch(Generic[T]):
12791279
12801280
MOVE_KEYS = { # key_symbol: (x, y)
12811281
# Arrow keys.
1282-
tcod.event.K_LEFT: (-1, 0),
1283-
tcod.event.K_RIGHT: (1, 0),
1284-
tcod.event.K_UP: (0, -1),
1285-
tcod.event.K_DOWN: (0, 1),
1286-
tcod.event.K_HOME: (-1, -1),
1287-
tcod.event.K_END: (-1, 1),
1288-
tcod.event.K_PAGEUP: (1, -1),
1289-
tcod.event.K_PAGEDOWN: (1, 1),
1290-
tcod.event.K_PERIOD: (0, 0),
1282+
tcod.event.KeySym.LEFT: (-1, 0),
1283+
tcod.event.KeySym.RIGHT: (1, 0),
1284+
tcod.event.KeySym.UP: (0, -1),
1285+
tcod.event.KeySym.DOWN: (0, 1),
1286+
tcod.event.KeySym.HOME: (-1, -1),
1287+
tcod.event.KeySym.END: (-1, 1),
1288+
tcod.event.KeySym.PAGEUP: (1, -1),
1289+
tcod.event.KeySym.PAGEDOWN: (1, 1),
1290+
tcod.event.KeySym.PERIOD: (0, 0),
12911291
# Numpad keys.
1292-
tcod.event.K_KP_1: (-1, 1),
1293-
tcod.event.K_KP_2: (0, 1),
1294-
tcod.event.K_KP_3: (1, 1),
1295-
tcod.event.K_KP_4: (-1, 0),
1296-
tcod.event.K_KP_5: (0, 0),
1297-
tcod.event.K_KP_6: (1, 0),
1298-
tcod.event.K_KP_7: (-1, -1),
1299-
tcod.event.K_KP_8: (0, -1),
1300-
tcod.event.K_KP_9: (1, -1),
1301-
tcod.event.K_CLEAR: (0, 0), # Numpad `clear` key.
1292+
tcod.event.KeySym.KP_1: (-1, 1),
1293+
tcod.event.KeySym.KP_2: (0, 1),
1294+
tcod.event.KeySym.KP_3: (1, 1),
1295+
tcod.event.KeySym.KP_4: (-1, 0),
1296+
tcod.event.KeySym.KP_5: (0, 0),
1297+
tcod.event.KeySym.KP_6: (1, 0),
1298+
tcod.event.KeySym.KP_7: (-1, -1),
1299+
tcod.event.KeySym.KP_8: (0, -1),
1300+
tcod.event.KeySym.KP_9: (1, -1),
1301+
tcod.event.KeySym.CLEAR: (0, 0), # Numpad `clear` key.
13021302
# Vi Keys.
1303-
tcod.event.K_h: (-1, 0),
1304-
tcod.event.K_j: (0, 1),
1305-
tcod.event.K_k: (0, -1),
1306-
tcod.event.K_l: (1, 0),
1307-
tcod.event.K_y: (-1, -1),
1308-
tcod.event.K_u: (1, -1),
1309-
tcod.event.K_b: (-1, 1),
1310-
tcod.event.K_n: (1, 1),
1303+
tcod.event.KeySym.h: (-1, 0),
1304+
tcod.event.KeySym.j: (0, 1),
1305+
tcod.event.KeySym.k: (0, -1),
1306+
tcod.event.KeySym.l: (1, 0),
1307+
tcod.event.KeySym.y: (-1, -1),
1308+
tcod.event.KeySym.u: (1, -1),
1309+
tcod.event.KeySym.b: (-1, 1),
1310+
tcod.event.KeySym.n: (1, 1),
13111311
}
13121312
13131313
@@ -1333,7 +1333,7 @@ def ev_keydown(self, event: tcod.event.KeyDown) -> None:
13331333
if event.sym in MOVE_KEYS:
13341334
# Send movement keys to the cmd_move method with parameters.
13351335
self.cmd_move(*MOVE_KEYS[event.sym])
1336-
elif event.sym == tcod.event.K_ESCAPE:
1336+
elif event.sym == tcod.event.KeySym.ESCAPE:
13371337
self.cmd_escape()
13381338
13391339
def ev_mousebuttondown(self, event: tcod.event.MouseButtonDown) -> None:

0 commit comments

Comments
 (0)