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
12
#include "
cpp-terminal/private/unicode.hpp
"
13
14
// ------------------------- Key ---------------------------
15
16
void
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
89
std::string
Term::Key::name
()
const
90
{
91
std::string str;
92
this->append_name(str);
93
return
str;
94
}
95
96
std::string
Term::Key::str
()
const
{
return
Term::Private::utf32_to_utf8
(
static_cast<
char32_t
>
(this->value)); }
Term::Key
Definition
key.hpp:86
Term::Key::F12
@ F12
Definition
key.hpp:259
Term::Key::ArrowRight
@ ArrowRight
Definition
key.hpp:239
Term::Key::F21
@ F21
Definition
key.hpp:268
Term::Key::F4
@ F4
Definition
key.hpp:251
Term::Key::F19
@ F19
Definition
key.hpp:266
Term::Key::F23
@ F23
Definition
key.hpp:270
Term::Key::F2
@ F2
Definition
key.hpp:249
Term::Key::Home
@ Home
Definition
key.hpp:243
Term::Key::F10
@ F10
Definition
key.hpp:257
Term::Key::F5
@ F5
Definition
key.hpp:252
Term::Key::F22
@ F22
Definition
key.hpp:269
Term::Key::Tab
@ Tab
Definition
key.hpp:230
Term::Key::F11
@ F11
Definition
key.hpp:258
Term::Key::F8
@ F8
Definition
key.hpp:255
Term::Key::F15
@ F15
Definition
key.hpp:262
Term::Key::ArrowLeft
@ ArrowLeft
Definition
key.hpp:238
Term::Key::PageDown
@ PageDown
Definition
key.hpp:247
Term::Key::PageUp
@ PageUp
Definition
key.hpp:246
Term::Key::End
@ End
Definition
key.hpp:245
Term::Key::F7
@ F7
Definition
key.hpp:254
Term::Key::PrintScreen
@ PrintScreen
Definition
key.hpp:272
Term::Key::Enter
@ Enter
Definition
key.hpp:231
Term::Key::F20
@ F20
Definition
key.hpp:267
Term::Key::F18
@ F18
Definition
key.hpp:265
Term::Key::ArrowUp
@ ArrowUp
Definition
key.hpp:240
Term::Key::F14
@ F14
Definition
key.hpp:261
Term::Key::F3
@ F3
Definition
key.hpp:250
Term::Key::F16
@ F16
Definition
key.hpp:263
Term::Key::F6
@ F6
Definition
key.hpp:253
Term::Key::Backspace
@ Backspace
Definition
key.hpp:229
Term::Key::NoKey
@ NoKey
Definition
key.hpp:92
Term::Key::Menu
@ Menu
Definition
key.hpp:273
Term::Key::Esc
@ Esc
Definition
key.hpp:232
Term::Key::F1
@ F1
Definition
key.hpp:248
Term::Key::F9
@ F9
Definition
key.hpp:256
Term::Key::F13
@ F13
Definition
key.hpp:260
Term::Key::F17
@ F17
Definition
key.hpp:264
Term::Key::ArrowDown
@ ArrowDown
Definition
key.hpp:241
Term::Key::Numeric5
@ Numeric5
Definition
key.hpp:242
Term::Key::Space
@ Space
Definition
key.hpp:128
Term::Key::Del
@ Del
Definition
key.hpp:233
Term::Key::F24
@ F24
Definition
key.hpp:271
Term::Key::Insert
@ Insert
Definition
key.hpp:244
Term::Key::name
std::string name() const
Definition
key.cpp:89
Term::Key::append_name
void append_name(std::string &strOut) const
Definition
key.cpp:16
Term::Key::hasCtrl
constexpr bool hasCtrl() const
Definition
key.hpp:403
Term::Key::hasAlt
constexpr bool hasAlt() const
Definition
key.hpp:410
Term::Key::iscntrl
constexpr bool iscntrl() const
Definition
key.hpp:377
Term::Key::str
std::string str() const
Definition
key.cpp:96
Term::Key::value
std::int32_t value
Definition
key.hpp:421
Term::Key::isunicode
constexpr bool isunicode() const
Definition
key.hpp:389
Term::MetaKey::Value::Ctrl
@ Ctrl
Term::MetaKey::Value::Alt
@ Alt
key.hpp
Term::Private::utf32_to_utf8
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
unicode.hpp
cpp-terminal
key.cpp
Generated by
1.11.0