SFML - Simple and Fast Multimedia Library

SFML 3.1.0
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-2026 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
41namespace sf
42{
47 class Event
48{
49public:
54 struct Closed
55 {
56 };
57
66 struct Resized
67 {
68 Vector2u size;
69 };
70
75 struct FocusLost
76 {
77 };
78
83 struct FocusGained
84 {
85 };
86
91 struct TextEntered
92 {
93 char32_t unicode{};
94 };
95
100 struct KeyPressed
101 {
102 Keyboard::Key code{};
103 Keyboard::Scancode scancode{};
104 bool alt{};
105 bool control{};
106 bool shift{};
107 bool system{};
108 };
109
114 struct KeyReleased
115 {
116 Keyboard::Key code{};
117 Keyboard::Scancode scancode{};
118 bool alt{};
119 bool control{};
120 bool shift{};
121 bool system{};
122 };
123
128 struct MouseWheelScrolled
129 {
130 Mouse::Wheel wheel{};
131 float delta{};
132 Vector2i position;
133 };
134
139 struct MouseButtonPressed
140 {
141 Mouse::Button button{};
142 Vector2i position;
143 };
144
149 struct MouseButtonReleased
150 {
151 Mouse::Button button{};
152 Vector2i position;
153 };
154
159 struct MouseMoved
160 {
161 Vector2i position;
162 };
163
192 struct MouseMovedRaw
193 {
194 Vector2i delta;
195 };
196
201 struct MouseEntered
202 {
203 };
204
209 struct MouseLeft
210 {
211 };
212
217 struct JoystickButtonPressed
218 {
219 unsigned int joystickId{};
220 unsigned int button{};
221 };
222
227 struct JoystickButtonReleased
228 {
229 unsigned int joystickId{};
230 unsigned int button{};
231 };
232
237 struct JoystickMoved
238 {
239 unsigned int joystickId{};
240 Joystick::Axis axis{};
241 float position{};
242 };
243
248 struct JoystickConnected
249 {
250 unsigned int joystickId{};
251 };
252
257 struct JoystickDisconnected
258 {
259 unsigned int joystickId{};
260 };
261
266 struct TouchBegan
267 {
268 unsigned int finger{};
269 Vector2i position;
270 };
271
276 struct TouchMoved
277 {
278 unsigned int finger{};
279 Vector2i position;
280 };
281
286 struct TouchEnded
287 {
288 unsigned int finger{};
289 Vector2i position;
290 };
291
296 struct SensorChanged
297 {
298 Sensor::Type type{};
299 Vector3f value;
300 };
301
310 template <typename TEventSubtype>
311 Event(const TEventSubtype& eventSubtype);
312
321 template <typename TEventSubtype>
322 [[nodiscard]] bool is() const;
323
332 template <typename TEventSubtype>
333 [[nodiscard]] TEventSubtype* getIf();
334
343 template <typename TEventSubtype>
344 [[nodiscard]] const TEventSubtype* getIf() const;
345
354 template <typename Visitor>
355 decltype(auto) visit(Visitor&& visitor);
356
365 template <typename Visitor>
366 decltype(auto) visit(Visitor&& visitor) const;
367
368private:
370 // Member data
372 std::variant<Closed,
373 Resized,
374 FocusLost,
375 FocusGained,
376 TextEntered,
377 KeyPressed,
378 KeyReleased,
379 MouseWheelScrolled,
380 MouseButtonPressed,
381 MouseButtonReleased,
382 MouseMoved,
383 MouseMovedRaw,
384 MouseEntered,
385 MouseLeft,
386 JoystickButtonPressed,
387 JoystickButtonReleased,
388 JoystickMoved,
389 JoystickConnected,
390 JoystickDisconnected,
391 TouchBegan,
392 TouchMoved,
393 TouchEnded,
394 SensorChanged>
395 m_data;
396
398 // Helper functions
400 template <typename T, typename... Ts>
401 [[nodiscard]] static constexpr bool isInParameterPack(const std::variant<Ts...>*)
402 {
403 return std::disjunction_v<std::is_same<T, Ts>...>;
404 }
405
406 template <typename T>
407 static constexpr bool isEventSubtype = isInParameterPack<T>(decltype (&m_data)(nullptr));
408
409 friend class WindowBase;
410
411 template <typename Handler, typename... Ts>
412 [[nodiscard]] static constexpr bool isInvocableWithEventSubtype(const std::variant<Ts...>*)
413 {
414 return std::disjunction_v<std::is_invocable<Handler&, Ts&>...>;
415 }
416
417 template <typename Handler>
418 static constexpr bool isEventHandler = isInvocableWithEventSubtype<Handler>(decltype (&m_data)(nullptr));
419};
420
421} // namespace sf
422
423#include <SFML/Window/Event.inl>
424
425
Joystick.hpp
Keyboard.hpp
Mouse.hpp
Sensor.hpp
Vector2.hpp
sf::Event::WindowBase
friend class WindowBase
Definition Event.hpp:409
sf::Event::visit
decltype(auto) visit(Visitor &&visitor)
Apply a visitor to the event.
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::getIf
TEventSubtype * getIf()
Attempt to get specified 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:309
sf::Vector2u
Vector2< unsigned int > Vector2u
Definition Vector2.hpp:212
sf::Vector2i
Vector2< int > Vector2i
Definition Vector2.hpp:211
sf::Event::Closed
Closed event subtype.
Definition Event.hpp:55
sf::Event::FocusGained
Gained focus event subtype.
Definition Event.hpp:84
sf::Event::FocusLost
Lost focus event subtype.
Definition Event.hpp:76
sf::Event::JoystickButtonPressed
Joystick button pressed event subtype.
Definition Event.hpp:218
sf::Event::JoystickButtonPressed::button
unsigned int button
Index of the button that has been pressed (in range [0 .. Joystick::ButtonCount - 1]).
Definition Event.hpp:220
sf::Event::JoystickButtonPressed::joystickId
unsigned int joystickId
Index of the joystick (in range [0 .. Joystick::Count - 1]).
Definition Event.hpp:219
sf::Event::JoystickButtonReleased
Joystick button released event subtype.
Definition Event.hpp:228
sf::Event::JoystickButtonReleased::button
unsigned int button
Index of the button that has been released (in range [0 .. Joystick::ButtonCount - 1]).
Definition Event.hpp:230
sf::Event::JoystickButtonReleased::joystickId
unsigned int joystickId
Index of the joystick (in range [0 .. Joystick::Count - 1]).
Definition Event.hpp:229
sf::Event::JoystickConnected
Joystick connected event subtype.
Definition Event.hpp:249
sf::Event::JoystickConnected::joystickId
unsigned int joystickId
Index of the joystick (in range [0 .. Joystick::Count - 1]).
Definition Event.hpp:250
sf::Event::JoystickDisconnected
Joystick disconnected event subtype.
Definition Event.hpp:258
sf::Event::JoystickDisconnected::joystickId
unsigned int joystickId
Index of the joystick (in range [0 .. Joystick::Count - 1]).
Definition Event.hpp:259
sf::Event::JoystickMoved
Joystick axis move event subtype.
Definition Event.hpp:238
sf::Event::JoystickMoved::joystickId
unsigned int joystickId
Index of the joystick (in range [0 .. Joystick::Count - 1]).
Definition Event.hpp:239
sf::Event::JoystickMoved::axis
Joystick::Axis axis
Axis on which the joystick moved.
Definition Event.hpp:240
sf::Event::JoystickMoved::position
float position
New position on the axis (in range [-100 .. 100]).
Definition Event.hpp:241
sf::Event::KeyPressed
Key pressed event subtype.
Definition Event.hpp:101
sf::Event::KeyPressed::system
bool system
Is the System key pressed?
Definition Event.hpp:107
sf::Event::KeyPressed::control
bool control
Is the Control key pressed?
Definition Event.hpp:105
sf::Event::KeyPressed::shift
bool shift
Is the Shift key pressed?
Definition Event.hpp:106
sf::Event::KeyPressed::alt
bool alt
Is the Alt key pressed?
Definition Event.hpp:104
sf::Event::KeyPressed::code
Keyboard::Key code
Code of the key that has been pressed.
Definition Event.hpp:102
sf::Event::KeyPressed::scancode
Keyboard::Scancode scancode
Physical code of the key that has been pressed.
Definition Event.hpp:103
sf::Event::KeyReleased
Key released event subtype.
Definition Event.hpp:115
sf::Event::KeyReleased::alt
bool alt
Is the Alt key pressed?
Definition Event.hpp:118
sf::Event::KeyReleased::control
bool control
Is the Control key pressed?
Definition Event.hpp:119
sf::Event::KeyReleased::shift
bool shift
Is the Shift key pressed?
Definition Event.hpp:120
sf::Event::KeyReleased::system
bool system
Is the System key pressed?
Definition Event.hpp:121
sf::Event::KeyReleased::code
Keyboard::Key code
Code of the key that has been released.
Definition Event.hpp:116
sf::Event::KeyReleased::scancode
Keyboard::Scancode scancode
Physical code of the key that has been released.
Definition Event.hpp:117
sf::Event::MouseButtonPressed
Mouse button pressed event subtype.
Definition Event.hpp:140
sf::Event::MouseButtonPressed::position
Vector2i position
Position of the mouse pointer, relative to the top left of the owner window.
Definition Event.hpp:142
sf::Event::MouseButtonPressed::button
Mouse::Button button
Code of the button that has been pressed.
Definition Event.hpp:141
sf::Event::MouseButtonReleased
Mouse button released event subtype.
Definition Event.hpp:150
sf::Event::MouseButtonReleased::position
Vector2i position
Position of the mouse pointer, relative to the top left of the owner window.
Definition Event.hpp:152
sf::Event::MouseButtonReleased::button
Mouse::Button button
Code of the button that has been released.
Definition Event.hpp:151
sf::Event::MouseEntered
Mouse entered event subtype.
Definition Event.hpp:202
sf::Event::MouseLeft
Mouse left event subtype.
Definition Event.hpp:210
sf::Event::MouseMovedRaw
Mouse move raw event subtype.
Definition Event.hpp:193
sf::Event::MouseMovedRaw::delta
Vector2i delta
Delta movement of the mouse since the last event.
Definition Event.hpp:194
sf::Event::MouseMoved
Mouse move event subtype.
Definition Event.hpp:160
sf::Event::MouseMoved::position
Vector2i position
Position of the mouse pointer, relative to the top left of the owner window.
Definition Event.hpp:161
sf::Event::MouseWheelScrolled
Mouse wheel scrolled event subtype.
Definition Event.hpp:129
sf::Event::MouseWheelScrolled::wheel
Mouse::Wheel wheel
Which wheel (for mice with multiple ones).
Definition Event.hpp:130
sf::Event::MouseWheelScrolled::position
Vector2i position
Position of the mouse pointer, relative to the top left of the owner window.
Definition Event.hpp:132
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:131
sf::Event::Resized
Resized event subtype.
Definition Event.hpp:67
sf::Event::Resized::size
Vector2u size
New size, in pixels.
Definition Event.hpp:68
sf::Event::SensorChanged
Sensor event subtype.
Definition Event.hpp:297
sf::Event::SensorChanged::type
Sensor::Type type
Type of the sensor.
Definition Event.hpp:298
sf::Event::SensorChanged::value
Vector3f value
Current value of the sensor on the X, Y, and Z axes.
Definition Event.hpp:299
sf::Event::TextEntered
Text event subtype.
Definition Event.hpp:92
sf::Event::TextEntered::unicode
char32_t unicode
UTF-32 Unicode value of the character.
Definition Event.hpp:93
sf::Event::TouchBegan
Touch began event subtype.
Definition Event.hpp:267
sf::Event::TouchBegan::position
Vector2i position
Start position of the touch, relative to the top left of the owner window.
Definition Event.hpp:269
sf::Event::TouchBegan::finger
unsigned int finger
Index of the finger in case of multi-touch events.
Definition Event.hpp:268
sf::Event::TouchEnded
Touch ended event subtype.
Definition Event.hpp:287
sf::Event::TouchEnded::position
Vector2i position
Final position of the touch, relative to the top left of the owner window.
Definition Event.hpp:289
sf::Event::TouchEnded::finger
unsigned int finger
Index of the finger in case of multi-touch events.
Definition Event.hpp:288
sf::Event::TouchMoved
Touch moved event subtype.
Definition Event.hpp:277
sf::Event::TouchMoved::position
Vector2i position
Current position of the touch, relative to the top left of the owner window.
Definition Event.hpp:279
sf::Event::TouchMoved::finger
unsigned int finger
Index of the finger in case of multi-touch events.
Definition Event.hpp:278