FTXUI/examples/component/print_key_press.cpp

67 lines
2.0 KiB
C++
Raw Permalink Normal View History

2020-04-20 03:00:37 +08:00
// 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.
2021-05-23 18:53:20 +08:00
#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
2021-05-10 02:32:27 +08:00
#include <utility> // for move
#include <vector> // for vector
2021-05-02 02:40:35 +08:00
2021-05-10 02:32:27 +08:00
#include "ftxui/component/captured_mouse.hpp" // for ftxui
2021-05-23 18:53:20 +08:00
#include "ftxui/component/component.hpp" // for CatchEvent, Renderer
2021-05-10 02:32:27 +08:00
#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
2021-05-23 18:53:20 +08:00
#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);
2021-04-25 21:22:38 +08:00
}
return codes;
2021-04-25 21:22:38 +08:00
}
int main() {
2021-05-23 18:53:20 +08:00
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);
2021-05-23 18:53:20 +08:00
});
auto right_column = Renderer([&] {
Elements children = {
text("Event"),
separator(),
};
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) {
2018-10-19 04:58:38 +08:00
keys.push_back(event);
return false;
2021-05-23 18:53:20 +08:00
});
2021-05-23 18:53:20 +08:00
screen.Loop(component);
}