-
-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathglobal_win.cpp
More file actions
82 lines (62 loc) · 1.63 KB
/
global_win.cpp
File metadata and controls
82 lines (62 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "global.h"
#include <windows.h>
static DWORD origMode = 0;
void resetTerminalMode()
{
if (origMode) {
HANDLE stdinHandle = GetStdHandle(STD_INPUT_HANDLE);
SetConsoleMode(stdinHandle, origMode);
}
}
void setTerminalMode(bool enableEcho)
{
HANDLE stdinHandle = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode = 0;
GetConsoleMode(stdinHandle, &mode);
if (!origMode) {
origMode = mode;
}
mode &= ~ENABLE_LINE_INPUT;
if (enableEcho) {
mode |= ENABLE_ECHO_INPUT;
} else {
mode &= ~ENABLE_ECHO_INPUT;
}
SetConsoleMode(stdinHandle, mode);
}
QByteArray readStdInput()
{
QByteArray input;
DWORD count = 0;
HANDLE stdinHandle = GetStdHandle(STD_INPUT_HANDLE);
if (!GetNumberOfConsoleInputEvents(stdinHandle, &count) || count == 0) {
return input;
}
INPUT_RECORD records[64];
DWORD readCount = 0;
if (!ReadConsoleInputW(stdinHandle, records, 64, &readCount)) {
return input;
}
for (DWORD i = 0; i < readCount; ++i) {
const INPUT_RECORD &rec = records[i];
if (rec.EventType != KEY_EVENT) {
continue;
}
const KEY_EVENT_RECORD &key = rec.Event.KeyEvent;
if (!key.bKeyDown) {
continue;
}
wchar_t ch = key.uChar.UnicodeChar;
if (ch == 0) {
continue;
}
if (ch == L'\r') {
input += "\r\n";
} else if (key.wVirtualKeyCode == VK_BACK) {
input += (char)0x7f;
} else {
input += QString::fromWCharArray(&ch, 1).toUtf8();
}
}
return input;
}