Events¶
Event types reference¶
Type | Attributes | Remarks |
---|---|---|
Event.CLOSED |
In fullscreen, Alt + F4 won’t send the CLOSED event (on GNU/Linux, at least). |
|
Event.RESIZED |
width , height |
|
Event.LOST_FOCUS |
||
Event.GAINED_FOCUS |
||
Event.TEXT_ENTERED |
unicode |
The attribute lets you retrieve the character entered by the user, as a Unicode string. |
Event.KEY_PRESSED , Event.KEY_RELEASED |
code , alt , control , shift , system |
code is the code of the key that was pressed/released, the other attributes are booleans and tell you if the alt/control/shit/system modifier was pressed. |
Event.MOUSE_WHEEL_MOVED |
delta , x , y |
The attribute contains the mouse wheel move (positive if forward, negative if backward). |
Event.MOUSE_BUTTON_PRESSED , Event.MOUSE_BUTTON_RELEASED |
button , x , y |
See the Mouse class for the button codes. |
Event.MOUSE_MOVED |
x , y |
|
Event.MOUSE_ENTERED |
||
Event.MOUSE_LEFT |
||
Event.JOYSTICK_BUTTON_PRESSED , Event.JOYSTICK_BUTTON_RELEASED |
joystick_id , button |
button is a number between 0 and Joystick.BUTTON_COUNT - 1. |
Event.JOYSTICK_MOVED |
joystick_id , axis , position |
See the Joystick class for the axis codes. |
Event.JOYSTICK_CONNECTED , Event.JOYSTICK_DISCONNECTED |
joystick_id |
-
class
sfml.
Event
¶ This class behaves differently from the C++
sf::Event
class. Every Event object will always only feature the attributes that actually make sense regarding the event type. This means that there is no need for the C++ union; you just access whatever attribute you want.For example, this is the kind of code you’d write in C++:
if (event.Type == sf::Event::KeyPressed && event.Key.Code == sf::Keyboard::Escape) { // ... }
In Python, it becomes:
if event.type == sfml.Event.KEY_PRESSED and event.code == sfml.Keyboard.ESCAPE: # ...
Note
All the events have
Event
type. There are no specific subtypes likeKeyPressedEvent
orMouseEnteredEvent
. Instead, events are common Python objects in the sense that their attributes can be modified at runtime, unlike other pySFML objects. This is how their specific attributes are set.This class provides the following special methods:
str(event)
returns a description of the event with its name and its attributes.
-
NAMES
¶ A class attribute that maps event codes to a short description:
>>> sfml.Event.NAMES[sfml.Event.CLOSED] 'Closed' >>> sfml.Event.NAMES[sfml.Event.KEY_PRESSED] 'Key pressed'
If you want to print this information about a specific object, you can simply use
print
;Event.__str__()
will look up the description for you.
Event types:
-
CLOSED
¶ The window requested to be closed.
-
RESIZED
¶ The window was resized.
-
LOST_FOCUS
¶ The window lost focus.
-
GAINED_FOCUS
¶ The window gained focus.
-
TEXT_ENTERED
¶ A character was entered.
-
KEY_PRESSED
¶ A key was pressed.
-
KEY_RELEASED
¶ A key was released.
-
MOUSE_WHEEL_MOVED
¶ The mouse wheel was scrolled.
-
MOUSE_BUTTON_PRESSED
¶ A mouse button was pressed.
-
MOUSE_BUTTON_RELEASED
¶ A mouse button was released.
-
MOUSE_MOVED
¶ The mouse cursors moved.
-
MOUSE_ENTERED
¶ The mouse cursor entered the area of the window.
-
MOUSE_LEFT
¶ The mouse cursor entered the area of the window.
-
JOYSTICK_BUTTON_PRESSED
¶ A joystick button was pressed.
-
JOYSTICK_BUTTON_RELEASED
¶ A joystick button was released.
-
JOYSTICK_MOVED
¶ The joystick moved along an axis.
-
JOYSTICK_CONNECTED
¶ A joystick was connected.
-
JOYSTICK_DISCONNECTED
¶ A joystick was disconnected.
-
class
sfml.
Joystick
¶ This class gives access to the real-time state of the joysticks.
It only contains static functions, so it’s not meant to be instanciated. Instead, each joystick is identified by an index that is passed to the functions of this class. Calling the constructor will raise
NotImplementedError
.This class allows users to query the state of joysticks at any time and directly, without having to deal with a window and its events. Compared to the
Event.JOYSTICK_MOVED
,Event.JOYSTICK_BUTTON_PRESSED
andEvent.JOYSTICK_BUTTON_RELEASED
events, this class can retrieve the state of axes and buttons of joysticks at any time (you don’t need to store and update a boolean on your side in order to know if a button is pressed or released), and you always get the real state of joysticks, even if they are moved, pressed or released when your window is out of focus and no event is triggered.SFML supports:
- 8 joysticks (
COUNT
) - 32 buttons per joystick (
BUTTON_COUNT
) - 8 axes per joystick (
AXIS_COUNT
)
Unlike the keyboard or mouse, the state of joysticks is sometimes not directly available (depending on the OS), so the
update()
method must be called in order to update the current state of joysticks. When you have a window with event handling, this is done automatically, you don’t need to call anything. But if you have no window, or if you want to check joysticks state before creating one, you must callupdate()
explicitely.Usage example:
# Is joystick #0 connected? connected = sfml.Joystick.is_connected(0) # How many buttons does joystick #0 support? buttons = sfml.Joystick.get_button_count(0) # Does joystick #0 define a X axis? has_x = sfml.Joystick.has_axis(0, sfml.Joystick.X) # Is button #2 pressed on joystick #0? pressed = sfml.Joystick.is_button_pressed(0, 2) # What's the current position of the Y axis on joystick #0? position = sfml.Joystick.get_axis_position(0, sfml.Joystick.Y)
-
COUNT
¶ The maximum number of supported joysticks.
-
BUTTON_COUNT
¶ The maximum number of supported buttons.
-
AXIS_COUNT
¶ The maximum number of supported axes.
Axes codes:
-
X
¶ The x axis.
-
Y
¶ The y axis.
-
Z
¶ The z axis.
-
R
¶ The r axis.
-
U
¶ The u axis.
-
V
¶ The v axis.
-
POV_X
¶ The x axis of the point-of-view hat.
-
POV_Y
¶ The y axis of the point-of-view hat.
-
classmethod
is_connected
(int joystick)¶ Return
True
is joystick is connected, otherwiseFalse
is returned.
Return the number of buttons supported by joystick. If the joystick is not connected, return 0.
-
classmethod
has_axis
(int joystick, int axis)¶ Return whether joystick supports the given axis. If the joystick isn’t connected,
False
is returned. axis should be an axis code.
Return whether button is pressed on joystick. If the joystick isn’t connected,
False
is returned.
-
classmethod
get_axis_position
(int joystick, int axis)¶ Return the current position along axis as a float. If the joystick is not connected, 0.0 is returned. axis should be an axis code.
-
classmethod
update
()¶ Update the state of all the joysticks. You don’t need to call this method yourself in most cases. If you haven’t created any window, however, you will need to call it to update the joystick state.
- 8 joysticks (
-
class
sfml.
Keyboard
¶ This class provides an interface to the state of the keyboard. It only contains static methods (a single keyboard is assumed), so it’s not meant to be instanciated.
This class allows users to query the keyboard state at any time and directly, without having to deal with a window and its events. Compared to the
Event.KEY_PRESSED
andEvent.KEY_RELEASED
events, Keyboard can retrieve the state of a key at any time (you don’t need to store and update a boolean on your side in order to know if a key is pressed or released), and you always get the real state of the keyboard, even if keys are pressed or released when your window is out of focus and no event is triggered.Usage example:
if sfml.Keyboard.is_key_pressed(sfml.Keyboard.LEFT): pass # move left... elif sfml.Keyboard.is_key_pressed(sfml.Keyboard.RIGHT): pass # move right... elif sfml.Keyboard.is_key_pressed(sfml.Keyboard.ESCAPE): pass # quit...
Key codes:
-
A
¶
-
B
¶
-
C
¶
-
D
¶
-
E
¶
-
F
¶
-
G
¶
-
H
¶
-
I
¶
-
J
¶
-
K
¶
-
L
¶
-
M
¶
-
N
¶
-
O
¶
-
P
¶
-
Q
¶
-
R
¶
-
S
¶
-
T
¶
-
U
¶
-
V
¶
-
W
¶
-
X
¶
-
Y
¶
-
Z
¶
-
NUM0
¶ The 0 key.
-
NUM1
¶ The 1 key.
-
NUM2
¶ The 2 key.
-
NUM3
¶ The 3 key.
-
NUM4
¶ The 4 key.
-
NUM5
¶ The 5 key.
-
NUM6
¶ The 6 key.
-
NUM7
¶ The 7 key.
-
NUM8
¶ The 8 key.
-
NUM9
¶ The 9 key.
-
ESCAPE
¶
-
L_CONTROL
¶ The left control key.
-
L_SHIFT
¶ The left shift key.
-
L_ALT
¶ The left alt key.
-
L_SYSTEM
¶ The left OS-specific key, e.g. window, apple or home key.
-
R_CONTROL
¶ The right control key.
-
R_SHIFT
¶ The right shift key.
-
R_ALT
¶ The right alt key.
-
R_SYSTEM
¶ The right OS-specific key, e.g. window, apple or home key.
-
MENU
¶ The menu key.
-
L_BRACKET
¶ The
[
key.
-
R_BRACKET
¶ The
]
key.
-
SEMI_COLON
¶ The
;
key.
-
COMMA
¶ The
,
key.
-
PERIOD
¶ The
.
key.
-
QUOTE
¶ The
'
key.
-
SLASH
¶ The
/
key.
-
BACK_SLASH
¶ The
\
key.
-
TILDE
¶ The
~
key.
-
EQUAL
¶ The
=
key.
-
DASH
¶ The
-
key.
-
SPACE
¶
-
RETURN
¶
-
BACK_SPACE
¶ The back space key.
-
TAB
¶ The tabulation key.
-
PAGE_UP
¶
-
PAGE_DOWN
¶
-
END
¶
-
HOME
¶
-
INSERT
¶
-
DELETE
¶
-
ADD
¶ The
+
key.
-
SUBTRACT
¶ The
-
key.
-
MULTIPLY
¶ The
*
key.
-
DIVIDE
¶ The
/
key.
-
LEFT
¶ The left arrow.
-
RIGHT
¶ The right arrow.
-
UP
¶ The up arrow.
-
DOWN
¶ The down arrow.
-
NUMPAD0
¶ The numpad 0 key.
-
NUMPAD1
¶ The numpad 1 key.
-
NUMPAD2
¶ The numpad 2 key.
-
NUMPAD3
¶ The numpad 3 key.
-
NUMPAD4
¶ The numpad 4 key.
-
NUMPAD5
¶ The numpad 5 key.
-
NUMPAD6
¶ The numpad 6 key.
-
NUMPAD7
¶ The numpad 7 key.
-
NUMPAD8
¶ The numpad 8 key.
-
NUMPAD9
¶ The numpad 9 key.
-
F1
¶
-
F2
¶
-
F3
¶
-
F4
¶
-
F5
¶
-
F6
¶
-
F7
¶
-
F8
¶
-
F9
¶
-
F10
¶
-
F11
¶
-
F12
¶
-
F13
¶
-
F14
¶
-
F15
¶
-
PAUSE
¶
-
KEY_COUNT
¶ The total number of keyboard keys.
-
-
class
sfml.
Mouse
¶ This class gives access to the real-time state of the mouse. It only contains static functions (a single mouse is assumed), so it’s not meant to be instanciated. Calling the constructor will raise
NotImplementedError
.This class allows users to query the mouse state at any time and directly, without having to deal with a window and its events. Compared to the
Event.MOUSE_MOVED
,Event.MOUSE_BUTTON_PRESSED
andEvent.MOUSE_BUTTON_RELEASED
events, this class can retrieve the state of the cursor and the buttons at any time (you don’t need to store and update a boolean on your side in order to know if a button is pressed or released), and you always get the real state of the mouse, even if it is moved, pressed or released when your window is out of focus and no event is triggered.The
set_position()
andget_position()
methods can be used to change or retrieve the current position of the mouse pointer. There are two versions: one that operates in global coordinates (relative to the desktop) and one that operates in window coordinates (relative to a specific window).Usage example:
if sfml.Mouse.is_button_pressed(sfml.Mouse.LEFT): pass # left click... # Get global mouse position position = sfml.Mouse.get_position() # Set mouse position relative to a window sfml.Mouse.set_position((100, 200), window)
-
LEFT
¶ The left mouse button.
-
RIGHT
¶ The right mouse button.
-
MIDDLE
¶ The middle (wheel) mouse button.
-
X_BUTTON1
¶ The first extra mouse button.
-
X_BUTTON2
¶ The second extra mouse button.
-
BUTTON_COUNT
¶ The total number of mouse buttons.
Return
True
if button is pressed, otherwise returnsFalse
. button should be a mouse button code.
-
classmethod
get_position
([window])¶ Return a tuple with the current position of the cursor. With no arguments, the global position on the desktop is returned. If a window argument is provided, the position relative to the window is returned.
-
classmethod
set_position
(tuple position[, window])¶ Set the current position of the cursor. With only one argument, position is considered a as global desktop position. If a window argument is provided, the position is considered as relative to the window.
-