Game Development Reference
In-Depth Information
Events and Handling the KEYDOWN Event
Table 18-1: Events, and what causes them to be generated.
Event
Description
QUIT
Generated when the user closes with window.
Generated when the user pressed down a key. Has a key
attribute that tells which key was pressed. Also has a mod
attribute that tells if the Shift, Ctrl, Alt, or other keys were held
down when this key was pressed.
KEYDOWN
Generated when the user releases a key. Has a key and mod
attribute that are similar to those for KEYDOWN .
KEYUP
Generated whenever the mouse moves over the window. Has a
pos attribute that returns tuple (x, y) for the coordinates of
where the mouse is in the window. The rel attribute also
returns a (x, y) tuple, but it gives coordinates relative since the
last MOUSEMOTION event. For example, if the mouse moves
left by four pixels from (200, 200) to (196, 200), then rel will
be (-4, 0) . The buttons attribute returns a tuple of three
integers. The first integer in the tuple is for the left mouse
button, the second integer for the middle mouse button (if there
is a middle mouse button), and the third integer is for the right
mouse button. These integers will be 0 if they are not being
pressed down when the mouse moved and 1 if they are pressed
down.
MOUSEMOTION
Generated when a mouse button is pressed down in the window.
This event has a pos attribute which is an (x, y) tuple for the
coordinates of where the mouse was when the button was
pressed. There is also a button attribute which is an integer
from 1 to 5 that tells which mouse button was pressed:
Value of button
Mouse Button
MOUSEBUTTONDOWN
1
Left button
2
Middle button
3
Right button
4
Scroll wheel moved up
5
Scroll wheel moved down
Generated when the mouse button is released. This has the same
attributes as MOUSEBUTTONDOWN
MOUSEBUTTONUP
The code to handle the key press and key release events is below. But at the start of the
program, we will set all of these variables to False .
Search WWH ::




Custom Search