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 Code(Event event) {
std::string codes;
for (auto& it : event.input()) {
codes += " " + std::to_string((unsigned int)it);
}
return codes;
}
int main() {
auto screen = ScreenInteractive::TerminalOutput();
std::vector<Event> keys;
auto left_column = Renderer([&] {
Elements children = {
text("Codes"),
separator(),
};
for (size_t i = std::max(0, (int)keys.size() - 20); i < keys.size(); ++i) {
children.push_back(text(Code(keys[i])));
}
return vbox(children);
});
auto right_column = Renderer([&] {
Elements children = {
text("Event"),
};
for (size_t i = std::max(0, (int)keys.size() - 20); i < keys.size(); ++i) {
children.push_back(text(keys[i].DebugString()));
}
return vbox(children);
});
int split_size = 40;
auto component = ResizableSplitLeft(left_column, right_column, &split_size);
component |= border;
component |= CatchEvent([&](Event event) {
keys.push_back(event);
return false;
});
screen.Loop(component);
}
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:61
Element text(std::wstring text)
Display a piece of unicode text.
Definition text.cpp:119
std::vector< Element > Elements
Definition elements.hpp:23
Element separator()
Draw a vertical or horizontal separation in between two other elements.
Component ResizableSplitLeft(Component main, Component back, int *main_size)
An horizontal split in between two components, configurable using the mouse.
Element border(Element)
Draw a border around the element.
Definition border.cpp:228
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:27