FTXUI  5.0.0
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 <functional>
9#include <string> // for string, operator==
10#include <vector>
11
12namespace ftxui {
13
14class ScreenInteractive;
15class ComponentBase;
16
17/// @brief Represent an event. It can be key press event, a terminal resize, or
18/// more ...
19///
20/// For example:
21/// - Printable character can be created using Event::Character('a').
22/// - Some special are predefined, like Event::ArrowLeft.
23/// - One can find arbitrary code for special Events using:
24/// ./example/util/print_key_press
25/// For instance, CTLR+A maps to Event::Special({1});
26///
27/// Useful documentation about xterm specification:
28/// https://invisible-island.net/xterm/ctlseqs/ctlseqs.html
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 CursorReporting(std::string, int x, int y);
37
38 // --- Arrow ---
39 static const Event ArrowLeft;
40 static const Event ArrowRight;
41 static const Event ArrowUp;
42 static const Event ArrowDown;
43
44 static const Event ArrowLeftCtrl;
45 static const Event ArrowRightCtrl;
46 static const Event ArrowUpCtrl;
47 static const Event ArrowDownCtrl;
48
49 // --- Other ---
50 static const Event Backspace;
51 static const Event Delete;
52 static const Event Return;
53 static const Event Escape;
54 static const Event Tab;
55 static const Event TabReverse;
56 static const Event F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12;
57
58 static const Event Home;
59 static const Event End;
60
61 static const Event PageUp;
62 static const Event PageDown;
63
64 // --- Custom ---
65 static const Event Custom;
66
67 //--- Method section ---------------------------------------------------------
68 bool is_character() const { return type_ == Type::Character; }
69 std::string character() const { return input_; }
70
71 bool is_mouse() const { return type_ == Type::Mouse; }
72 struct Mouse& mouse() { return data_.mouse; }
73
74 bool is_cursor_reporting() const { return type_ == Type::CursorReporting; }
75 int cursor_x() const { return data_.cursor.x; }
76 int cursor_y() const { return data_.cursor.y; }
77
78 const std::string& input() const { return input_; }
79
80 bool operator==(const Event& other) const { return input_ == other.input_; }
81 bool operator!=(const Event& other) const { return !operator==(other); }
82
83 //--- State section ----------------------------------------------------------
85
86 private:
87 friend ComponentBase;
88 friend ScreenInteractive;
89 enum class Type {
90 Unknown,
91 Character,
92 Mouse,
93 CursorReporting,
94 };
95 Type type_ = Type::Unknown;
96
97 struct Cursor {
98 int x = 0;
99 int y = 0;
100 };
101
102 union {
103 struct Mouse mouse;
104 struct Cursor cursor;
105 } data_ = {};
106
107 std::string input_;
108};
109
110} // namespace ftxui
111
112#endif /* end of include guard: FTXUI_COMPONENT_EVENT_HPP */
It implement rendering itself as ftxui::Element. It implement keyboard navigation by responding to ft...
Component Slider(SliderOption< T > options)
A slider in any direction.
Definition slider.cpp:339
Represent an event. It can be key press event, a terminal resize, or more ...
Definition event.hpp:29
static const Event TabReverse
Definition event.hpp:55
static const Event ArrowLeftCtrl
Definition event.hpp:44
std::string character() const
Definition event.hpp:69
static Event CursorReporting(std::string, int x, int y)
Definition event.cpp:64
int cursor_y() const
Definition event.hpp:76
int cursor_x() const
Definition event.hpp:75
static const Event PageUp
Definition event.hpp:61
static const Event Escape
Definition event.hpp:53
bool is_mouse() const
Definition event.hpp:71
static const Event F12
Definition event.hpp:56
struct Mouse & mouse()
Definition event.hpp:72
static const Event F5
Definition event.hpp:56
static const Event F3
Definition event.hpp:56
static const Event F9
Definition event.hpp:56
ScreenInteractive * screen_
Definition event.hpp:84
static const Event Custom
Definition event.hpp:65
static Event Character(std::string)
An event corresponding to a given typed character.
Definition event.cpp:16
static const Event F2
Definition event.hpp:56
static const Event Backspace
Definition event.hpp:50
static const Event F7
Definition event.hpp:56
static const Event ArrowUp
Definition event.hpp:41
const std::string & input() const
Definition event.hpp:78
static const Event Tab
Definition event.hpp:54
static const Event ArrowDown
Definition event.hpp:42
static const Event End
Definition event.hpp:59
static const Event F11
Definition event.hpp:56
static const Event Home
Definition event.hpp:58
static const Event F8
Definition event.hpp:56
static const Event F4
Definition event.hpp:56
static const Event ArrowUpCtrl
Definition event.hpp:46
static const Event F10
Definition event.hpp:56
static const Event PageDown
Definition event.hpp:62
static const Event F6
Definition event.hpp:56
static const Event F1
Definition event.hpp:56
static const Event Return
Definition event.hpp:52
bool operator==(const Event &other) const
Definition event.hpp:80
static const Event ArrowLeft
Definition event.hpp:39
bool operator!=(const Event &other) const
Definition event.hpp:81
bool is_character() const
Definition event.hpp:68
static const Event Delete
Definition event.hpp:51
static const Event ArrowDownCtrl
Definition event.hpp:47
bool is_cursor_reporting() const
Definition event.hpp:74
static const Event ArrowRightCtrl
Definition event.hpp:45
static Event Special(std::string)
An custom event whose meaning is defined by the user of the library.
Definition event.cpp:56
static const Event ArrowRight
Definition event.hpp:40
A mouse event. It contains the coordinate of the mouse, the button pressed and the modifier (shift,...
Definition mouse.hpp:11