このモジュールにはキーボードを操作する機能があります。
キーボードのボタンを押して離すと、イベントキューにpygame.KEYDOWNイベントとpygame.KEYUPイベントが取得されます。取得された各イベントは、キーボードの各キーを表す数値情報を持っています。
pygame.KEYDOWNイベントはさらにユニコードとスキャンコードに関する情報を持っています。ユニコード情報は入力されたキーを文字に変換した値を表します。これはshiftキーと別のキーを同時押しするような場合も考慮されてます。スキャンコード情報はユーザーが使用するキーボード環境固有のキーコードを表します。取得されるスキャンコードは使用されるキーボードによって変わることがありますが、マルチメディアキーボードのように普通とは違った配列のキー環境で入力キーを特定する場合に便利です。
Pygameには入力キーを識別するための多くのキーボード定数があり、 それらを使用してキーボードのどのキーが押されたかを特定します。下記がキーボード定数の一覧です。
KeyASCII ASCII Common Name
K_BACKSPACE backspace
K_TAB tab
K_CLEAR clear
K_RETURN
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
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
またPygameには修飾キーの入力状態を表す値もあり、それを下記の修飾キー定数と論理和で組み合わせることで入力状態を確認できます。
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
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 backspace
K_TAB tab
K_CLEAR clear
K_RETURN
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
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
ウィンドウがキーボードからの入力情報を受け取れる状態であればtrueが戻り値として返されます。ウィンドウが常にキーボード入力情報を受け取れる状態にするためには、pygame.event.set_grab命令を使用してコンピューターの全ての入力処理を独占的に確保する必要があります。
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 - control the sharing of input devices with other applications to grab all input.
全てのキーの入力状態を表すboolean型の配列を戻り値として返します。キーボード定数を配列の添え字として使用してください。指定した位置の値がTrueだった場合は、そのキーボード定数に該当するキーが押されたことになります。
この命令を使うと入力されたキー一覧を取得することができますが、ユーザーのテキスト入力を処理する場合にこの命令を使うのは止めたほうがいいでしょう。この命令ではキーの押された順番まではわかりませんし、pygame.key.get_pressed命令が実行されるまでの間隔に高速でキーを入力されてしまうと入力情報を読み取ることができないのです。 この命令で取得できる入力キー情報を使ってユーザーからの文章入力処理を実装することはできないでしょう。もし実装したいのであれば、イベントキューに送られるpygame.KEYDOWNイベントを参照してください。
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 - get the state of all keyboard buttons. 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.
入力されている全ての修飾キーの状態をビットマスクで表した値を取得します。得られたビットマスクをビット演算子で処理することで、shiftキーやcapslockキー、それ以外のどのキーが押されたかを調べることができます。
使用例 #右のshiftキーが押された場合 if mods & KMOD_LSHIFT: #shiftーが押された場合 if mods & KMOD_SHIFT: #shiftーとctrlキーが押された場合 if mods & KMOD_SHIFT and mods & KMOD_CTRL:
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.
特定の修飾キーが押された状態のビットマスクをプログラムに設定します。
Create a bitmask of the modifier constants you want to impose on your program.
キーリピートが有効になっている場合、キーが押しっぱなしの状態だと何度も pygame.KEYDOWNイベントが発生します。 delay引数は押しっぱなし状態でpygame.KEYDOWNが発生する間隔をミリ秒単位で表した数値です。 After that another pygame.KEYDOWN will be sent every interval milliseconds.引数を設定しなかった場合、キーリピート設定は無効になります。
pygameが初期化される際、キーリピート機能は無効に設定されます。
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.KEYDOWNイベントが発生します。delay引数は押しっぱなし状態でpygame.KEYDOWNが発生する間隔をミリ秒単位で表した数値です。 After that another pygame.KEYDOWN will be sent every interval milliseconds.
pygameが初期化される際、キーリピート機能は無効に設定されます。
この命令はpygameのバージョン1.8で新たに実装されました。
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.
引数として渡したキーボード定数から、キーを特定するための名前を取得します。
Get the descriptive name of the button from a keyboard button id constant.