diff --git a/src/terminal/adapter/DispatchTypes.hpp b/src/terminal/adapter/DispatchTypes.hpp index 2e4af16791..78a8ff9da0 100644 --- a/src/terminal/adapter/DispatchTypes.hpp +++ b/src/terminal/adapter/DispatchTypes.hpp @@ -388,6 +388,7 @@ namespace Microsoft::Console::VirtualTerminal::DispatchTypes DECSCNM_ScreenMode = DECPrivateMode(5), DECOM_OriginMode = DECPrivateMode(6), DECAWM_AutoWrapMode = DECPrivateMode(7), + DECARM_AutoRepeatMode = DECPrivateMode(8), ATT610_StartCursorBlink = DECPrivateMode(12), DECTCEM_TextCursorEnableMode = DECPrivateMode(25), XTERM_EnableDECCOLMSupport = DECPrivateMode(40), diff --git a/src/terminal/adapter/adaptDispatch.cpp b/src/terminal/adapter/adaptDispatch.cpp index c38788ed77..7e5fb800f8 100644 --- a/src/terminal/adapter/adaptDispatch.cpp +++ b/src/terminal/adapter/adaptDispatch.cpp @@ -1110,6 +1110,9 @@ bool AdaptDispatch::_ModeParamsHelper(const DispatchTypes::ModeParams param, con case DispatchTypes::ModeParams::DECAWM_AutoWrapMode: success = SetAutoWrapMode(enable); break; + case DispatchTypes::ModeParams::DECARM_AutoRepeatMode: + success = _SetInputMode(TerminalInput::Mode::AutoRepeat, enable); + break; case DispatchTypes::ModeParams::ATT610_StartCursorBlink: success = EnableCursorBlinking(enable); break; @@ -1962,6 +1965,9 @@ bool AdaptDispatch::HardReset() // Reset the Backarrow Key mode _SetInputMode(TerminalInput::Mode::BackarrowKey, false); + // Set the keyboard Auto Repeat mode + _SetInputMode(TerminalInput::Mode::AutoRepeat, true); + // Delete all current tab stops and reapply _ResetTabStops(); diff --git a/src/terminal/adapter/ut_adapter/inputTest.cpp b/src/terminal/adapter/ut_adapter/inputTest.cpp index 00ae6ce2cb..b86d5d95a4 100644 --- a/src/terminal/adapter/ut_adapter/inputTest.cpp +++ b/src/terminal/adapter/ut_adapter/inputTest.cpp @@ -46,6 +46,7 @@ public: TEST_METHOD(DifferentModifiersTest); TEST_METHOD(CtrlNumTest); TEST_METHOD(BackarrowKeyModeTest); + TEST_METHOD(AutoRepeatModeTest); wchar_t GetModifierChar(const bool fShift, const bool fAlt, const bool fCtrl) { @@ -853,3 +854,56 @@ void InputTest::BackarrowKeyModeTest() s_expectedInput = L"\x1b\x8"; TestKey(pInput, LEFT_ALT_PRESSED | LEFT_CTRL_PRESSED | SHIFT_PRESSED, vkey); } + +void InputTest::AutoRepeatModeTest() +{ + Log::Comment(L"Starting test..."); + + auto receivedChars = std::wstring{}; + const auto pInput = new TerminalInput([&](auto& inEvents) { + for (auto& record : IInputEvent::ToInputRecords(inEvents)) + { + receivedChars.push_back(record.Event.KeyEvent.uChar.UnicodeChar); + } + }); + + const auto repeatKey = [&](auto vKey, auto unicodeChar, int repeatCount) { + auto irTest = INPUT_RECORD{ 0 }; + irTest.EventType = KEY_EVENT; + irTest.Event.KeyEvent.wRepeatCount = 1; + irTest.Event.KeyEvent.wVirtualKeyCode = vKey; + irTest.Event.KeyEvent.uChar.UnicodeChar = unicodeChar; + irTest.Event.KeyEvent.bKeyDown = TRUE; + + // We're simulating a key being held down so that it repeats, by sending + // multiple KeyDown events followed by a single KeyUp event. + + for (auto i = 0; i < repeatCount; i++) + { + auto keyDownEvent = IInputEvent::Create(irTest); + VERIFY_IS_TRUE(pInput->HandleKey(keyDownEvent.get())); + } + + irTest.Event.KeyEvent.bKeyDown = FALSE; + auto keyUpEvent = IInputEvent::Create(irTest); + VERIFY_IS_FALSE(pInput->HandleKey(keyUpEvent.get())); + }; + + Log::Comment(L"Sending repeating keypresses with DECARM disabled."); + + pInput->SetInputMode(TerminalInput::Mode::AutoRepeat, false); + receivedChars.clear(); + repeatKey('A', L'a', 5); + repeatKey('B', L'b', 5); + repeatKey('C', L'c', 5); + VERIFY_ARE_EQUAL(L"abc", receivedChars); + + Log::Comment(L"Sending repeating keypresses with DECARM enabled."); + + pInput->SetInputMode(TerminalInput::Mode::AutoRepeat, true); + receivedChars.clear(); + repeatKey('A', L'a', 5); + repeatKey('B', L'b', 5); + repeatKey('C', L'c', 5); + VERIFY_ARE_EQUAL(L"aaaaabbbbbccccc", receivedChars); +} diff --git a/src/terminal/input/terminalInput.cpp b/src/terminal/input/terminalInput.cpp index 355f3c242e..5826eea57f 100644 --- a/src/terminal/input/terminalInput.cpp +++ b/src/terminal/input/terminalInput.cpp @@ -554,9 +554,24 @@ bool TerminalInput::HandleKey(const IInputEvent* const pInEvent) // Only need to handle key down. See raw key handler (see RawReadWaitRoutine in stream.cpp) if (!keyEvent.IsKeyDown()) { + // If this is a release of the last recorded key press, we can reset that. + if (keyEvent.GetVirtualKeyCode() == _lastVirtualKeyCode) + { + _lastVirtualKeyCode = 0; + } return false; } + // If this is a repeat of the last recorded key press, and Auto Repeat Mode + // is disabled, then we should suppress this event. + if (keyEvent.GetVirtualKeyCode() == _lastVirtualKeyCode && !_inputMode.test(Mode::AutoRepeat)) + { + // Note that we must return true here to say we've handled the event, + // otherwise the key press can still end up being submitted. + return true; + } + _lastVirtualKeyCode = keyEvent.GetVirtualKeyCode(); + // The VK_BACK key depends on the state of Backarrow Key mode (DECBKM). // If the mode is set, we should send BS. If reset, we should send DEL. if (keyEvent.GetVirtualKeyCode() == VK_BACK) diff --git a/src/terminal/input/terminalInput.hpp b/src/terminal/input/terminalInput.hpp index 80ff4e8c06..70fc0e6b62 100644 --- a/src/terminal/input/terminalInput.hpp +++ b/src/terminal/input/terminalInput.hpp @@ -39,6 +39,7 @@ namespace Microsoft::Console::VirtualTerminal enum class Mode : size_t { Ansi, + AutoRepeat, Keypad, CursorKey, BackarrowKey, @@ -92,7 +93,9 @@ namespace Microsoft::Console::VirtualTerminal // storage location for the leading surrogate of a utf-16 surrogate pair std::optional _leadingSurrogate; - til::enumset _inputMode{ Mode::Ansi }; + WORD _lastVirtualKeyCode{ 0 }; + + til::enumset _inputMode{ Mode::Ansi, Mode::AutoRepeat }; bool _forceDisableWin32InputMode{ false }; void _SendChar(const wchar_t ch);