Console Library 4.7.0
A header-only library that makes C++ simple
Loading...
Searching...
No Matches
kb.h
Go to the documentation of this file.
1
10
11/*
12Copyright (c) 2026 MrXie1109
13
14Permission is hereby granted, free of charge, to any person obtaining a copy
15of this software and associated documentation files (the "Software"), to deal
16in the Software without restriction, including without limitation the rights
17to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18copies of the Software, and to permit persons to whom the Software is
19furnished to do so, subject to the following conditions:
20
21The above copyright notice and this permission notice shall be included in all
22copies or substantial portions of the Software.
23
24THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30SOFTWARE.
31*/
32
33#pragma once
34#ifdef _WIN32
35#include <windows.h>
36#include <conio.h>
37#else
38#include <termios.h>
39#include <unistd.h>
40#include <poll.h>
41#endif
42
43namespace console
44{
116
122 {
123 public:
129 {
130#ifdef _WIN32
131 hStdin = GetStdHandle(STD_INPUT_HANDLE);
132 GetConsoleMode(hStdin, &oldMode);
133 SetConsoleMode(hStdin, oldMode & ~(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT));
134#else
135 tcgetattr(STDIN_FILENO, &oldTio);
136 struct termios newTio = oldTio;
137 newTio.c_lflag &= ~(ICANON | ECHO);
138 newTio.c_cc[VMIN] = 0;
139 newTio.c_cc[VTIME] = 0;
140 tcsetattr(STDIN_FILENO, TCSANOW, &newTio);
141#endif
142 }
143
149 {
150#ifdef _WIN32
151 SetConsoleMode(hStdin, oldMode);
152#else
153 tcsetattr(STDIN_FILENO, TCSANOW, &oldTio);
154#endif
155 }
156
158 Keyboard(const Keyboard &) = delete;
159
161 Keyboard(Keyboard &&) = delete;
162
164 Keyboard &operator=(const Keyboard &) = delete;
165
168
174 {
175 int raw = readRaw();
176 if (raw == -1)
177 return Key::None;
178 return parse(raw);
179 }
180
186 {
187 while (true)
188 {
189 int raw = readRawBlock();
190 if (raw != -1)
191 return parse(raw);
192 }
193 }
194
195 private:
196#ifdef _WIN32
197 HANDLE hStdin;
198 DWORD oldMode;
199#else
200 struct termios oldTio;
201#endif
202
209 int readRaw()
210 {
211#ifdef _WIN32
212 if (_kbhit())
213 return _getch();
214 return -1;
215#else
216 struct pollfd pfd = {STDIN_FILENO, POLLIN, 0};
217 if (poll(&pfd, 1, 0) <= 0)
218 return -1;
219 unsigned char c;
220 if (read(STDIN_FILENO, &c, 1) != 1)
221 return -1;
222 return c;
223#endif
224 }
225
232 int readRawBlock()
233 {
234#ifdef _WIN32
235 return _getch();
236#else
237 unsigned char c;
238 struct pollfd pfd = {STDIN_FILENO, POLLIN, 0};
239 while (true)
240 {
241 if (poll(&pfd, 1, -1) > 0)
242 {
243 if (read(STDIN_FILENO, &c, 1) == 1)
244 return c;
245 }
246 }
247#endif
248 }
249
261 Key parse(int raw)
262 {
263 if ((raw >= 'A' && raw <= 'Z') || (raw >= '0' && raw <= '9'))
264 {
265 return static_cast<Key>(raw);
266 }
267 if (raw >= 'a' && raw <= 'z')
268 {
269 return static_cast<Key>(raw - 32);
270 }
271 switch (raw)
272 {
273 case ' ':
274 return Key::Space;
275 case 13:
276 return Key::Enter;
277 case 27:
278 return handleEscape();
279 case 8:
280 case 127:
281 return Key::Backspace;
282 case 9:
283 return Key::Tab;
284 }
285#ifdef _WIN32
286 if (raw == 0xE0 || raw == 0x00)
287 {
288 int ext = _getch();
289 switch (ext)
290 {
291 case 0x48:
292 return Key::Up;
293 case 0x50:
294 return Key::Down;
295 case 0x4B:
296 return Key::Left;
297 case 0x4D:
298 return Key::Right;
299 case 0x3B:
300 return Key::F1;
301 case 0x3C:
302 return Key::F2;
303 case 0x3D:
304 return Key::F3;
305 case 0x3E:
306 return Key::F4;
307 case 0x3F:
308 return Key::F5;
309 case 0x40:
310 return Key::F6;
311 case 0x41:
312 return Key::F7;
313 case 0x42:
314 return Key::F8;
315 case 0x43:
316 return Key::F9;
317 case 0x44:
318 return Key::F10;
319 case 0x85:
320 return Key::F11;
321 case 0x86:
322 return Key::F12;
323 }
324 }
325#endif
326 return Key::None;
327 }
328
336 Key handleEscape()
337 {
338#ifndef _WIN32
339 struct pollfd pfd = {STDIN_FILENO, POLLIN, 0};
340 if (poll(&pfd, 1, 5) <= 0)
341 return Key::Esc;
342 char seq[3];
343 int n = read(STDIN_FILENO, seq, 3);
344 if (n >= 2 && seq[0] == '[')
345 {
346 switch (seq[1])
347 {
348 case 'A':
349 return Key::Up;
350 case 'B':
351 return Key::Down;
352 case 'C':
353 return Key::Right;
354 case 'D':
355 return Key::Left;
356 }
357 }
358#endif
359 return Key::Esc;
360 }
361 };
362}
Keyboard & operator=(Keyboard &&)=delete
被删除的移动赋值
Keyboard()
构造函数,开始监视键盘键击。
Definition kb.h:128
Key wait()
等待直到键击并取得当前按键。
Definition kb.h:185
Keyboard(Keyboard &&)=delete
被删除的移动构造函数
Key get()
试图取得当前键击的按键。
Definition kb.h:173
Keyboard(const Keyboard &)=delete
被删除的拷贝构造函数
Keyboard & operator=(const Keyboard &)=delete
被删除的拷贝赋值
~Keyboard()
析构函数,结束对键盘键击的监视。
Definition kb.h:148
本库所有组件所在的顶层命名空间。
Key
用于表示常用按键的枚举。
Definition kb.h:50
@ X
Definition kb.h:76
@ Num6
Definition kb.h:86
@ Down
Definition kb.h:99
@ C
Definition kb.h:55
@ F6
Definition kb.h:108
@ Z
Definition kb.h:78
@ Up
Definition kb.h:98
@ F5
Definition kb.h:107
@ E
Definition kb.h:57
@ P
Definition kb.h:68
@ F7
Definition kb.h:109
@ F8
Definition kb.h:110
@ Num7
Definition kb.h:87
@ F3
Definition kb.h:105
@ U
Definition kb.h:73
@ V
Definition kb.h:74
@ Y
Definition kb.h:77
@ Tab
Definition kb.h:96
@ S
Definition kb.h:71
@ W
Definition kb.h:75
@ F11
Definition kb.h:113
@ M
Definition kb.h:65
@ None
Definition kb.h:51
@ Num2
Definition kb.h:82
@ Num5
Definition kb.h:85
@ A
Definition kb.h:53
@ F
Definition kb.h:58
@ Num0
Definition kb.h:80
@ F9
Definition kb.h:111
@ Num8
Definition kb.h:88
@ N
Definition kb.h:66
@ Esc
Definition kb.h:94
@ Right
Definition kb.h:101
@ Left
Definition kb.h:100
@ B
Definition kb.h:54
@ Num3
Definition kb.h:83
@ K
Definition kb.h:63
@ F10
Definition kb.h:112
@ T
Definition kb.h:72
@ Num1
Definition kb.h:81
@ H
Definition kb.h:60
@ Backspace
Definition kb.h:95
@ L
Definition kb.h:64
@ Space
Definition kb.h:91
@ Num9
Definition kb.h:89
@ I
Definition kb.h:61
@ G
Definition kb.h:59
@ Num4
Definition kb.h:84
@ F1
Definition kb.h:103
@ R
Definition kb.h:70
@ F4
Definition kb.h:106
@ F12
Definition kb.h:114
@ Q
Definition kb.h:69
@ Enter
Definition kb.h:93
@ O
Definition kb.h:67
@ D
Definition kb.h:56
@ F2
Definition kb.h:104
@ J
Definition kb.h:62