Skip to content

Commit

Permalink
Use Window messages rather than GenerateConsoleCtrlEvent for Ctrl-C
Browse files Browse the repository at this point in the history
Fixes #116
  • Loading branch information
rprichard committed May 17, 2017
1 parent 096f978 commit c9ce3ad
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/agent/ConsoleInput.cc
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,16 @@ void ConsoleInput::flushInputRecords(std::vector<INPUT_RECORD> &records)
records.clear();
}

static void sendKeyMessage(HWND hwnd, bool isKeyDown, uint16_t virtualKey)
{
uint32_t scanCode = MapVirtualKey(virtualKey, MAPVK_VK_TO_VSC);
if (scanCode > 255) {
scanCode = 0;
}
SendMessage(hwnd, isKeyDown ? WM_KEYDOWN : WM_KEYUP, virtualKey,
(scanCode << 16) | 1u | (isKeyDown ? 0u : 0xc0000000u));
}

int ConsoleInput::scanInput(std::vector<INPUT_RECORD> &records,
const char *input,
int inputSize,
Expand All @@ -368,9 +378,11 @@ int ConsoleInput::scanInput(std::vector<INPUT_RECORD> &records,
// Ctrl-C.
if (input[0] == '\x03' && (inputConsoleMode() & ENABLE_PROCESSED_INPUT)) {
flushInputRecords(records);
trace("Ctrl-C");
BOOL ret = GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0);
trace("GenerateConsoleCtrlEvent: %d", ret);
trace("Sending Ctrl-C KEYDOWN/KEYUP messages");
sendKeyMessage(m_console.hwnd(), true, VK_CONTROL);
sendKeyMessage(m_console.hwnd(), true, 'C');
sendKeyMessage(m_console.hwnd(), false, 'C');
sendKeyMessage(m_console.hwnd(), false, VK_CONTROL);
return 1;
}

Expand Down Expand Up @@ -664,14 +676,8 @@ void ConsoleInput::appendKeyPress(std::vector<INPUT_RECORD> &records,
if (hasDebugInput) {
trace("sending keypress to console HWND");
}
uint32_t scanCode = MapVirtualKey(virtualKey, MAPVK_VK_TO_VSC);
if (scanCode > 255) {
scanCode = 0;
}
SendMessage(m_console.hwnd(), WM_KEYDOWN, virtualKey,
(scanCode << 16) | 1u);
SendMessage(m_console.hwnd(), WM_KEYUP, virtualKey,
(scanCode << 16) | (1u | (1u << 30) | (1u << 31)));
sendKeyMessage(m_console.hwnd(), true, virtualKey);
sendKeyMessage(m_console.hwnd(), false, virtualKey);
return;
}

Expand Down

0 comments on commit c9ce3ad

Please sign in to comment.