SFML 3.0.1
Loading...
Searching...
No Matches
Event.hpp
Go to the documentation of this file.
1
2//
3// SFML - Simple and Fast Multimedia Library
4// Copyright (C) 2007-2025 Laurent Gomila (laurent@sfml-dev.org)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25#pragma once
26
28// Headers
30#include <SFML/Window/Joystick.hpp>
31#include <SFML/Window/Keyboard.hpp>
32#include <SFML/Window/Mouse.hpp>
33#include <SFML/Window/Sensor.hpp>
34
35#include <SFML/System/Vector2.hpp>
36
37#include <type_traits>
38#include <variant>
39
40
41 namespace sf
42{
47 class Event
48{
49 public:
54 struct Closed
55 {
56 };
57
62 struct Resized
63 {
64 Vector2u size;
65 };
66
71 struct FocusLost
72 {
73 };
74
79 struct FocusGained
80 {
81 };
82
87 struct TextEntered
88 {
89 char32_t unicode{};
90 };
91
96 struct KeyPressed
97 {
98 Keyboard::Key code{};
99 Keyboard::Scancode scancode{};
100 bool alt{};
101 bool control{};
102 bool shift{};
103 bool system{};
104 };
105
110 struct KeyReleased
111 {
112 Keyboard::Key code{};
113 Keyboard::Scancode scancode{};
114 bool alt{};
115 bool control{};
116 bool shift{};
117 bool system{};
118 };
119
124 struct MouseWheelScrolled
125 {
126 Mouse::Wheel wheel{};
127 float delta{};
128 Vector2i position;
129 };
130
135 struct MouseButtonPressed
136 {
137 Mouse::Button button{};
138 Vector2i position;
139 };
140
145 struct MouseButtonReleased
146 {
147 Mouse::Button button{};
148 Vector2i position;
149 };
150
155 struct MouseMoved
156 {
157 Vector2i position;
158 };
159
188 struct MouseMovedRaw
189 {
190 Vector2i delta;
191 };
192
197 struct MouseEntered
198 {
199 };
200
205 struct MouseLeft
206 {
207 };
208
213 struct JoystickButtonPressed
214 {
215 unsigned int joystickId{};
216 unsigned int button{};
217 };
218
223 struct JoystickButtonReleased
224 {
225 unsigned int joystickId{};
226 unsigned int button{};
227 };
228
233 struct JoystickMoved
234 {
235 unsigned int joystickId{};
236 Joystick::Axis axis{};
237 float position{};
238 };
239
244 struct JoystickConnected
245 {
246 unsigned int joystickId{};
247 };
248
253 struct JoystickDisconnected
254 {
255 unsigned int joystickId{};
256 };
257
262 struct TouchBegan
263 {
264 unsigned int finger{};
265 Vector2i position;
266 };
267
272 struct TouchMoved
273 {
274 unsigned int finger{};
275 Vector2i position;
276 };
277
282 struct TouchEnded
283 {
284 unsigned int finger{};
285 Vector2i position;
286 };
287
292 struct SensorChanged
293 {
294 Sensor::Type type{};
295 Vector3f value;
296 };
297
306 template <typename TEventSubtype>
307 Event(const TEventSubtype& eventSubtype);
308
317 template <typename TEventSubtype>
318 [[nodiscard]] bool is() const;
319
328 template <typename TEventSubtype>
329 [[nodiscard]] const TEventSubtype* getIf() const;
330
339 template <typename Visitor>
340 decltype(auto) visit(Visitor&& visitor) const;
341
342 private:
344 // Member data
346 std::variant<Closed,
347 Resized,
348 FocusLost,
349 FocusGained,
350 TextEntered,
351 KeyPressed,
352 KeyReleased,
353 MouseWheelScrolled,
354 MouseButtonPressed,
355 MouseButtonReleased,
356 MouseMoved,
357 MouseMovedRaw,
358 MouseEntered,
359 MouseLeft,
360 JoystickButtonPressed,
361 JoystickButtonReleased,
362 JoystickMoved,
363 JoystickConnected,
364 JoystickDisconnected,
365 TouchBegan,
366 TouchMoved,
367 TouchEnded,
368 SensorChanged>
369 m_data;
370
372 // Helper functions
374 template <typename T, typename... Ts>
375 [[nodiscard]] static constexpr bool isInParameterPack(const std::variant<Ts...>*)
376 {
377 return std::disjunction_v<std::is_same<T, Ts>...>;
378 }
379
380 template <typename T>
381 static constexpr bool isEventSubtype = isInParameterPack<T>(decltype (&m_data)(nullptr));
382
383 friend class WindowBase;
384
385 template <typename Handler, typename... Ts>
386 [[nodiscard]] static constexpr bool isInvocableWithEventSubtype(const std::variant<Ts...>*)
387 {
388 return std::disjunction_v<std::is_invocable<Handler&, Ts&>...>;
389 }
390
391 template <typename Handler>
392 static constexpr bool isEventHandler = isInvocableWithEventSubtype<Handler>(decltype (&m_data)(nullptr));
393};
394
395} // namespace sf
396
397#include <SFML/Window/Event.inl>
398
399
Joystick.hpp
Keyboard.hpp
Mouse.hpp
Sensor.hpp
Vector2.hpp
sf::Event::WindowBase
friend class WindowBase
Definition Event.hpp:383
sf::Event::getIf
const TEventSubtype * getIf() const
Attempt to get specified event subtype.
sf::Event::is
bool is() const
Check current event subtype.
sf::Event::Event
Event(const TEventSubtype &eventSubtype)
Construct from a given sf::Event subtype.
sf::Event::visit
decltype(auto) visit(Visitor &&visitor) const
Apply a visitor to the event.
sf::Joystick::Axis
Axis
Axes supported by SFML joysticks.
Definition Joystick.hpp:55
sf::Keyboard::Scancode
Scan Scancode
Definition Keyboard.hpp:329
sf::Keyboard::Key
Key
Key codes.
Definition Keyboard.hpp:52
sf::Mouse::Button
Button
Mouse buttons.
Definition Mouse.hpp:50
sf::Mouse::Wheel
Wheel
Mouse wheels.
Definition Mouse.hpp:66
sf::Sensor::Type
Type
Sensor type.
Definition Sensor.hpp:45
sf
Definition AudioResource.hpp:36
sf::Vector3f
Vector3< float > Vector3f
Definition Vector3.hpp:306
sf::Vector2u
Vector2< unsigned int > Vector2u
Definition Vector2.hpp:209
sf::Vector2i
Vector2< int > Vector2i
Definition Vector2.hpp:208
sf::Event::Closed
Closed event subtype.
Definition Event.hpp:55
sf::Event::FocusGained
Gained focus event subtype.
Definition Event.hpp:80
sf::Event::FocusLost
Lost focus event subtype.
Definition Event.hpp:72
sf::Event::JoystickButtonPressed
Joystick button pressed event subtype.
Definition Event.hpp:214
sf::Event::JoystickButtonPressed::button
unsigned int button
Index of the button that has been pressed (in range [0 .. Joystick::ButtonCount - 1])
Definition Event.hpp:216
sf::Event::JoystickButtonPressed::joystickId
unsigned int joystickId
Index of the joystick (in range [0 .. Joystick::Count - 1])
Definition Event.hpp:215
sf::Event::JoystickButtonReleased
Joystick button released event subtype.
Definition Event.hpp:224
sf::Event::JoystickButtonReleased::button
unsigned int button
Index of the button that has been released (in range [0 .. Joystick::ButtonCount - 1])
Definition Event.hpp:226
sf::Event::JoystickButtonReleased::joystickId
unsigned int joystickId
Index of the joystick (in range [0 .. Joystick::Count - 1])
Definition Event.hpp:225
sf::Event::JoystickConnected
Joystick connected event subtype.
Definition Event.hpp:245
sf::Event::JoystickConnected::joystickId
unsigned int joystickId
Index of the joystick (in range [0 .. Joystick::Count - 1])
Definition Event.hpp:246
sf::Event::JoystickDisconnected
Joystick disconnected event subtype.
Definition Event.hpp:254
sf::Event::JoystickDisconnected::joystickId
unsigned int joystickId
Index of the joystick (in range [0 .. Joystick::Count - 1])
Definition Event.hpp:255
sf::Event::JoystickMoved
Joystick axis move event subtype.
Definition Event.hpp:234
sf::Event::JoystickMoved::joystickId
unsigned int joystickId
Index of the joystick (in range [0 .. Joystick::Count - 1])
Definition Event.hpp:235
sf::Event::JoystickMoved::axis
Joystick::Axis axis
Axis on which the joystick moved.
Definition Event.hpp:236
sf::Event::JoystickMoved::position
float position
New position on the axis (in range [-100 .. 100])
Definition Event.hpp:237
sf::Event::KeyPressed
Key pressed event subtype.
Definition Event.hpp:97
sf::Event::KeyPressed::system
bool system
Is the System key pressed?
Definition Event.hpp:103
sf::Event::KeyPressed::control
bool control
Is the Control key pressed?
Definition Event.hpp:101
sf::Event::KeyPressed::shift
bool shift
Is the Shift key pressed?
Definition Event.hpp:102
sf::Event::KeyPressed::alt
bool alt
Is the Alt key pressed?
Definition Event.hpp:100
sf::Event::KeyPressed::code
Keyboard::Key code
Code of the key that has been pressed.
Definition Event.hpp:98
sf::Event::KeyPressed::scancode
Keyboard::Scancode scancode
Physical code of the key that has been pressed.
Definition Event.hpp:99
sf::Event::KeyReleased
Key released event subtype.
Definition Event.hpp:111
sf::Event::KeyReleased::alt
bool alt
Is the Alt key pressed?
Definition Event.hpp:114
sf::Event::KeyReleased::control
bool control
Is the Control key pressed?
Definition Event.hpp:115
sf::Event::KeyReleased::shift
bool shift
Is the Shift key pressed?
Definition Event.hpp:116
sf::Event::KeyReleased::system
bool system
Is the System key pressed?
Definition Event.hpp:117
sf::Event::KeyReleased::code
Keyboard::Key code
Code of the key that has been released.
Definition Event.hpp:112
sf::Event::KeyReleased::scancode
Keyboard::Scancode scancode
Physical code of the key that has been released.
Definition Event.hpp:113
sf::Event::MouseButtonPressed
Mouse button pressed event subtype.
Definition Event.hpp:136
sf::Event::MouseButtonPressed::position
Vector2i position
Position of the mouse pointer, relative to the top left of the owner window.
Definition Event.hpp:138
sf::Event::MouseButtonPressed::button
Mouse::Button button
Code of the button that has been pressed.
Definition Event.hpp:137
sf::Event::MouseButtonReleased
Mouse button released event subtype.
Definition Event.hpp:146
sf::Event::MouseButtonReleased::position
Vector2i position
Position of the mouse pointer, relative to the top left of the owner window.
Definition Event.hpp:148
sf::Event::MouseButtonReleased::button
Mouse::Button button
Code of the button that has been released.
Definition Event.hpp:147
sf::Event::MouseEntered
Mouse entered event subtype.
Definition Event.hpp:198
sf::Event::MouseLeft
Mouse left event subtype.
Definition Event.hpp:206
sf::Event::MouseMovedRaw
Mouse move raw event subtype.
Definition Event.hpp:189
sf::Event::MouseMovedRaw::delta
Vector2i delta
Delta movement of the mouse since the last event.
Definition Event.hpp:190
sf::Event::MouseMoved
Mouse move event subtype.
Definition Event.hpp:156
sf::Event::MouseMoved::position
Vector2i position
Position of the mouse pointer, relative to the top left of the owner window.
Definition Event.hpp:157
sf::Event::MouseWheelScrolled
Mouse wheel scrolled event subtype.
Definition Event.hpp:125
sf::Event::MouseWheelScrolled::wheel
Mouse::Wheel wheel
Which wheel (for mice with multiple ones)
Definition Event.hpp:126
sf::Event::MouseWheelScrolled::position
Vector2i position
Position of the mouse pointer, relative to the top left of the owner window.
Definition Event.hpp:128
sf::Event::MouseWheelScrolled::delta
float delta
Wheel offset (positive is up/left, negative is down/right). High-precision mice may use non-integral ...
Definition Event.hpp:127
sf::Event::Resized
Resized event subtype.
Definition Event.hpp:63
sf::Event::Resized::size
Vector2u size
New size, in pixels.
Definition Event.hpp:64
sf::Event::SensorChanged
Sensor event subtype.
Definition Event.hpp:293
sf::Event::SensorChanged::type
Sensor::Type type
Type of the sensor.
Definition Event.hpp:294
sf::Event::SensorChanged::value
Vector3f value
Current value of the sensor on the X, Y, and Z axes.
Definition Event.hpp:295
sf::Event::TextEntered
Text event subtype.
Definition Event.hpp:88
sf::Event::TextEntered::unicode
char32_t unicode
UTF-32 Unicode value of the character.
Definition Event.hpp:89
sf::Event::TouchBegan
Touch began event subtype.
Definition Event.hpp:263
sf::Event::TouchBegan::position
Vector2i position
Start position of the touch, relative to the top left of the owner window.
Definition Event.hpp:265
sf::Event::TouchBegan::finger
unsigned int finger
Index of the finger in case of multi-touch events.
Definition Event.hpp:264
sf::Event::TouchEnded
Touch ended event subtype.
Definition Event.hpp:283
sf::Event::TouchEnded::position
Vector2i position
Final position of the touch, relative to the top left of the owner window.
Definition Event.hpp:285
sf::Event::TouchEnded::finger
unsigned int finger
Index of the finger in case of multi-touch events.
Definition Event.hpp:284
sf::Event::TouchMoved
Touch moved event subtype.
Definition Event.hpp:273
sf::Event::TouchMoved::position
Vector2i position
Current position of the touch, relative to the top left of the owner window.
Definition Event.hpp:275
sf::Event::TouchMoved::finger
unsigned int finger
Index of the finger in case of multi-touch events.
Definition Event.hpp:274