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
|
2021-08-09 05:25:20 +08:00
|
|
|
#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
|
2018-10-10 01:06:03 +08:00
|
|
|
|
2019-01-12 22:00:08 +08:00
|
|
|
using namespace ftxui;
|
2019-01-07 00:10:35 +08:00
|
|
|
|
2021-08-09 05:25:20 +08:00
|
|
|
std::string Stringify(Event event) {
|
|
|
|
std::string out;
|
2021-04-25 21:22:38 +08:00
|
|
|
for (auto& it : event.input())
|
2021-08-09 05:25:20 +08:00
|
|
|
out += " " + std::to_string((unsigned int)it);
|
2021-04-25 21:22:38 +08:00
|
|
|
|
2021-08-09 05:25:20 +08:00
|
|
|
out = "(" + out + " ) -> ";
|
2021-04-25 21:22:38 +08:00
|
|
|
if (event.is_character()) {
|
2022-10-19 04:58:22 +08:00
|
|
|
out += "Event::Character(\"" + event.character() + "\")";
|
2021-04-25 21:22:38 +08:00
|
|
|
} else if (event.is_mouse()) {
|
2021-08-09 05:25:20 +08:00
|
|
|
out += "mouse";
|
2021-04-25 21:22:38 +08:00
|
|
|
switch (event.mouse().button) {
|
|
|
|
case Mouse::Left:
|
2021-08-09 05:25:20 +08:00
|
|
|
out += "_left";
|
2021-04-25 21:22:38 +08:00
|
|
|
break;
|
|
|
|
case Mouse::Middle:
|
2021-08-09 05:25:20 +08:00
|
|
|
out += "_middle";
|
2021-04-25 21:22:38 +08:00
|
|
|
break;
|
|
|
|
case Mouse::Right:
|
2021-08-09 05:25:20 +08:00
|
|
|
out += "_right";
|
2021-04-25 21:22:38 +08:00
|
|
|
break;
|
|
|
|
case Mouse::None:
|
2021-08-09 05:25:20 +08:00
|
|
|
out += "_none";
|
2021-04-25 21:22:38 +08:00
|
|
|
break;
|
|
|
|
case Mouse::WheelUp:
|
2021-08-09 05:25:20 +08:00
|
|
|
out += "_wheel_up";
|
2021-04-25 21:22:38 +08:00
|
|
|
break;
|
|
|
|
case Mouse::WheelDown:
|
2021-08-09 05:25:20 +08:00
|
|
|
out += "_wheel_down";
|
2021-04-25 21:22:38 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (event.mouse().motion) {
|
|
|
|
case Mouse::Pressed:
|
2021-08-09 05:25:20 +08:00
|
|
|
out += "_pressed";
|
2021-04-25 21:22:38 +08:00
|
|
|
break;
|
|
|
|
case Mouse::Released:
|
2021-08-09 05:25:20 +08:00
|
|
|
out += "_released";
|
2021-04-25 21:22:38 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (event.mouse().control)
|
2021-08-09 05:25:20 +08:00
|
|
|
out += "_control";
|
2021-04-25 21:22:38 +08:00
|
|
|
if (event.mouse().shift)
|
2021-08-09 05:25:20 +08:00
|
|
|
out += "_shift";
|
2021-04-25 21:22:38 +08:00
|
|
|
if (event.mouse().meta)
|
2021-08-09 05:25:20 +08:00
|
|
|
out += "_meta";
|
2021-04-25 21:22:38 +08:00
|
|
|
|
2021-08-09 05:25:20 +08:00
|
|
|
out += "(" + //
|
|
|
|
std::to_string(event.mouse().x) + "," +
|
|
|
|
std::to_string(event.mouse().y) + ")";
|
2022-10-19 04:58:22 +08:00
|
|
|
} 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";
|
2021-04-25 21:22:38 +08:00
|
|
|
} else {
|
2021-08-09 05:25:20 +08:00
|
|
|
out += "(special)";
|
2021-04-25 21:22:38 +08:00
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2023-06-03 19:59:39 +08:00
|
|
|
int main() {
|
2021-05-23 18:53:20 +08:00
|
|
|
auto screen = ScreenInteractive::TerminalOutput();
|
|
|
|
|
|
|
|
std::vector<Event> keys;
|
2018-10-10 01:06:03 +08:00
|
|
|
|
2021-05-23 18:53:20 +08:00
|
|
|
auto component = Renderer([&] {
|
2019-01-13 01:24:46 +08:00
|
|
|
Elements children;
|
2021-05-23 18:53:20 +08:00
|
|
|
for (size_t i = std::max(0, (int)keys.size() - 20); i < keys.size(); ++i)
|
2021-04-25 21:22:38 +08:00
|
|
|
children.push_back(text(Stringify(keys[i])));
|
2021-08-09 05:25:20 +08:00
|
|
|
return window(text("keys"), vbox(std::move(children)));
|
2021-05-23 18:53:20 +08:00
|
|
|
});
|
2018-10-10 01:06:03 +08:00
|
|
|
|
2022-03-12 22:18:36 +08:00
|
|
|
component |= CatchEvent([&](Event event) {
|
2018-10-19 04:58:38 +08:00
|
|
|
keys.push_back(event);
|
2018-10-10 01:06:03 +08:00
|
|
|
return true;
|
2021-05-23 18:53:20 +08:00
|
|
|
});
|
2018-10-10 01:06:03 +08:00
|
|
|
|
2021-05-23 18:53:20 +08:00
|
|
|
screen.Loop(component);
|
2018-10-10 01:06:03 +08:00
|
|
|
}
|