Skip to content

Commit 4978cf9

Browse files
committedMay 19, 2017
Go back to using GenerateConsoleCtrlEvent for processed-mode Ctrl-C
This change reverts the first part of the rprichardGH-116 fix. See rprichard#116
1 parent 0c86e4f commit 4978cf9

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed
 

‎src/agent/ConsoleInput.cc

+23-13
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,15 @@ void ConsoleInput::flushInputRecords(std::vector<INPUT_RECORD> &records)
358358
records.clear();
359359
}
360360

361+
// This behavior isn't strictly correct, because the keypresses (probably?)
362+
// adopt the keyboard state (e.g. Ctrl/Alt/Shift modifiers) of the current
363+
// window station's keyboard, which has no necessary relationship to the winpty
364+
// instance. It's unlikely to be an issue in practice, but it's conceivable.
365+
// (Imagine a foreground SSH server, where the local user holds down Ctrl,
366+
// while the remote user tries to use WSL navigation keys.) I suspect using
367+
// the BackgroundDesktop mechanism in winpty would fix the problem.
368+
//
369+
// https://github.com/rprichard/winpty/issues/116
361370
static void sendKeyMessage(HWND hwnd, bool isKeyDown, uint16_t virtualKey)
362371
{
363372
uint32_t scanCode = MapVirtualKey(virtualKey, MAPVK_VK_TO_VSC);
@@ -375,21 +384,22 @@ int ConsoleInput::scanInput(std::vector<INPUT_RECORD> &records,
375384
{
376385
ASSERT(inputSize >= 1);
377386

378-
// Ctrl-C. We need to use window messages when the console is in
379-
// ENABLE_PROCESSED_INPUT mode so that Ctrl-C interrupts a ReadConsole
380-
// call. In unprocessed mode, we can't use this code path, because it
381-
// would produce a KEY_EVENT_RECORD with a NUL UnicodeChar. We can't use
382-
// the ordinary VkKeyScan code path for Ctrl-C, either, because it produces
383-
// an unmodified VK_CANCEL. Instead, there's an entry for Ctrl-C in the
384-
// SimpleEncoding table in DefaultInputMap.
385-
// See https://github.com/rprichard/winpty/issues/116.
387+
// Ctrl-C.
388+
//
389+
// In processed mode, use GenerateConsoleCtrlEvent so that Ctrl-C handlers
390+
// are called. GenerateConsoleCtrlEvent unfortunately doesn't interrupt
391+
// ReadConsole calls[1]. Using WM_KEYDOWN/UP fixes the ReadConsole
392+
// problem, but breaks in background window stations/desktops.
393+
//
394+
// In unprocessed mode, there's an entry for Ctrl-C in the SimpleEncoding
395+
// table in DefaultInputMap.
396+
//
397+
// [1] https://github.com/rprichard/winpty/issues/116
386398
if (input[0] == '\x03' && (inputConsoleMode() & ENABLE_PROCESSED_INPUT)) {
387399
flushInputRecords(records);
388-
trace("Sending Ctrl-C KEYDOWN/KEYUP messages");
389-
sendKeyMessage(m_console.hwnd(), true, VK_CONTROL);
390-
sendKeyMessage(m_console.hwnd(), true, 'C');
391-
sendKeyMessage(m_console.hwnd(), false, 'C');
392-
sendKeyMessage(m_console.hwnd(), false, VK_CONTROL);
400+
trace("Ctrl-C");
401+
const BOOL ret = GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0);
402+
trace("GenerateConsoleCtrlEvent: %d", ret);
393403
return 1;
394404
}
395405

0 commit comments

Comments
 (0)
Please sign in to comment.