pygame.key 键值说明

pygame.key

pygame.key
pygame module to work with the keyboard
pygame.key.get_focusedtrue if the display is receiving keyboard input from the system
pygame.key.get_pressedget the state of all keyboard buttons
pygame.key.get_modsdetermine which modifier keys are being held
pygame.key.set_modstemporarily set which modifier keys are pressed
pygame.key.set_repeatcontrol how held keys are repeated
pygame.key.get_repeatsee how held keys are repeated
pygame.key.nameget the name of a key identifier

This module contains functions for dealing with the keyboard.

The event queue gets pygame.KEYDOWN and pygame.KEYUP events when the keyboard buttons are pressed and released. Both events have a key attribute that is a integer id representing every key on the keyboard.

The pygame.KEYDOWN event has an additional attributes unicode, and scancode. unicode represents a single character string that is the fully translated character entered. This takes into account the shift and composition keys. scancode represents the platform specific key code. This could be different from keyboard to keyboard, but is useful for key selection of weird keys like the multimedia keys.

There are many keyboard constants, they are used to represent keys on the keyboard. The following is a list of all keyboard constants

KeyASCII      ASCII   Common Name
K_BACKSPACE   \b      backspace
K_TAB         \t      tab
K_CLEAR               clear
K_RETURN      \r      return
K_PAUSE               pause
K_ESCAPE      ^[      escape
K_SPACE               space
K_EXCLAIM     !       exclaim
K_QUOTEDBL    "       quotedbl
K_HASH        #       hash
K_DOLLAR      $       dollar
K_AMPERSAND   &       ampersand
K_QUOTE               quote
K_LEFTPAREN   (       left parenthesis
K_RIGHTPAREN  )       right parenthesis
K_ASTERISK    *       asterisk
K_PLUS        +       plus sign
K_COMMA       ,       comma
K_MINUS       -       minus sign
K_PERIOD      .       period
K_SLASH       /       forward slash
K_0           0       0
K_1           1       1
K_2           2       2
K_3           3       3
K_4           4       4
K_5           5       5
K_6           6       6
K_7           7       7
K_8           8       8
K_9           9       9
K_COLON       :       colon
K_SEMICOLON   ;       semicolon
K_LESS        <       less-than sign
K_EQUALS      =       equals sign
K_GREATER     >       greater-than sign
K_QUESTION    ?       question mark
K_AT          @       at
K_LEFTBRACKET [       left bracket
K_BACKSLASH   \       backslash
K_RIGHTBRACKET ]      right bracket
K_CARET       ^       caret
K_UNDERSCORE  _       underscore
K_BACKQUOTE   `       grave
K_a           a       a
K_b           b       b
K_c           c       c
K_d           d       d
K_e           e       e
K_f           f       f
K_g           g       g
K_h           h       h
K_i           i       i
K_j           j       j
K_k           k       k
K_l           l       l
K_m           m       m
K_n           n       n
K_o           o       o
K_p           p       p
K_q           q       q
K_r           r       r
K_s           s       s
K_t           t       t
K_u           u       u
K_v           v       v
K_w           w       w
K_x           x       x
K_y           y       y
K_z           z       z
K_DELETE              delete
K_KP0                 keypad 0
K_KP1                 keypad 1
K_KP2                 keypad 2
K_KP3                 keypad 3
K_KP4                 keypad 4
K_KP5                 keypad 5
K_KP6                 keypad 6
K_KP7                 keypad 7
K_KP8                 keypad 8
K_KP9                 keypad 9
K_KP_PERIOD   .       keypad period
K_KP_DIVIDE   /       keypad divide
K_KP_MULTIPLY *       keypad multiply
K_KP_MINUS    -       keypad minus
K_KP_PLUS     +       keypad plus
K_KP_ENTER    \r      keypad enter
K_KP_EQUALS   =       keypad equals
K_UP                  up arrow
K_DOWN                down arrow
K_RIGHT               right arrow
K_LEFT                left arrow
K_INSERT              insert
K_HOME                home
K_END                 end
K_PAGEUP              page up
K_PAGEDOWN            page down
K_F1                  F1
K_F2                  F2
K_F3                  F3
K_F4                  F4
K_F5                  F5
K_F6                  F6
K_F7                  F7
K_F8                  F8
K_F9                  F9
K_F10                 F10
K_F11                 F11
K_F12                 F12
K_F13                 F13
K_F14                 F14
K_F15                 F15
K_NUMLOCK             numlock
K_CAPSLOCK            capslock
K_SCROLLOCK           scrollock
K_RSHIFT              right shift
K_LSHIFT              left shift
K_RCTRL               right ctrl
K_LCTRL               left ctrl
K_RALT                right alt
K_LALT                left alt
K_RMETA               right meta
K_LMETA               left meta
K_LSUPER              left windows key
K_RSUPER              right windows key
K_MODE                mode shift
K_HELP                help
K_PRINT               print screen
K_SYSREQ              sysrq
K_BREAK               break
K_MENU                menu
K_POWER               power
K_EURO                euro

The keyboard also has a list of modifier states that can be assembled bit bitwise ORing them together.

KMOD_NONE, KMOD_LSHIFT, KMOD_RSHIFT, KMOD_SHIFT, KMOD_CAPS,
KMOD_LCTRL, KMOD_RCTRL, KMOD_CTRL, KMOD_LALT, KMOD_RALT,
KMOD_ALT, KMOD_LMETA, KMOD_RMETA, KMOD_META, KMOD_NUM, KMOD_MODE
pygame.key. get_focused ( ) �0�9
true if the display is receiving keyboard input from the system
get_focused() -> bool

This is true when the display window has keyboard focus from the system. If the display needs to ensure it does not lose keyboard focus, it can use pygame.event.set_grab() to grab all input.

pygame.key. get_pressed ( ) �0�9
get the state of all keyboard buttons
get_pressed() -> bools

Returns a sequence of boolean values representing the state of every key on the keyboard. Use the key constant values to index the array. A True value means the that button is pressed.

Getting the list of pushed buttons with this function is not the proper way to handle text entry from the user. You have no way to know the order of keys pressed, and rapidly pushed keys can be completely unnoticed between two calls to pygame.key.get_pressed(). There is also no way to translate these pushed keys into a fully translated character value. See the pygame.KEYDOWN events on the event queue for this functionality.

pygame.key. get_mods ( ) �0�9
determine which modifier keys are being held
get_mods() -> int

Returns a single integer representing a bitmask of all the modifier keys being held. Using bitwise operators you can test if specific shift keys are pressed, the state of the capslock button, and more.

pygame.key. set_mods ( ) �0�9
temporarily set which modifier keys are pressed
set_mods(int) -> None

Create a bitmask of the modifier constants you want to impose on your program.

pygame.key. set_repeat ( ) �0�9
control how held keys are repeated
set_repeat() -> None
set_repeat(delay, interval) -> None

When the keyboard repeat is enabled, keys that are held down will generate multiple pygame.KEYDOWN events. The delay is the number of milliseconds before the first repeated pygame.KEYDOWN will be sent. After that another pygame.KEYDOWN will be sent every interval milliseconds. If no arguments are passed the key repeat is disabled.

When pygame is initialized the key repeat is disabled.

pygame.key. get_repeat ( ) �0�9
see how held keys are repeated
get_repeat() -> (delay, interval)

When the keyboard repeat is enabled, keys that are held down will generate multiple pygame.KEYDOWN events. The delay is the number of milliseconds before the first repeated pygame.KEYDOWN will be sent. After that another pygame.KEYDOWN will be sent every interval milliseconds.

When pygame is initialized the key repeat is disabled.

New in pygame 1.8.

pygame.key. name ( ) �0�9
get the name of a key identifier
name(key) -> string

Get the descriptive name of the button from a keyboard button id constant.

  • 8
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
pygame.key模块是与键盘相关的Pygame模块,提供了一些函数来处理键盘输入。以下是一些常用的函数和它们的功能[^2]: 1. pygame.key.get_focused():当窗口获得键盘的输入焦点时返回True。 2. pygame.key.get_pressed():获取键盘上所有按键的状态,返回一个包含按键状态的元组。 3. pygame.key.get_mods():检测是否有组合键被按下,返回一个表示按下的组合键的位掩码。 4. pygame.key.set_mods():临时设置某些组合键为被按下状态。 5. pygame.key.set_repeat():控制重复响应持续按下按键的时间。 6. pygame.key.get_repeat():获取重复响应按键的参数。 7. pygame.key.name():获取按键标识符对应的名字。 下面是一个示例代码,演示了如何使用pygame.key模块来获取键盘输入的状态和按键名称: ```python import pygame from pygame.locals import * pygame.init() # 创建一个窗口 window = pygame.display.set_mode((200, 200)) pygame.display.set_caption("Keyboard Input") running = True while running: for event in pygame.event.get(): if event.type == QUIT: running = False # 获取键盘上所有按键的状态 keys = pygame.key.get_pressed() # 检测是否有组合键被按下 mods = pygame.key.get_mods() # 获取重复响应按键的参数 repeat = pygame.key.get_repeat() # 打印按键状态和按键名称 for key in range(len(keys)): if keys[key]: print("Key pressed:", pygame.key.name(key)) # 打印组合键状态 if mods & KMOD_SHIFT: print("Shift key pressed") if mods & KMOD_CTRL: print("Ctrl key pressed") if mods & KMOD_ALT: print("Alt key pressed") # 打印重复响应按键的参数 print("Repeat delay:", repeat[0]) print("Repeat interval:", repeat[1]) pygame.quit() ``` 这段代码创建了一个窗口,并在窗口中打印键盘输入的状态和按键名称。同时还打印了组合键状态和重复响应按键的参数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值