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
32#include <SFML/Window/Mouse.hpp>
34
36
37#include <type_traits>
38#include <variant>
39
40
41namespace sf
42{
47class Event
48{
49public:
54 struct Closed
55 {
56 };
57
66 struct Resized
67 {
69 };
70
75 struct FocusLost
76 {
77 };
78
84 {
85 };
86
92 {
93 char32_t unicode{};
94 };
95
101 {
104 bool alt{};
105 bool control{};
106 bool shift{};
107 bool system{};
108 };
109
123
134
144
154
160 {
162 };
163
193 {
195 };
196
202 {
203 };
204
210 {
211 };
212
218 {
219 unsigned int joystickId{};
220 unsigned int button{};
221 };
222
228 {
229 unsigned int joystickId{};
230 unsigned int button{};
231 };
232
238 {
239 unsigned int joystickId{};
241 float position{};
242 };
243
249 {
250 unsigned int joystickId{};
251 };
252
258 {
259 unsigned int joystickId{};
260 };
261
267 {
268 unsigned int finger{};
270 };
271
277 {
278 unsigned int finger{};
280 };
281
287 {
288 unsigned int finger{};
290 };
291
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,
385 MouseLeft,
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
friend class WindowBase
Definition Event.hpp:409
decltype(auto) visit(Visitor &&visitor)
Apply a visitor to the event.
const TEventSubtype * getIf() const
Attempt to get specified event subtype.
bool is() const
Check current event subtype.
Event(const TEventSubtype &eventSubtype)
Construct from a given sf::Event subtype.
TEventSubtype * getIf()
Attempt to get specified event subtype.
decltype(auto) visit(Visitor &&visitor) const
Apply a visitor to the event.
Axis
Axes supported by SFML joysticks.
Definition Joystick.hpp:55
Key
Key codes.
Definition Keyboard.hpp:52
Button
Mouse buttons.
Definition Mouse.hpp:50
Wheel
Mouse wheels.
Definition Mouse.hpp:66
Type
Sensor type.
Definition Sensor.hpp:45
Vector3< float > Vector3f
Definition Vector3.hpp:309
Vector2< unsigned int > Vector2u
Definition Vector2.hpp:212
Vector2< int > Vector2i
Definition Vector2.hpp:211
Closed event subtype.
Definition Event.hpp:55
Gained focus event subtype.
Definition Event.hpp:84
Lost focus event subtype.
Definition Event.hpp:76
Joystick button pressed event subtype.
Definition Event.hpp:218
unsigned int button
Index of the button that has been pressed (in range [0 .. Joystick::ButtonCount - 1]).
Definition Event.hpp:220
unsigned int joystickId
Index of the joystick (in range [0 .. Joystick::Count - 1]).
Definition Event.hpp:219
Joystick button released event subtype.
Definition Event.hpp:228
unsigned int button
Index of the button that has been released (in range [0 .. Joystick::ButtonCount - 1]).
Definition Event.hpp:230
unsigned int joystickId
Index of the joystick (in range [0 .. Joystick::Count - 1]).
Definition Event.hpp:229
Joystick connected event subtype.
Definition Event.hpp:249
unsigned int joystickId
Index of the joystick (in range [0 .. Joystick::Count - 1]).
Definition Event.hpp:250
Joystick disconnected event subtype.
Definition Event.hpp:258
unsigned int joystickId
Index of the joystick (in range [0 .. Joystick::Count - 1]).
Definition Event.hpp:259
Joystick axis move event subtype.
Definition Event.hpp:238
unsigned int joystickId
Index of the joystick (in range [0 .. Joystick::Count - 1]).
Definition Event.hpp:239
Joystick::Axis axis
Axis on which the joystick moved.
Definition Event.hpp:240
float position
New position on the axis (in range [-100 .. 100]).
Definition Event.hpp:241
Key pressed event subtype.
Definition Event.hpp:101
bool system
Is the System key pressed?
Definition Event.hpp:107
bool control
Is the Control key pressed?
Definition Event.hpp:105
bool shift
Is the Shift key pressed?
Definition Event.hpp:106
bool alt
Is the Alt key pressed?
Definition Event.hpp:104
Keyboard::Key code
Code of the key that has been pressed.
Definition Event.hpp:102
Keyboard::Scancode scancode
Physical code of the key that has been pressed.
Definition Event.hpp:103
Key released event subtype.
Definition Event.hpp:115
bool alt
Is the Alt key pressed?
Definition Event.hpp:118
bool control
Is the Control key pressed?
Definition Event.hpp:119
bool shift
Is the Shift key pressed?
Definition Event.hpp:120
bool system
Is the System key pressed?
Definition Event.hpp:121
Keyboard::Key code
Code of the key that has been released.
Definition Event.hpp:116
Keyboard::Scancode scancode
Physical code of the key that has been released.
Definition Event.hpp:117
Mouse button pressed event subtype.
Definition Event.hpp:140
Vector2i position
Position of the mouse pointer, relative to the top left of the owner window.
Definition Event.hpp:142
Mouse::Button button
Code of the button that has been pressed.
Definition Event.hpp:141
Mouse button released event subtype.
Definition Event.hpp:150
Vector2i position
Position of the mouse pointer, relative to the top left of the owner window.
Definition Event.hpp:152
Mouse::Button button
Code of the button that has been released.
Definition Event.hpp:151
Mouse entered event subtype.
Definition Event.hpp:202
Mouse left event subtype.
Definition Event.hpp:210
Mouse move raw event subtype.
Definition Event.hpp:193
Vector2i delta
Delta movement of the mouse since the last event.
Definition Event.hpp:194
Mouse move event subtype.
Definition Event.hpp:160
Vector2i position
Position of the mouse pointer, relative to the top left of the owner window.
Definition Event.hpp:161
Mouse wheel scrolled event subtype.
Definition Event.hpp:129
Mouse::Wheel wheel
Which wheel (for mice with multiple ones).
Definition Event.hpp:130
Vector2i position
Position of the mouse pointer, relative to the top left of the owner window.
Definition Event.hpp:132
float delta
Wheel offset (positive is up/left, negative is down/right). High-precision mice may use non-integral ...
Definition Event.hpp:131
Resized event subtype.
Definition Event.hpp:67
Vector2u size
New size, in pixels.
Definition Event.hpp:68
Sensor event subtype.
Definition Event.hpp:297
Sensor::Type type
Type of the sensor.
Definition Event.hpp:298
Vector3f value
Current value of the sensor on the X, Y, and Z axes.
Definition Event.hpp:299
Text event subtype.
Definition Event.hpp:92
char32_t unicode
UTF-32 Unicode value of the character.
Definition Event.hpp:93
Touch began event subtype.
Definition Event.hpp:267
Vector2i position
Start position of the touch, relative to the top left of the owner window.
Definition Event.hpp:269
unsigned int finger
Index of the finger in case of multi-touch events.
Definition Event.hpp:268
Touch ended event subtype.
Definition Event.hpp:287
Vector2i position
Final position of the touch, relative to the top left of the owner window.
Definition Event.hpp:289
unsigned int finger
Index of the finger in case of multi-touch events.
Definition Event.hpp:288
Touch moved event subtype.
Definition Event.hpp:277
Vector2i position
Current position of the touch, relative to the top left of the owner window.
Definition Event.hpp:279
unsigned int finger
Index of the finger in case of multi-touch events.
Definition Event.hpp:278