pygame.key
pygame中与键盘相关的模块
这个模块包含处理键盘的函数。
当键盘的按键被按下时,事件队列会得到pygame.KEYDOWN和pygame.KEYUP事件。这两个事件都有一个叫做key的整型属性表示键盘上的键。pygame.KEYDOWN事件还有一个额外的属性unicode,表示键盘输入的转后对应的字符。这个字符会考虑shift键和其它组合键的状态。
有很多键盘相关的常量,他们表示键盘上的键。下面是所有键盘常量的列表:
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
键盘还有一组修饰键的状态,可以通过位或把它们组合起来:
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
1. pygame.key.get_focused
如果display取得了系统的键盘输入焦点,返回True。
pygame.key.get_focused(): return bool
当键盘从系统获得输入焦点时,这个函数返回True。如果display要保证它不失去输入焦点,则可以使用pygame.event.set_grab函数来捕获所有的输入。
2. pygame.key.get_pressed
取得所有键盘按键的状态
pygame.key.get_pressed(): return bools
返回一组布尔值,表示键盘上每个按键的状态。使用按键常量来索引这个数组。如果值为True,表示这个键被按下了。
- 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. December 25, 2005 7:36pm - Anonymous
- pygame.event.pump() m = pygame.key.get_mods()
if m & KMOD_SHIFT:
- print 'shift pressed'
- untill I used:
This is so because the get_pressed() function wraps the SDL_GetKeyState() function and in the SDL_GetKeyState() documentation it is written that one should use SDL_PumpEvents() to update the state array and pygame.event.pump() just happens to be a wrapper for SDL_PumpEvents()
- pygame.event.pump() m = pygame.key.get_mods()
3. pygame.key.get_mods
- determine which modifier keys are being held pygame.key.get_mods(): return 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. February 23, 2006 12:14pm - Anonymous DSFDSFDKJKFJEFJDJFKLSD
4. pygame.key.set_mods
- temporarily set which modifier keys are pressed pygame.key.set_mods(int): return None Create a bitmask of the modifier constants you want to impose on your program. October 17, 2006 9:00am - Anonymous sexwiev
5. pygame.key.set_repeat
- control how held keys are repeated pygame.key.set_repeat(): return None pygame.key.set_repeat(delay, interval): return None When the keyboard repeat is enabled, keys that are held down will generate multiple pygame.KEYDOWN events. The delay is the number if 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.
6. pygame.key.name
获取一个键的名字
pygame.key.name(key): return string
从一个键的标识常量获得这个键的描述性名称。
pygame.mouse
- pygame module to work with the mouse
- pygame.mouse.get_pressed - get the state of the mouse buttons get the state of the mouse buttons pygame.mouse.get_pos - get the mouse cursor position get the mouse cursor position pygame.mouse.get_rel - get the amount of mouse movement get the amount of mouse movement pygame.mouse.set_pos - set the mouse cursor position set the mouse cursor position pygame.mouse.set_visible - hide or show the mouse cursor hide or show the mouse cursor pygame.mouse.get_focused - check if the display is receiving mouse input check if the display is receiving mouse input pygame.mouse.set_cursor - set the image for the system mouse cursor set the image for the system mouse cursor pygame.mouse.get_cursor - get the image for the system mouse cursor get the image for the system mouse cursor
1. pygame.mouse.get_pressed
- get the state of the mouse buttons pygame.moouse.get_pressed(): return (button1, button2, button3) Returns a sequence of booleans representing the state of all the mouse buttons. A true value means the mouse is currently being pressed at the time of the call. Note, to get all of the mouse events it is better to use either
- pygame.event.wait() or pygame.event.get() and check all of those events
2. pygame.mouse.get_pos
- get the mouse cursor position pygame.mouse.get_pos(): return (x, y) Returns the X and Y position of the mouse cursor. The position is relative the the top-left corner of the display. The cursor position can be located outside of the display window, but is always constrained to the screen.
3. pygame.mouse.get_rel
- get the amount of mouse movement pygame.mouse.get_rel(): return (x, y) Returns the amount of movement in X and Y since the previous call to this function. The relative movement of the mouse cursor is constrained to the edges of the screen, but see the virtual input mouse mode for a way around this. Virtual input mode is described at the top of the page.
4. pygame.mouse.set_pos
- set the mouse cursor position pygame.mouse.set_pos([x, y]): return None Set the current mouse position to arguments given. If the mouse cursor is visible it will jump to the new coordinates. Moving the mouse will generate a new pygaqme.MOUSEMOTION event.
5. pygame.mouse.set_visible
- hide or show the mouse cursor pygame.mouse.set_visible(bool): return bool If the bool argument is true, the mouse cursor will be visible. This will return the previous visible state of the cursor.
6. pygame.mouse.get_focused
- check if the display is receiving mouse input pygame.mouse.get_focused(): return bool Returns true when pygame is receiving mouse input events (or, in windowing terminology, is "active" or has the "focus"). This method is most useful when working in a window. By contrast, in full-screen mode, this method always returns true. Note: under MS Windows, the window that has the mouse focus also has the keyboard focus. But under X-Windows, one window can receive mouse events and another receive keyboard events. pygame.mouse.get_focused - check if the display is receiving mouse input indicates whether the pygame window receives mouse events.
7. pygame.mouse.set_cursor
- set the image for the system mouse cursor pygame.mouse.set_cursor(size, hotspot, xormasks, andmasks): return None When the mouse cursor is visible, it will be displayed as a black and white bitmap using the given bitmask arrays. The size is a sequence containing the cursor width and height. Hotspot is a sequence containing the cursor hotspot position. xormasks is a sequence of bytes containing the cursor xor data masks. Lastly is andmasks, a sequence of bytes containting the cursor bitmask data. Width must be a multiple of 8, and the mask arrays must be the correct size for the given width and height. Otherwise an exception is raised. See the pygame.cursor module for help creating default and custom masks for the system cursor.
8. pygame.mouse.get_cursor
- get the image for the system mouse cursor pygame.mouse.get_cursor(): return (size, hotspot, xormasks, andmasks) Get the information about the mouse system cursor. The return value is the same data as the arguments passed into pygame.mouse.set_cursor - set the image for the system mouse cursor.
pygame.cursors
- pygame module for cursor resources
- pygame.cursors.compile - create binary cursor data from simple strings create binary cursor data from simple strings pygame.cursors.load_xbm - load cursor data from an xbm file load cursor data from an xbm file
>>> pygame.mouse.set_cursor(*pygame.cursors.arrow)
>>> cursor = pygame.cursors.compile(pygame.cursors.textmarker_strings) >>> pygame.mouse.set_cursor(*cursor)
- o pygame.cursors.arrow o pygame.cursors.diamond o pygame.cursors.broken_x o pygame.cursors.tri_left o pygame.cursors.tri_right
- o pygame.cursors.thickarrow_strings o pygame.cursors.sizer_x_strings o pygame.cursors.sizer_y_strings o pygame.cursors.sizer_xy_strings
1. pygame.cursors.compile
- create binary cursor data from simple strings pygame.cursor.compile(strings, black='X', white='.', xor='o'): return data, mask A sequence of strings can be used to create binary cursor data for the system cursor. The return values are the same format needed by pygame.mouse.set_cursor - set the image for the system mouse cursor. If you are creating your own cursor strings, you can use any value represent the black and white pixels. Some system allow you to set a special toggle color for the system color, this is also called the xor color. If the system does not support xor cursors, that color will simply be black. The width of the strings must all be equal and be divisible by 8. An example set of cursor strings looks like this
- thickarrow_strings = ( #sized 24x24
- "XX ", "XXX ", "XXXX ", "XX.XX ", "XX..XX ", "XX...XX ", "XX....XX ", "XX.....XX ", "XX......XX ", "XX.......XX ", "XX........XX ", "XX........XXX ", "XX......XXXXX ", "XX.XXX..XX ", "XXXX XX..XX ", "XX XX..XX ", " XX..XX ", " XX..XX ", " XX..XX ", " XXXX ", " XX ", " ", " ", " ")
\ZnbNzgytswsc %%%%%%%%%%%%%% _
- thickarrow_strings = ( #sized 24x24
2. pygame.cursors.load_xbm
- load cursor data from an xbm file pygame.cursors.load_xbm(cursorfile, maskfile=None): return cursor_args This loads cursors for a simple subset of XBM files. XBM files are traditionally used to store cursors on unix systems, they are an ascii format used to represent simple images. Sometimes the black and white color values will be split into two separate XBM files. You can pass a second maskfile argument to load the two images into a single cursor. The cursorfile and maskfile arguments can either be filenames or filelike object with the readlines method. The return value cursor_args can be passed directly to the pygame.mouse.set_cursor - set the image for the system mouse cursor function.
pygame.joystick
pygame.time