FTXUI  5.0.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
examples/component/print_key_press.cpp
// Copyright 2020 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
#include <stddef.h> // for size_t
#include <algorithm> // for max
#include <memory> // for allocator, shared_ptr
#include <string> // for char_traits, operator+, string, basic_string, to_string
#include <utility> // for move
#include <vector> // for vector
#include "ftxui/component/captured_mouse.hpp" // for ftxui
#include "ftxui/component/component.hpp" // for CatchEvent, Renderer
#include "ftxui/component/event.hpp" // for Event
#include "ftxui/component/mouse.hpp" // for Mouse, Mouse::Left, Mouse::Middle, Mouse::None, Mouse::Pressed, Mouse::Released, Mouse::Right, Mouse::WheelDown, Mouse::WheelUp
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/dom/elements.hpp" // for text, vbox, window, Element, Elements
using namespace ftxui;
std::string Stringify(Event event) {
std::string out;
for (auto& it : event.input())
out += " " + std::to_string((unsigned int)it);
out = "(" + out + " ) -> ";
if (event.is_character()) {
out += "Event::Character(\"" + event.character() + "\")";
} else if (event.is_mouse()) {
out += "mouse";
switch (event.mouse().button) {
out += "_left";
break;
out += "_middle";
break;
out += "_right";
break;
out += "_none";
break;
out += "_wheel_up";
break;
out += "_wheel_down";
break;
}
switch (event.mouse().motion) {
out += "_pressed";
break;
out += "_released";
break;
}
if (event.mouse().control)
out += "_control";
if (event.mouse().shift)
out += "_shift";
if (event.mouse().meta)
out += "_meta";
out += "(" + //
std::to_string(event.mouse().x) + "," +
std::to_string(event.mouse().y) + ")";
} else if (event == Event::ArrowLeft) {
out += "Event::ArrowLeft";
} else if (event == Event::ArrowRight) {
out += "Event::ArrowRight";
} else if (event == Event::ArrowUp) {
out += "Event::ArrowUp";
} else if (event == Event::ArrowDown) {
out += "Event::ArrowDown";
} else if (event == Event::ArrowLeftCtrl) {
out += "Event::ArrowLeftCtrl";
} else if (event == Event ::ArrowRightCtrl) {
out += "Event::ArrowRightCtrl";
} else if (event == Event::ArrowUpCtrl) {
out += "Event::ArrowUpCtrl";
} else if (event == Event::ArrowDownCtrl) {
out += "Event::ArrowDownCtrl";
} else if (event == Event::Backspace) {
out += "Event::Backspace";
} else if (event == Event::Delete) {
out += "Event::Delete";
} else if (event == Event::Escape) {
out += "Event::Escape";
} else if (event == Event::Return) {
out += "Event::Return";
} else if (event == Event::Tab) {
out += "Event::Tab";
} else if (event == Event::TabReverse) {
out += "Event::TabReverse";
} else if (event == Event::F1) {
out += "Event::F1";
} else if (event == Event::F2) {
out += "Event::F2";
} else if (event == Event::F3) {
out += "Event::F3";
} else if (event == Event::F4) {
out += "Event::F4";
} else if (event == Event::F5) {
out += "Event::F5";
} else if (event == Event::F6) {
out += "Event::F6";
} else if (event == Event::F7) {
out += "Event::F7";
} else if (event == Event::F8) {
out += "Event::F8";
} else if (event == Event::F9) {
out += "Event::F9";
} else if (event == Event::F10) {
out += "Event::F10";
} else if (event == Event::F11) {
out += "Event::F11";
} else if (event == Event::F12) {
out += "Event::F12";
} else if (event == Event::Home) {
out += "Event::Home";
} else if (event == Event::End) {
out += "Event::End";
} else if (event == Event::PageUp) {
out += "Event::PageUp";
} else if (event == Event::PageDown) {
out += "Event::PageDown";
} else if (event == Event::Custom) {
out += "Custom";
} else {
out += "(special)";
}
return out;
}
int main() {
std::vector<Event> keys;
auto component = Renderer([&] {
Elements children;
for (size_t i = std::max(0, (int)keys.size() - 20); i < keys.size(); ++i)
children.push_back(text(Stringify(keys[i])));
return window(text("keys"), vbox(std::move(children)));
});
component |= CatchEvent([&](Event event) {
keys.push_back(event);
return true;
});
screen.Loop(component);
}
static ScreenInteractive TerminalOutput()
Element window(Element title, Element content)
Draw window with a title and a border around the element.
Definition border.cpp:500
Component Renderer(Component child, std::function< Element()>)
Return a new Component, similar to |child|, but using |render| as the Component::Render() event.
Definition renderer.cpp:63
std::vector< Element > Elements
Definition elements.hpp:24
Element text(std::wstring text)
Display a piece of unicode text.
Definition text.cpp:120
Component CatchEvent(Component child, std::function< bool(Event)>)
Element vbox(Elements)
A container displaying elements vertically one by one.
Definition vbox.cpp:83
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
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
static const Event Custom
Definition event.hpp:65
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
static const Event ArrowLeft
Definition event.hpp:39
bool is_character() const
Definition event.hpp:68
static const Event Delete
Definition event.hpp:51
static const Event ArrowDownCtrl
Definition event.hpp:47
static const Event ArrowRight
Definition event.hpp:40
bool meta
Definition mouse.hpp:34
Button button
Definition mouse.hpp:27
bool shift
Definition mouse.hpp:33
bool control
Definition mouse.hpp:35
Motion motion
Definition mouse.hpp:30