10 examples of 'pygame keydown' in Python

Every line of 'pygame keydown' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
146def key_down(self, key):
147 if key in self.map:
148 self.velocity = V2((3 / self.scale, 0) if key in self.map[:2] else (0, 3 / self.scale)).elementwise() * (
149 (self.map.index(key) not in (1, 2)) * 2 - 1)
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

95def checkForKeyPress():
96 if len(pygame.event.get(QUIT)) > 0:
97 terminate()
98
99 keyUpEvents = pygame.event.get(KEYUP)
100 if len(keyUpEvents) == 0:
101 return None
102 if keyUpEvents[0].key == K_ESCAPE:
103 terminate()
104 return keyUpEvents[0].key
180def handle_input(self):
181 try:
182 event = pygame.event.wait()
183
184 if event.type == QUIT:
185 self.exit_status = 0
186 self.running = False
187
188 elif event.type == KEYDOWN:
189 if event.key == K_ESCAPE:
190 self.exit_status = 0
191 self.running = False
192 else:
193 self.running = False
194
195 elif event.type == VIDEORESIZE:
196 init_screen(event.w, event.h)
197 self.dirty = True
198
199 except KeyboardInterrupt:
200 self.exit_status = 0
201 self.running = False
7def check_keydown_events(event, ai_settings, screen, ship, bullets):
8 """Respond to keypresses."""
9 if event.key == pygame.K_RIGHT:
10 ship.moving_right = True
11 elif event.key == pygame.K_LEFT:
12 ship.moving_left = True
13 elif event.key == pygame.K_SPACE:
14 fire_bullet(ai_settings, screen, ship, bullets)
231def handle_event(self, e):
232 """ Map a pygame event to nuklear input. """
233
234 if e.type == pygame.KEYDOWN or e.type == pygame.KEYUP:
235 consumed = False
236 down = e.type == pygame.KEYDOWN
237 for nk_key in NkPygame.KEYMAP.map_key(e.key, e.mod):
238 lib.nk_input_key(self.ctx, nk_key, down)
239 consumed = True
240 if not consumed and down and len(e.unicode) == 1:
241 # Note: should pass unicode directly, but need to
242 # convert wchar_t (which is what cffi converts to)
243 # to int or char[4]. wchar_t is 2 bytes on windows
244 # for utf-16
245 if unicodedata.category(e.unicode)[0] != "C":
246 char = str(e.unicode)
247 if len(char) == 1:
248 lib.nk_input_char(self.ctx, str(e.unicode))
249 elif e.type == pygame.MOUSEBUTTONDOWN or e.type == pygame.MOUSEBUTTONUP:
250 down = e.type == pygame.MOUSEBUTTONDOWN
251 button = lib.NK_BUTTON_LEFT
252 if e.button == 1:
253 button = lib.NK_BUTTON_LEFT
254 elif e.button == 3:
255 button = lib.NK_BUTTON_RIGHT
256 lib.nk_input_button(self.ctx, button, e.pos[0], e.pos[1], down)
257 elif e.type == pygame.MOUSEMOTION:
258 lib.nk_input_motion(self.ctx, e.pos[0], e.pos[1])
116def on_keydown(self, keyname):
117 self.marker('Response/Button Press/Keyboard/%s' % keyname)
118 if self.callbacks_connected:
119 self.keydown_mastercallback(keyname)
4def quit_or_esc() -> bool:
5 """
6 Check if the quit event is triggered or the ESC key is pressed.
7 """
8 for event in pygame.event.get():
9 if event.type == pygame.QUIT or \
10 (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
11 return True
12 return False
158def keyPressed(self, key, char):
159 if self.active:
160 self.active = self.enemyStats.keyPressed(key)
161 else:
162 self.menu.keyPressed(key)
163
164 #close the menu scene
165 if key == Input.BButton:
166 self.engine.viewport.changeScene("MenuSystem")
426def handle_key(self, k):
427 level_state = self.game_state.level
428 if k == 'CANCEL':
429 return
430
431 if k not in KEYS_TO_DIRECTIONS:
432 self.logger.log("Invalid direction")
433 return
434
435 delta = KEYS_TO_DIRECTIONS[k]
436 if action_close(level_state, level_state.player, level_state.player.position + delta):
437 self.logger.log("You closed the door.")
438 else:
439 self.logger.log("There is no door there.")
589def OnKeyDown(self, event):
590 code = event.GetKeyCode()
591 if self.eskip:
592 self.eskip = False
593 else:
594 self.eskip = True
595 handler = self.bindings.get(code)
596 if handler is not None:
597 handler()
598 elif self.event_cb is not None:
599 pars = {'type': EEventKeyDown, 'keycode': code, 'scancode': code, 'modifiers': 0}
600 self.event_cb(pars)
601 pars = {'type': EEventKey, 'keycode': code, 'scancode': code, 'modifiers': 0}
602 self.event_cb(pars)
603 event.Skip()

Related snippets