FTXUI/examples/util/print_key_press.cpp

49 lines
1.2 KiB
C++
Raw 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.
#include <chrono>
2020-05-25 07:34:13 +08:00
#include <ftxui/component/component.hpp>
#include <ftxui/component/screen_interactive.hpp>
#include <ftxui/screen/string.hpp>
#include <iostream>
#include <thread>
using namespace ftxui;
class DrawKey : public Component {
public:
2019-01-13 01:24:46 +08:00
~DrawKey() override = default;
Element Render() override {
2019-01-13 01:24:46 +08:00
Elements children;
for (size_t i = std::max(0, (int)keys.size() - 10); i < keys.size(); ++i) {
std::wstring code;
2020-03-27 08:42:46 +08:00
for (auto& it : keys[i].input())
code += L" " + std::to_wstring((unsigned int)it);
2019-07-03 05:09:20 +08:00
code = L"(" + code + L" ) -> ";
if (keys[i].is_character())
2020-03-27 08:42:46 +08:00
code += keys[i].character();
2019-07-03 05:09:20 +08:00
else
2020-03-27 08:42:46 +08:00
code += L"(special)";
children.push_back(text(code));
}
2020-05-21 02:50:40 +08:00
return window(text(L"keys"), vbox(std::move(children)));
}
bool OnEvent(Event event) override {
2018-10-19 04:58:38 +08:00
keys.push_back(event);
return true;
}
private:
std::vector<Event> keys;
};
int main(int argc, const char* argv[]) {
2020-05-21 02:50:40 +08:00
auto screen = ScreenInteractive::TerminalOutput();
2019-01-13 01:24:46 +08:00
DrawKey draw_key;
screen.Loop(&draw_key);
}