FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
event.hpp
Go to the documentation of this file.
1// Copyright 2020 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4#ifndef FTXUI_COMPONENT_EVENT_HPP
5#define FTXUI_COMPONENT_EVENT_HPP
6
7#include <ftxui/component/mouse.hpp> // for Mouse
8#include <string> // for string, operator==
9
10namespace ftxui {
11
12class ScreenInteractive;
13class ComponentBase;
14
15/// @brief Represent an event. It can be key press event, a terminal resize, or
16/// more ...
17///
18/// For example:
19/// - Printable character can be created using Event::Character('a').
20/// - Some special are predefined, like Event::ArrowLeft.
21/// - One can find arbitrary code for special Events using:
22/// ./example/util/print_key_press
23/// For instance, CTLR+A maps to Event::Special({1});
24///
25/// Useful documentation about xterm specification:
26/// https://invisible-island.net/xterm/ctlseqs/ctlseqs.html
27///
28/// @ingroup component
29struct Event {
30 // --- Constructor section ---------------------------------------------------
31 static Event Character(std::string);
32 static Event Character(char);
33 static Event Character(wchar_t);
34 static Event Special(std::string);
35 static Event Mouse(std::string, Mouse mouse);
36 static Event CursorPosition(std::string, int x, int y); // Internal
37 static Event CursorShape(std::string, int shape); // Internal
38
39 // --- Arrow ---
40 static const Event ArrowLeft;
41 static const Event ArrowRight;
42 static const Event ArrowUp;
43 static const Event ArrowDown;
44
45 static const Event ArrowLeftCtrl;
46 static const Event ArrowRightCtrl;
47 static const Event ArrowUpCtrl;
48 static const Event ArrowDownCtrl;
49
50 // --- Other ---
51 static const Event Backspace;
52 static const Event Delete;
53 static const Event Return;
54 static const Event Escape;
55 static const Event Tab;
56 static const Event TabReverse;
57
58 // --- Navigation keys ---
59 static const Event Insert;
60 static const Event Home;
61 static const Event End;
62 static const Event PageUp;
63 static const Event PageDown;
64
65 // --- Function keys ---
66 static const Event F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12;
67
68 // --- Control keys ---
69 static const Event a, A, CtrlA, AltA, CtrlAltA;
70 static const Event b, B, CtrlB, AltB, CtrlAltB;
71 static const Event c, C, CtrlC, AltC, CtrlAltC;
72 static const Event d, D, CtrlD, AltD, CtrlAltD;
73 static const Event e, E, CtrlE, AltE, CtrlAltE;
74 static const Event f, F, CtrlF, AltF, CtrlAltF;
75 static const Event g, G, CtrlG, AltG, CtrlAltG;
76 static const Event h, H, CtrlH, AltH, CtrlAltH;
77 static const Event i, I, CtrlI, AltI, CtrlAltI;
78 static const Event j, J, CtrlJ, AltJ, CtrlAltJ;
79 static const Event k, K, CtrlK, AltK, CtrlAltK;
80 static const Event l, L, CtrlL, AltL, CtrlAltL;
81 static const Event m, M, CtrlM, AltM, CtrlAltM;
82 static const Event n, N, CtrlN, AltN, CtrlAltN;
83 static const Event o, O, CtrlO, AltO, CtrlAltO;
84 static const Event p, P, CtrlP, AltP, CtrlAltP;
85 static const Event q, Q, CtrlQ, AltQ, CtrlAltQ;
86 static const Event r, R, CtrlR, AltR, CtrlAltR;
87 static const Event s, S, CtrlS, AltS, CtrlAltS;
88 static const Event t, T, CtrlT, AltT, CtrlAltT;
89 static const Event u, U, CtrlU, AltU, CtrlAltU;
90 static const Event v, V, CtrlV, AltV, CtrlAltV;
91 static const Event w, W, CtrlW, AltW, CtrlAltW;
92 static const Event x, X, CtrlX, AltX, CtrlAltX;
93 static const Event y, Y, CtrlY, AltY, CtrlAltY;
94 static const Event z, Z, CtrlZ, AltZ, CtrlAltZ;
95
96 // --- Custom ---
97 static const Event Custom;
98
99 //--- Method section ---------------------------------------------------------
100 bool operator==(const Event& other) const { return input_ == other.input_; }
101 bool operator!=(const Event& other) const { return !operator==(other); }
102 bool operator<(const Event& other) const { return input_ < other.input_; }
103
104 const std::string& input() const { return input_; }
105
106 bool is_character() const { return type_ == Type::Character; }
107 std::string character() const { return input_; }
108
109 bool is_mouse() const { return type_ == Type::Mouse; }
110 struct Mouse& mouse() { return data_.mouse; }
111
112 // --- Internal Method section -----------------------------------------------
113 bool is_cursor_position() const { return type_ == Type::CursorPosition; }
114 int cursor_x() const { return data_.cursor.x; }
115 int cursor_y() const { return data_.cursor.y; }
116
117 bool is_cursor_shape() const { return type_ == Type::CursorShape; }
118 int cursor_shape() const { return data_.cursor_shape; }
119
120 // Debug
121 std::string DebugString() const;
122
123 //--- State section ----------------------------------------------------------
125
126 private:
127 friend ComponentBase;
128 friend ScreenInteractive;
129 enum class Type {
130 Unknown,
131 Character,
132 Mouse,
133 CursorPosition,
134 CursorShape,
135 };
136 Type type_ = Type::Unknown;
137
138 struct Cursor {
139 int x = 0;
140 int y = 0;
141 };
142
143 union {
144 struct Mouse mouse;
145 struct Cursor cursor;
147 } data_ = {};
148
149 std::string input_;
150};
151
152} // namespace ftxui
153
154#endif /* end of include guard: FTXUI_COMPONENT_EVENT_HPP */
static const Event TabReverse
Definition event.hpp:56
int cursor_shape() const
Definition event.hpp:118
static const Event j
Definition event.hpp:78
static const Event CtrlC
Definition event.hpp:71
static const Event CtrlP
Definition event.hpp:84
static const Event CtrlV
Definition event.hpp:90
static const Event ArrowLeftCtrl
Definition event.hpp:45
static const Event CtrlL
Definition event.hpp:80
static const Event AltT
Definition event.hpp:88
std::string character() const
Definition event.hpp:107
static const Event w
Definition event.hpp:91
static const Event CtrlAltX
Definition event.hpp:92
static const Event D
Definition event.hpp:72
static const Event CtrlAltN
Definition event.hpp:82
static const Event a
Definition event.hpp:69
static Event CursorShape(std::string, int shape)
An event corresponding to a terminal DCS (Device Control String).
Definition event.cpp:63
static const Event AltH
Definition event.hpp:76
static const Event AltF
Definition event.hpp:74
static const Event K
Definition event.hpp:79
int cursor_y() const
Definition event.hpp:115
static const Event T
Definition event.hpp:88
static const Event CtrlAltC
Definition event.hpp:71
static const Event X
Definition event.hpp:92
int cursor_x() const
Definition event.hpp:114
static const Event CtrlE
Definition event.hpp:73
static const Event Q
Definition event.hpp:85
static const Event u
Definition event.hpp:89
static const Event PageUp
Definition event.hpp:62
static const Event h
Definition event.hpp:76
static const Event CtrlAltF
Definition event.hpp:74
static const Event CtrlZ
Definition event.hpp:94
static const Event J
Definition event.hpp:78
int cursor_shape
Definition event.hpp:146
static const Event CtrlU
Definition event.hpp:89
static const Event AltQ
Definition event.hpp:85
static const Event b
Definition event.hpp:70
static const Event Escape
Definition event.hpp:54
static const Event AltY
Definition event.hpp:93
static const Event CtrlAltI
Definition event.hpp:77
bool is_mouse() const
Definition event.hpp:109
static const Event AltL
Definition event.hpp:80
static const Event AltW
Definition event.hpp:91
static const Event F12
Definition event.hpp:66
struct Mouse & mouse()
Definition event.hpp:110
static const Event E
Definition event.hpp:73
static const Event m
Definition event.hpp:81
static const Event N
Definition event.hpp:82
bool is_cursor_position() const
Definition event.hpp:113
static const Event CtrlAltP
Definition event.hpp:84
static const Event CtrlAltE
Definition event.hpp:73
static const Event F5
Definition event.hpp:66
static const Event CtrlF
Definition event.hpp:74
static const Event F3
Definition event.hpp:66
static const Event CtrlAltJ
Definition event.hpp:78
static const Event z
Definition event.hpp:94
static const Event AltK
Definition event.hpp:79
static const Event B
Definition event.hpp:70
static const Event H
Definition event.hpp:76
static const Event CtrlX
Definition event.hpp:92
static const Event F9
Definition event.hpp:66
static const Event AltC
Definition event.hpp:71
static const Event CtrlB
Definition event.hpp:70
static const Event CtrlAltH
Definition event.hpp:76
static const Event O
Definition event.hpp:83
ScreenInteractive * screen_
Definition event.hpp:124
static const Event R
Definition event.hpp:86
static const Event AltM
Definition event.hpp:81
static const Event CtrlR
Definition event.hpp:86
static const Event CtrlAltW
Definition event.hpp:91
static const Event CtrlAltO
Definition event.hpp:83
static const Event CtrlY
Definition event.hpp:93
static const Event Custom
Definition event.hpp:97
static const Event A
Definition event.hpp:69
static const Event AltG
Definition event.hpp:75
static const Event p
Definition event.hpp:84
static const Event l
Definition event.hpp:80
static const Event CtrlH
Definition event.hpp:76
bool operator<(const Event &other) const
Definition event.hpp:102
struct Mouse mouse
Definition event.hpp:144
std::string DebugString() const
Return a string representation of the event.
Definition event.cpp:91
static Event Character(std::string)
An event corresponding to a given typed character.
Definition event.cpp:28
static const Event AltO
Definition event.hpp:83
static const Event CtrlJ
Definition event.hpp:78
static const Event CtrlAltM
Definition event.hpp:81
static const Event CtrlS
Definition event.hpp:87
static const Event Z
Definition event.hpp:94
static const Event AltR
Definition event.hpp:86
static const Event CtrlW
Definition event.hpp:91
static const Event CtrlN
Definition event.hpp:82
static const Event F2
Definition event.hpp:66
static const Event CtrlM
Definition event.hpp:81
static const Event G
Definition event.hpp:75
static const Event Backspace
Definition event.hpp:51
static const Event d
Definition event.hpp:72
static const Event CtrlAltR
Definition event.hpp:86
static const Event CtrlK
Definition event.hpp:79
static const Event x
Definition event.hpp:92
static const Event F7
Definition event.hpp:66
static const Event ArrowUp
Definition event.hpp:42
const std::string & input() const
Definition event.hpp:104
static const Event Tab
Definition event.hpp:55
static const Event r
Definition event.hpp:86
static const Event AltU
Definition event.hpp:89
static const Event CtrlQ
Definition event.hpp:85
static const Event CtrlAltZ
Definition event.hpp:94
static const Event AltN
Definition event.hpp:82
static const Event AltA
Definition event.hpp:69
static const Event ArrowDown
Definition event.hpp:43
struct Cursor cursor
Definition event.hpp:145
static const Event End
Definition event.hpp:61
static const Event F11
Definition event.hpp:66
static const Event CtrlG
Definition event.hpp:75
static const Event n
Definition event.hpp:82
static const Event q
Definition event.hpp:85
static const Event AltZ
Definition event.hpp:94
static const Event Home
Definition event.hpp:60
static const Event C
Definition event.hpp:71
static const Event AltD
Definition event.hpp:72
static const Event CtrlAltY
Definition event.hpp:93
static const Event AltJ
Definition event.hpp:78
static const Event F8
Definition event.hpp:66
static const Event CtrlAltL
Definition event.hpp:80
static const Event U
Definition event.hpp:89
static const Event o
Definition event.hpp:83
static const Event AltB
Definition event.hpp:70
static const Event F4
Definition event.hpp:66
static const Event t
Definition event.hpp:88
static const Event y
Definition event.hpp:93
static const Event ArrowUpCtrl
Definition event.hpp:47
static const Event k
Definition event.hpp:79
static const Event CtrlAltS
Definition event.hpp:87
static const Event s
Definition event.hpp:87
static const Event I
Definition event.hpp:77
static const Event CtrlT
Definition event.hpp:88
static const Event F
Definition event.hpp:74
static const Event AltI
Definition event.hpp:77
static const Event F10
Definition event.hpp:66
static const Event AltP
Definition event.hpp:84
static const Event PageDown
Definition event.hpp:63
static const Event Y
Definition event.hpp:93
static const Event CtrlAltK
Definition event.hpp:79
static Event CursorPosition(std::string, int x, int y)
Definition event.cpp:82
static const Event F6
Definition event.hpp:66
static const Event CtrlAltG
Definition event.hpp:75
static const Event CtrlA
Definition event.hpp:69
static const Event i
Definition event.hpp:77
static const Event AltS
Definition event.hpp:87
static const Event g
Definition event.hpp:75
static const Event F1
Definition event.hpp:66
static const Event S
Definition event.hpp:87
static const Event Return
Definition event.hpp:53
static const Event CtrlAltU
Definition event.hpp:89
bool operator==(const Event &other) const
Definition event.hpp:100
static const Event V
Definition event.hpp:90
static const Event CtrlAltT
Definition event.hpp:88
static const Event CtrlAltA
Definition event.hpp:69
static const Event AltE
Definition event.hpp:73
static const Event P
Definition event.hpp:84
static const Event CtrlD
Definition event.hpp:72
static const Event ArrowLeft
Definition event.hpp:40
static const Event CtrlAltB
Definition event.hpp:70
bool operator!=(const Event &other) const
Definition event.hpp:101
static const Event AltV
Definition event.hpp:90
static const Event v
Definition event.hpp:90
static const Event e
Definition event.hpp:73
bool is_character() const
Definition event.hpp:106
static const Event CtrlO
Definition event.hpp:83
static const Event Delete
Definition event.hpp:52
static const Event CtrlAltV
Definition event.hpp:90
static const Event ArrowDownCtrl
Definition event.hpp:48
static const Event AltX
Definition event.hpp:92
static const Event CtrlAltD
Definition event.hpp:72
static const Event L
Definition event.hpp:80
static const Event W
Definition event.hpp:91
static const Event f
Definition event.hpp:74
static const Event Insert
Definition event.hpp:59
static const Event CtrlI
Definition event.hpp:77
static const Event ArrowRightCtrl
Definition event.hpp:46
bool is_cursor_shape() const
Definition event.hpp:117
static const Event c
Definition event.hpp:71
static const Event CtrlAltQ
Definition event.hpp:85
static const Event M
Definition event.hpp:81
static Event Special(std::string)
An custom event whose meaning is defined by the user of the library.
Definition event.cpp:74
static const Event ArrowRight
Definition event.hpp:41
It implement rendering itself as ftxui::Element. It implement keyboard navigation by responding to ft...
ScreenInteractive is a Screen that can handle events, run a main loop, and manage components.
Represent an event. It can be key press event, a terminal resize, or more ...
Definition event.hpp:29
A mouse event. It contains the coordinate of the mouse, the button pressed and the modifier (shift,...
Definition mouse.hpp:11
The FTXUI ftxui:: namespace.
Definition animation.hpp:10