cpp-terminal 1.0.0
Small C++ library for writing multiplatform terminal applications
Loading...
Searching...
No Matches
key.cpp
Go to the documentation of this file.
1/*
2* cpp-terminal
3* C++ library for writing multi-platform terminal applications.
4*
5* SPDX-FileCopyrightText: 2019-2024 cpp-terminal
6*
7* SPDX-License-Identifier: MIT
8*/
9
10#include "cpp-terminal/key.hpp"
11
13
14// ------------------------- Key ---------------------------
15
16void Term::Key::append_name(std::string& strOut) const
17{
18 Term::Key key = *this;
19 if(key == Term::Key::NoKey) return;
20 if(key.hasAlt())
21 {
22 strOut += "Alt+";
23 key = static_cast<Term::Key>(key.value - static_cast<std::int32_t>(Term::MetaKey::Value::Alt));
24 }
25 if(key.hasCtrl())
26 {
27 strOut += "Ctrl+";
28 if(!key.iscntrl()) key = static_cast<Term::Key>(key.value - static_cast<std::int32_t>(Term::MetaKey::Value::Ctrl));
29 }
30 if(key == Term::Key::Tab) strOut += "Tab";
31 else if(key == Term::Key::Enter)
32 strOut += "Enter";
33 else if(key == Term::Key::Esc)
34 strOut += "Esc";
35 else if(key == Term::Key::Backspace)
36 strOut += "Backspace";
37 else if(key == Term::Key::Del)
38 strOut += "Del";
39 else if(key.iscntrl())
40 strOut += static_cast<char>(key.value + 64);
41 else if(key == Term::Key::Space)
42 strOut += "Space";
43 else if(key.isunicode()) { strOut += Term::Private::utf32_to_utf8(static_cast<char32_t>(this->value)); }
44 else
45 {
46 switch(key)
47 {
48 case Term::Key::ArrowLeft: strOut += "Left Arrow"; break;
49 case Term::Key::ArrowRight: strOut += "Right Arrow"; break;
50 case Term::Key::ArrowUp: strOut += "Up arrow"; break;
51 case Term::Key::ArrowDown: strOut += "Down arrow"; break;
52 case Term::Key::Numeric5: strOut += "5 Numeric pad"; break;
53 case Term::Key::Home: strOut += "Home"; break;
54 case Term::Key::Insert: strOut += "Insert"; break;
55 case Term::Key::End: strOut += "End"; break;
56 case Term::Key::PageUp: strOut += "Page up"; break;
57 case Term::Key::PageDown: strOut += "Page down"; break;
58 case Term::Key::F1: strOut += "F1"; break;
59 case Term::Key::F2: strOut += "F2"; break;
60 case Term::Key::F3: strOut += "F3"; break;
61 case Term::Key::F4: strOut += "F4"; break;
62 case Term::Key::F5: strOut += "F5"; break;
63 case Term::Key::F6: strOut += "F6"; break;
64 case Term::Key::F7: strOut += "F7"; break;
65 case Term::Key::F8: strOut += "F8"; break;
66 case Term::Key::F9: strOut += "F9"; break;
67 case Term::Key::F10: strOut += "F10"; break;
68 case Term::Key::F11: strOut += "F11"; break;
69 case Term::Key::F12: strOut += "F12"; break;
70 case Term::Key::F13: strOut += "F13"; break;
71 case Term::Key::F14: strOut += "F14"; break;
72 case Term::Key::F15: strOut += "F15"; break;
73 case Term::Key::F16: strOut += "F16"; break;
74 case Term::Key::F17: strOut += "F17"; break;
75 case Term::Key::F18: strOut += "F18"; break;
76 case Term::Key::F19: strOut += "F19"; break;
77 case Term::Key::F20: strOut += "F20"; break;
78 case Term::Key::F21: strOut += "F21"; break;
79 case Term::Key::F22: strOut += "F22"; break;
80 case Term::Key::F23: strOut += "F23"; break;
81 case Term::Key::F24: strOut += "F24"; break;
82 case Term::Key::PrintScreen: strOut += "Print Screen"; break;
83 case Term::Key::Menu: strOut += "Menu"; break;
84 default: break;
85 }
86 }
87}
88
89std::string Term::Key::name() const
90{
91 std::string str;
92 this->append_name(str);
93 return str;
94}
95
96std::string Term::Key::str() const { return Term::Private::utf32_to_utf8(static_cast<char32_t>(this->value)); }
@ ArrowRight
Definition key.hpp:239
@ ArrowLeft
Definition key.hpp:238
@ PageDown
Definition key.hpp:247
@ PageUp
Definition key.hpp:246
@ PrintScreen
Definition key.hpp:272
@ Enter
Definition key.hpp:231
@ ArrowUp
Definition key.hpp:240
@ Backspace
Definition key.hpp:229
@ NoKey
Definition key.hpp:92
@ ArrowDown
Definition key.hpp:241
@ Numeric5
Definition key.hpp:242
@ Space
Definition key.hpp:128
@ Insert
Definition key.hpp:244
std::string name() const
Definition key.cpp:89
void append_name(std::string &strOut) const
Definition key.cpp:16
constexpr bool hasCtrl() const
Definition key.hpp:403
constexpr bool hasAlt() const
Definition key.hpp:410
constexpr bool iscntrl() const
Definition key.hpp:377
std::string str() const
Definition key.cpp:96
std::int32_t value
Definition key.hpp:421
constexpr bool isunicode() const
Definition key.hpp:389
std::string utf32_to_utf8(const char32_t &codepoint, const bool &exception=false)
Encode a codepoint using UTF-8 std::string .
Definition unicode.cpp:55