FTXUI  0.8.1
C++ functional terminal UI.
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 += "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 {
out += "(special)";
}
return out;
}
int main(int argc, const char* argv[]) {
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(component, [&](Event event) {
keys.push_back(event);
return true;
});
screen.Loop(component);
}
ftxui::Mouse::meta
bool meta
Definition: mouse.hpp:31
ftxui::window
Element window(Element title, Element content)
Draw window with a title and a border around the element.
Definition: border.cpp:163
ftxui::Mouse::Released
@ Released
Definition: mouse.hpp:19
screen_interactive.hpp
ftxui
Definition: captured_mouse.hpp:6
ftxui::to_string
std::string to_string(const std::wstring &s)
Convert a UTF8 std::string into a std::wstring.
Definition: string.cpp:297
ftxui::Mouse::WheelUp
@ WheelUp
Definition: mouse.hpp:14
event.hpp
ftxui::Mouse::y
int y
Definition: mouse.hpp:36
ftxui::Mouse::Right
@ Right
Definition: mouse.hpp:12
ftxui::Renderer
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:59
ftxui::Elements
std::vector< Element > Elements
Definition: elements.hpp:16
elements.hpp
captured_mouse.hpp
component.hpp
ftxui::Mouse::None
@ None
Definition: mouse.hpp:13
ftxui::Event::input
const std::string & input() const
Definition: event.hpp:71
ftxui::Mouse::button
Button button
Definition: mouse.hpp:24
ftxui::Mouse::WheelDown
@ WheelDown
Definition: mouse.hpp:15
ftxui::Event::is_character
bool is_character() const
Definition: event.hpp:59
ftxui::Mouse::Pressed
@ Pressed
Definition: mouse.hpp:20
ftxui::ScreenInteractive::TerminalOutput
static ScreenInteractive TerminalOutput()
Definition: screen_interactive.cpp:255
ftxui::Event::mouse
struct Mouse & mouse()
Definition: event.hpp:63
ftxui::vbox
Element vbox(Elements)
A container displaying elements vertically one by one.
Definition: vbox.cpp:76
ftxui::Mouse::shift
bool shift
Definition: mouse.hpp:30
ftxui::Mouse::x
int x
Definition: mouse.hpp:35
ftxui::CatchEvent
Component CatchEvent(Component child, std::function< bool(Event)>)
ftxui::Event::is_mouse
bool is_mouse() const
Definition: event.hpp:62
mouse.hpp
ftxui::Mouse::control
bool control
Definition: mouse.hpp:32
ftxui::Mouse::Middle
@ Middle
Definition: mouse.hpp:11
ftxui::Event
Represent an event. It can be key press event, a terminal resize, or more ...
Definition: event.hpp:25
ftxui::Mouse::Left
@ Left
Definition: mouse.hpp:10
ftxui::text
Element text(std::wstring text)
Display a piece of unicode text.
Definition: text.cpp:106
ftxui::Mouse::motion
Motion motion
Definition: mouse.hpp:27