FTXUI  5.0.0
C++ functional terminal UI.
event.cpp
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 #include <utility> // for move
5 
7 #include "ftxui/component/mouse.hpp" // for Mouse
8 #include "ftxui/screen/string.hpp" // for to_wstring
9 
10 namespace ftxui {
11 
12 /// @brief An event corresponding to a given typed character.
13 /// @param input The character typed by the user.
14 /// @ingroup component
15 // static
16 Event Event::Character(std::string input) {
17  Event event;
18  event.input_ = std::move(input);
19  event.type_ = Type::Character;
20  return event;
21 }
22 
23 /// @brief An event corresponding to a given typed character.
24 /// @param c The character typed by the user.
25 /// @ingroup component
26 // static
28  return Event::Character(std::string{c});
29 }
30 
31 /// @brief An event corresponding to a given typed character.
32 /// @param c The character typed by the user.
33 /// @ingroup component
34 // static
35 Event Event::Character(wchar_t c) {
36  return Event::Character(to_string(std::wstring{c}));
37 }
38 
39 /// @brief An event corresponding to a given typed character.
40 /// @param input The sequence of character send by the terminal.
41 /// @param mouse The mouse state.
42 /// @ingroup component
43 // static
44 Event Event::Mouse(std::string input, struct Mouse mouse) {
45  Event event;
46  event.input_ = std::move(input);
47  event.type_ = Type::Mouse;
48  event.data_.mouse = mouse; // NOLINT
49  return event;
50 }
51 
52 /// @brief An event corresponding to a terminal DCS (Device Control String).
53 // static
54 Event Event::CursorShape(std::string input, int shape) {
55  Event event;
56  event.input_ = std::move(input);
57  event.type_ = Type::CursorShape;
58  event.data_.cursor_shape = shape; // NOLINT
59  return event;
60 }
61 
62 /// @brief An custom event whose meaning is defined by the user of the library.
63 /// @param input An arbitrary sequence of character defined by the developer.
64 /// @ingroup component.
65 // static
66 Event Event::Special(std::string input) {
67  Event event;
68  event.input_ = std::move(input);
69  return event;
70 }
71 
72 /// @internal
73 // static
74 Event Event::CursorPosition(std::string input, int x, int y) {
75  Event event;
76  event.input_ = std::move(input);
77  event.type_ = Type::CursorPosition;
78  event.data_.cursor = {x, y}; // NOLINT
79  return event;
80 }
81 
82 // --- Arrow ---
83 const Event Event::ArrowLeft = Event::Special("\x1B[D"); // NOLINT
84 const Event Event::ArrowRight = Event::Special("\x1B[C"); // NOLINT
85 const Event Event::ArrowUp = Event::Special("\x1B[A"); // NOLINT
86 const Event Event::ArrowDown = Event::Special("\x1B[B"); // NOLINT
87 const Event Event::ArrowLeftCtrl = Event::Special("\x1B[1;5D"); // NOLINT
88 const Event Event::ArrowRightCtrl = Event::Special("\x1B[1;5C"); // NOLINT
89 const Event Event::ArrowUpCtrl = Event::Special("\x1B[1;5A"); // NOLINT
90 const Event Event::ArrowDownCtrl = Event::Special("\x1B[1;5B"); // NOLINT
91 const Event Event::Backspace = Event::Special({127}); // NOLINT
92 const Event Event::Delete = Event::Special("\x1B[3~"); // NOLINT
93 const Event Event::Escape = Event::Special("\x1B"); // NOLINT
94 const Event Event::Return = Event::Special({10}); // NOLINT
95 const Event Event::Tab = Event::Special({9}); // NOLINT
96 const Event Event::TabReverse = Event::Special({27, 91, 90}); // NOLINT
97 
98 // See https://invisible-island.net/xterm/xterm-function-keys.html
99 // We follow xterm-new / vterm-xf86-v4 / mgt / screen
100 const Event Event::F1 = Event::Special("\x1BOP"); // NOLINT
101 const Event Event::F2 = Event::Special("\x1BOQ"); // NOLINT
102 const Event Event::F3 = Event::Special("\x1BOR"); // NOLINT
103 const Event Event::F4 = Event::Special("\x1BOS"); // NOLINT
104 const Event Event::F5 = Event::Special("\x1B[15~"); // NOLINT
105 const Event Event::F6 = Event::Special("\x1B[17~"); // NOLINT
106 const Event Event::F7 = Event::Special("\x1B[18~"); // NOLINT
107 const Event Event::F8 = Event::Special("\x1B[19~"); // NOLINT
108 const Event Event::F9 = Event::Special("\x1B[20~"); // NOLINT
109 const Event Event::F10 = Event::Special("\x1B[21~"); // NOLINT
110 const Event Event::F11 = Event::Special("\x1B[23~"); // NOLINT
111 const Event Event::F12 = Event::Special("\x1B[24~"); // NOLINT
112 
113 const Event Event::Insert = Event::Special("\x1B[2~"); // NOLINT
114 const Event Event::Home = Event::Special({27, 91, 72}); // NOLINT
115 const Event Event::End = Event::Special({27, 91, 70}); // NOLINT
116 const Event Event::PageUp = Event::Special({27, 91, 53, 126}); // NOLINT
117 const Event Event::PageDown = Event::Special({27, 91, 54, 126}); // NOLINT
118 const Event Event::Custom = Event::Special({0}); // NOLINT
119 
120 } // namespace ftxui
std::string to_string(const std::wstring &s)
Convert a UTF8 std::string into a std::wstring.
Definition: string.cpp:1565
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:56
static const Event ArrowLeftCtrl
Definition: event.hpp:45
static Event CursorShape(std::string, int shape)
An event corresponding to a terminal DCS (Device Control String).
Definition: event.cpp:54
static const Event PageUp
Definition: event.hpp:63
static const Event Escape
Definition: event.hpp:54
static const Event F12
Definition: event.hpp:57
static Event Mouse(std::string, Mouse mouse)
An event corresponding to a given typed character.
Definition: event.cpp:44
const std::string & input() const
Definition: event.hpp:73
static const Event F5
Definition: event.hpp:57
static const Event F3
Definition: event.hpp:57
static const Event F9
Definition: event.hpp:57
static const Event Custom
Definition: event.hpp:67
static Event Character(std::string)
An event corresponding to a given typed character.
Definition: event.cpp:16
static const Event F2
Definition: event.hpp:57
static const Event Backspace
Definition: event.hpp:51
static const Event F7
Definition: event.hpp:57
static const Event ArrowUp
Definition: event.hpp:42
static const Event Tab
Definition: event.hpp:55
static const Event ArrowDown
Definition: event.hpp:43
static const Event End
Definition: event.hpp:61
static const Event F11
Definition: event.hpp:57
static const Event Home
Definition: event.hpp:60
static const Event F8
Definition: event.hpp:57
static const Event F4
Definition: event.hpp:57
static const Event ArrowUpCtrl
Definition: event.hpp:47
struct Mouse & mouse()
Definition: event.hpp:79
static const Event F10
Definition: event.hpp:57
static const Event PageDown
Definition: event.hpp:64
static Event CursorPosition(std::string, int x, int y)
Definition: event.cpp:74
static const Event F6
Definition: event.hpp:57
static const Event F1
Definition: event.hpp:57
static const Event Return
Definition: event.hpp:53
static const Event ArrowLeft
Definition: event.hpp:40
static const Event Delete
Definition: event.hpp:52
static const Event ArrowDownCtrl
Definition: event.hpp:48
static const Event Insert
Definition: event.hpp:59
static const Event ArrowRightCtrl
Definition: event.hpp:46
static Event Special(std::string)
An custom event whose meaning is defined by the user of the library.
Definition: event.cpp:66
static const Event ArrowRight
Definition: event.hpp:41
A mouse event. It contains the coordinate of the mouse, the button pressed and the modifier (shift,...
Definition: mouse.hpp:11