Skip to content

Commit e35a807

Browse files
author
david allen
committed
Refactor keyboar handling to be event-based.
1 parent 5281470 commit e35a807

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

HelloWorld_pygame.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Módulos | imports
22
import sys
3-
import keyboard
43
import pygame
54
from pygame.locals import *
65
from sympy import Point, pi, sin, cos
@@ -128,15 +127,20 @@ def main():
128127
# ----------
129128
for evento in pygame.event.get():
130129

131-
# window quit event
132-
if evento.type == QUIT:
133-
sys.exit(0)
134-
pass
135-
136130
# keyboard input
131+
# ----------
137132
if evento.type == pygame.KEYDOWN or evento.type == pygame.KEYUP:
138-
print(f'Key event captures for key = "{evento.key}"')
139-
133+
print(f'Key event captures for key = "{pygame.key.name(evento.key)}"')
134+
135+
# handle special keyboard inputs
136+
# ----------
137+
# quit exit on "Esc" pressed
138+
if evento.key == pygame.K_ESCAPE:
139+
print("\nyou pressed ESCAPE, so exiting...\n")
140+
sys.exit(0)
141+
pass
142+
# ----------
143+
# handle modifier key presses
140144
if evento.mod == pygame.KMOD_NONE:
141145
print('No modifier keys were in a pressed state when this '
142146
'event occurred.')
@@ -153,20 +157,15 @@ def main():
153157
pass
154158
pass
155159
pass
156-
pass
157-
pass
158160

159-
# handle special keyboard inputs
160-
# ----------
161-
try:
162-
# quit exit on "Esc" pressed
163-
if keyboard.is_pressed('Esc'):
164-
print("\nyou pressed Esc, so exiting...\n")
161+
# window quit event
162+
# ----------
163+
if evento.type == QUIT:
165164
sys.exit(0)
166165
pass
166+
167167
pass
168-
except:
169-
break
168+
pass
170169

171170
return 0
172171
pass

0 commit comments

Comments
 (0)