Skip to content

Commit 9496cc8

Browse files
authored
Merge pull request #181 from flagarde/fixes
fixed input on wndows
2 parents be2a27a + abe9a26 commit 9496cc8

File tree

10 files changed

+232
-55
lines changed

10 files changed

+232
-55
lines changed

cpp-terminal/base.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,9 @@ void Term::get_cursor_position(int& rows, int& cols) {
122122

123123
Term::Terminal::Terminal(bool _clear_screen,
124124
bool enable_keyboard,
125-
bool disable_ctrl_c,
125+
bool disable_signal_keys,
126126
bool _hide_cursor)
127-
: BaseTerminal(enable_keyboard, disable_ctrl_c),
127+
: BaseTerminal(enable_keyboard, disable_signal_keys),
128128
clear_screen{_clear_screen},
129129
hide_cursor{_hide_cursor} {
130130
if (clear_screen) {

cpp-terminal/base.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class Terminal : public Private::BaseTerminal {
114114
public:
115115
Terminal(bool _clear_screen,
116116
bool enable_keyboard,
117-
bool disable_ctrl_c,
117+
bool disable_signal_keys,
118118
bool _hide_cursor);
119119
// providing no parameters will disable the keyboard and ctrl+c
120120
explicit Terminal(bool _clear_screen);

cpp-terminal/input.cpp

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,52 @@
44
#include <thread>
55
#include "private/platform.hpp"
66

7-
int Term::read_key() {
8-
int key{};
9-
while ((key = read_key0()) == 0) {
7+
bool Term::is_ASCII(const Term::Key& key) {
8+
if (key >= 0 && key <= 127)
9+
return true;
10+
else
11+
return false;
12+
}
13+
14+
bool Term::is_extended_ASCII(const Term::Key& key) {
15+
if (key >= 0 && key <= 255)
16+
return true;
17+
else
18+
return false;
19+
}
20+
21+
bool Term::is_CTRL(const Term::Key& key) {
22+
// Need to supress the TAB etc...
23+
if (key > 0 && key <= 31 && key != Key::BACKSPACE && key != Key::TAB &&
24+
key != ESC && /* the two mapped to ENTER */ key != Key::LF && key != CR)
25+
return true;
26+
else
27+
return false;
28+
}
29+
30+
bool Term::is_ALT(const Term::Key& key) {
31+
if ((key & Key::ALT) == Key::ALT)
32+
return true;
33+
else
34+
return false;
35+
}
36+
37+
std::int32_t Term::read_key() {
38+
std::int32_t key{Key::NO_KEY};
39+
while ((key = read_key0()) == Key::NO_KEY) {
1040
std::this_thread::sleep_for(std::chrono::milliseconds(10));
1141
}
1242
return key;
1343
}
1444

15-
int Term::read_key0() {
16-
char c{};
45+
std::int32_t Term::read_key0() {
46+
char c{'\0'};
1747
if (!Private::read_raw(&c))
18-
return 0;
19-
20-
if (c == '\x1b') {
21-
char seq[4];
48+
return Key::NO_KEY;
49+
if (is_CTRL(static_cast<Term::Key>(c))) {
50+
return c;
51+
} else if (c == Key::ESC) {
52+
char seq[4]{'\0', '\0', '\0', '\0'};
2253

2354
if (!Private::read_raw(&seq[0]))
2455
return Key::ESC;
@@ -152,13 +183,11 @@ int Term::read_key0() {
152183
return -4;
153184
} else {
154185
switch (c) {
155-
case '\x09': // TAB
156-
return Key::TAB;
157-
case '\x0a': // LF; falls-through
158-
case '\x0d': // CR
159-
return Key::ENTER;
160-
case '\x7f': // DEL
186+
case Key::DEL:
161187
return Key::BACKSPACE;
188+
case Key::LF:
189+
case Key::CR:
190+
return Key::ENTER;
162191
}
163192
if (c == '\xc3') {
164193
if (!Private::read_raw(&c)) {
@@ -202,4 +231,4 @@ std::string Term::read_stdin_alone() {
202231
// temporarily enable raw mode
203232
Term::Terminal term(false, true, false, false);
204233
return Term::read_stdin();
205-
}
234+
}

cpp-terminal/input.hpp

Lines changed: 158 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,146 @@
11
#pragma once
2+
#include <cstdint>
23
#include <string>
34

45
namespace Term {
5-
enum Key {
6-
BACKSPACE = 1000,
7-
ENTER,
8-
ALT_ENTER,
9-
TAB,
6+
enum Key : std::int32_t {
7+
NO_KEY = -1,
8+
// Begin ASCII (some ASCII names has been change to their CTRL + key part)
9+
NUL = 0,
10+
CTRL_A = 1,
11+
CTRL_B = 2,
12+
CTRL_C = 3,
13+
CTRL_D = 4,
14+
CTRL_E = 5,
15+
CTRL_F = 6,
16+
CTRL_G = 7,
17+
BACKSPACE = 8,
18+
TAB = 9,
19+
ENTER = 10,
20+
LF = 10,
21+
CTRL_K = 11,
22+
CTRL_L = 12,
23+
CR = 13, // MAPED to ENTER
24+
CTRL_N = 14,
25+
CTRL_O = 15,
26+
CTRL_P = 16,
27+
CTRL_Q = 17,
28+
CTRL_R = 18,
29+
CTRL_S = 19,
30+
CTRL_T = 20,
31+
CTRL_U = 21,
32+
CTRL_V = 22,
33+
CTRL_W = 23,
34+
CTRL_X = 24,
35+
CTRL_Y = 25,
36+
CTRL_Z = 26,
37+
ESC = 27,
38+
CTRL_SLASH = 28,
39+
CTRL_CLOSE_BRACKET = 29,
40+
CTRL_CARET = 30,
41+
CTRL_UNDERSCORE = 31,
42+
SPACE = 32,
43+
EXCLAMATION_MARK = 33,
44+
QUOTE = 34,
45+
HASH = 35,
46+
DOLLAR = 36,
47+
PERCENT = 37,
48+
AMPERSAND = 38,
49+
APOSTROPHE = 39,
50+
OPEN_PARENTHESIS = 40,
51+
CLOSE_PARENTHESIS = 41,
52+
ASTERISK = 42,
53+
PLUS = 43,
54+
COMMA = 44,
55+
HYPHEN = 45,
56+
MINUS = 45,
57+
PERIOD = 46,
58+
SLASH = 47,
59+
ZERO = 48,
60+
ONE = 49,
61+
TWO = 50,
62+
THREE = 51,
63+
FOUR = 52,
64+
FIVE = 53,
65+
SIX = 54,
66+
SEVEN = 55,
67+
EIGHT = 56,
68+
NINE = 57,
69+
COLON = 58,
70+
SEMICOLON = 59,
71+
LESS_THAN = 60,
72+
OPEN_CHEVRON = 60,
73+
EQUAL = 61,
74+
GREATER_THAN = 62,
75+
CLOSE_CHEVRON = 62,
76+
QUESTION_MARK = 63,
77+
AROBASE = 64,
78+
A = 65,
79+
B = 66,
80+
C = 67,
81+
D = 68,
82+
E = 69,
83+
F = 70,
84+
G = 71,
85+
H = 72,
86+
I = 73,
87+
J = 74,
88+
K = 75,
89+
L = 76,
90+
M = 77,
91+
N = 78,
92+
O = 79,
93+
P = 80,
94+
Q = 81,
95+
R = 82,
96+
S = 83,
97+
T = 84,
98+
U = 85,
99+
V = 86,
100+
W = 87,
101+
X = 88,
102+
Y = 89,
103+
Z = 90,
104+
OPEN_BRACKET = 91,
105+
BACKSLASH = 92,
106+
CLOSE_BRACKET = 93,
107+
CARET = 94,
108+
UNDERSCORE = 95,
109+
GRAVE_ACCENT = 96,
110+
a = 97,
111+
b = 98,
112+
c = 99,
113+
d = 100,
114+
e = 101,
115+
f = 102,
116+
g = 103,
117+
h = 104,
118+
i = 105,
119+
j = 106,
120+
k = 107,
121+
l = 108,
122+
m = 109,
123+
n = 110,
124+
o = 111,
125+
p = 112,
126+
q = 113,
127+
r = 114,
128+
s = 115,
129+
t = 116,
130+
u = 117,
131+
v = 118,
132+
w = 119,
133+
x = 120,
134+
y = 121,
135+
z = 122,
136+
OPEN_BRACE = 123,
137+
VERTICAL_BAR = 124,
138+
CLOSE_BRACE = 125,
139+
TILDE = 126,
140+
DEL = 127,
141+
// End ASCII
142+
// Extended ANSII goes up to 255
143+
ALT_ENTER = 256,
10144
ARROW_LEFT,
11145
ARROW_RIGHT,
12146
ARROW_UP,
@@ -16,13 +150,11 @@ enum Key {
16150
CTRL_RIGHT,
17151
CTRL_LEFT,
18152
NUMERIC_5,
19-
DEL,
20153
HOME,
21154
INSERT,
22155
END,
23156
PAGE_UP,
24157
PAGE_DOWN,
25-
ESC,
26158
F1,
27159
F2,
28160
F3,
@@ -35,20 +167,34 @@ enum Key {
35167
F10,
36168
F11,
37169
F12,
38-
// special keys
39-
CTRL = -96,
40-
ALT = -128
170+
// Keys bellow need to be under 512
171+
// special keys (CTRL is special^2)
172+
CTRL = -AROBASE,
173+
// Now use << to for detecting special key + key press
174+
ALT = (1 << 9)
41175
};
42176

177+
// Detect if Key is convertible to ANSII
178+
bool is_ASCII(const Key&);
179+
180+
// Detect if Key is convertible to Extended ANSII
181+
bool is_extended_ASCII(const Key&);
182+
183+
// Detect if Key is CTRL+*
184+
bool is_CTRL(const Key&);
185+
186+
// Detecti if Key is ALT+*
187+
bool is_ALT(const Key&);
188+
43189
// Waits for a key press, translates escape codes
44190
// if Term:Terminal is not enabling the keyboard it'll loop for infinity
45-
int read_key();
191+
std::int32_t read_key();
46192

47193
// If there was a key press, returns the translated key from escape codes,
48194
// otherwise returns 0. If the escape code is not supported it returns a
49195
// negative number.
50196
// if Term::Terminal is not enabling the keyboard it'll always return 0
51-
int read_key0();
197+
std::int32_t read_key0();
52198

53199
// returns the stdin as a string
54200
// waits until the EOT signal is send

cpp-terminal/private/platform.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ Term::Private::BaseTerminal::~BaseTerminal() noexcept(false) {
176176

177177
#ifdef _WIN32
178178
Term::Private::BaseTerminal::BaseTerminal(bool enable_keyboard,
179-
bool /*disable_ctrl_c*/)
179+
bool disable_signal_keys)
180180
: keyboard_enabled{enable_keyboard} {
181181
// silently disable raw mode for non-tty
182182
if (keyboard_enabled)
@@ -216,14 +216,17 @@ Term::Private::BaseTerminal::BaseTerminal(bool enable_keyboard,
216216
if (has_ansi_escape_code()) {
217217
flags |= ENABLE_VIRTUAL_TERMINAL_INPUT;
218218
}
219+
if (disable_signal_keys) {
220+
flags &= ~ENABLE_PROCESSED_INPUT;
221+
}
219222
flags &= ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT);
220223
if (!SetConsoleMode(hin, flags)) {
221224
throw std::runtime_error("SetConsoleMode() failed");
222225
}
223226
}
224227
#else
225228
Term::Private::BaseTerminal::BaseTerminal(bool enable_keyboard,
226-
bool disable_ctrl_c)
229+
bool disable_signal_keys)
227230
: orig_termios{std::make_unique<termios>()},
228231
keyboard_enabled{enable_keyboard} {
229232
// silently disable raw mode for non-tty
@@ -246,7 +249,7 @@ Term::Private::BaseTerminal::BaseTerminal(bool enable_keyboard,
246249
// raw.c_oflag &= ~(OPOST);
247250
raw.c_cflag |= (CS8);
248251
raw.c_lflag &= ~(ECHO | ICANON | IEXTEN);
249-
if (disable_ctrl_c) {
252+
if (disable_signal_keys) {
250253
raw.c_lflag &= ~(ISIG);
251254
}
252255
raw.c_cc[VMIN] = 0;

cpp-terminal/private/platform.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class BaseTerminal {
6161

6262
public:
6363
explicit BaseTerminal(bool enable_keyboard = false,
64-
bool disable_ctrl_c = true);
64+
bool disable_signal_keys = true);
6565

6666
virtual ~BaseTerminal() noexcept(false);
6767
};

0 commit comments

Comments
 (0)