1 // Copyright (c) 2016 Ryan Prichard
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to
5 // deal in the Software without restriction, including without limitation the
6 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 // sell copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 #include "ConsoleInputReencoding.h"
23 #include "ConsoleInput.h"
27 static void outch(std::vector<INPUT_RECORD> &out, wchar_t ch) {
28 ConsoleInput::appendInputRecord(out, TRUE, 0, ch, 0);
31 } // anonymous namespace
33 void reencodeEscapedKeyPress(
34 std::vector<INPUT_RECORD> &out,
40 enum { None, Numeric, Letter } kind;
44 EscapedKey escapeCode = {};
46 case VK_UP: escapeCode = { EscapedKey::Letter, {'A'} }; break;
47 case VK_DOWN: escapeCode = { EscapedKey::Letter, {'B'} }; break;
48 case VK_RIGHT: escapeCode = { EscapedKey::Letter, {'C'} }; break;
49 case VK_LEFT: escapeCode = { EscapedKey::Letter, {'D'} }; break;
50 case VK_CLEAR: escapeCode = { EscapedKey::Letter, {'E'} }; break;
51 case VK_F1: escapeCode = { EscapedKey::Numeric, {'1', '1'} }; break;
52 case VK_F2: escapeCode = { EscapedKey::Numeric, {'1', '2'} }; break;
53 case VK_F3: escapeCode = { EscapedKey::Numeric, {'1', '3'} }; break;
54 case VK_F4: escapeCode = { EscapedKey::Numeric, {'1', '4'} }; break;
55 case VK_F5: escapeCode = { EscapedKey::Numeric, {'1', '5'} }; break;
56 case VK_F6: escapeCode = { EscapedKey::Numeric, {'1', '7'} }; break;
57 case VK_F7: escapeCode = { EscapedKey::Numeric, {'1', '8'} }; break;
58 case VK_F8: escapeCode = { EscapedKey::Numeric, {'1', '9'} }; break;
59 case VK_F9: escapeCode = { EscapedKey::Numeric, {'2', '0'} }; break;
60 case VK_F10: escapeCode = { EscapedKey::Numeric, {'2', '1'} }; break;
61 case VK_F11: escapeCode = { EscapedKey::Numeric, {'2', '3'} }; break;
62 case VK_F12: escapeCode = { EscapedKey::Numeric, {'2', '4'} }; break;
63 case VK_HOME: escapeCode = { EscapedKey::Letter, {'H'} }; break;
64 case VK_INSERT: escapeCode = { EscapedKey::Numeric, {'2'} }; break;
65 case VK_DELETE: escapeCode = { EscapedKey::Numeric, {'3'} }; break;
66 case VK_END: escapeCode = { EscapedKey::Letter, {'F'} }; break;
67 case VK_PRIOR: escapeCode = { EscapedKey::Numeric, {'5'} }; break;
68 case VK_NEXT: escapeCode = { EscapedKey::Numeric, {'6'} }; break;
70 if (escapeCode.kind != EscapedKey::None) {
72 if (keyState & SHIFT_PRESSED) { flags |= 0x1; }
73 if (keyState & LEFT_ALT_PRESSED) { flags |= 0x2; }
74 if (keyState & LEFT_CTRL_PRESSED) { flags |= 0x4; }
77 if (escapeCode.kind == EscapedKey::Numeric) {
78 for (wchar_t ch : escapeCode.content) {
83 } else if (flags != 0) {
88 outch(out, L'1' + flags);
90 if (escapeCode.kind == EscapedKey::Numeric) {
93 outch(out, escapeCode.content[0]);
100 if (keyState & LEFT_ALT_PRESSED) {
106 if (keyState & SHIFT_PRESSED) {
115 if (codePoint != 0) {
116 if (keyState & LEFT_ALT_PRESSED) {
119 ConsoleInput::appendCPInputRecords(out, TRUE, 0, codePoint, 0);