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