@@ -358,6 +358,15 @@ void ConsoleInput::flushInputRecords(std::vector<INPUT_RECORD> &records)
358
358
records.clear ();
359
359
}
360
360
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
361
370
static void sendKeyMessage (HWND hwnd, bool isKeyDown, uint16_t virtualKey)
362
371
{
363
372
uint32_t scanCode = MapVirtualKey (virtualKey, MAPVK_VK_TO_VSC);
@@ -375,21 +384,22 @@ int ConsoleInput::scanInput(std::vector<INPUT_RECORD> &records,
375
384
{
376
385
ASSERT (inputSize >= 1 );
377
386
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
386
398
if (input[0 ] == ' \x03 ' && (inputConsoleMode () & ENABLE_PROCESSED_INPUT)) {
387
399
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);
393
403
return 1 ;
394
404
}
395
405
0 commit comments