Skip to content

Commit

Permalink
Add support to the Windows Terminal for win32-input-mode
Browse files Browse the repository at this point in the history
  • Loading branch information
zadjii-msft committed May 14, 2020
1 parent 943e714 commit 6b2d952
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/cascadia/PublicTerminalCore/HwndTerminal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ void HwndTerminal::_SendKeyEvent(WORD vkey, WORD scanCode) noexcept
try
{
const auto flags = getControlKeyState();
_terminal->SendKeyEvent(vkey, scanCode, flags);
_terminal->SendKeyEvent(vkey, scanCode, flags, true);
}
CATCH_LOG();

Expand Down
25 changes: 18 additions & 7 deletions src/cascadia/TerminalControl/TermControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
if (!handled)
{
// _TrySendKeyEvent pretends it didn't handle F7 for some unknown reason.
(void)_TrySendKeyEvent(VK_F7, 0, modifiers);
(void)_TrySendKeyEvent(VK_F7, 0, modifiers, true);
handled = true;
}

Expand All @@ -721,6 +721,17 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation

void TermControl::_KeyDownHandler(winrt::Windows::Foundation::IInspectable const& /*sender*/,
Input::KeyRoutedEventArgs const& e)
{
_KeyHandler(e, true);
}

void TermControl::_KeyUpHandler(winrt::Windows::Foundation::IInspectable const& /*sender*/,
Input::KeyRoutedEventArgs const& e)
{
_KeyHandler(e, false);
}

void TermControl::_KeyHandler(Input::KeyRoutedEventArgs const& e, const bool keyDown)
{
// If the current focused element is a child element of searchbox,
// we do not send this event up to terminal
Expand All @@ -734,9 +745,9 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
// - key modifier is pressed
// NOTE: for key combos like CTRL + C, two events are fired (one for CTRL, one for 'C'). We care about the 'C' event and then check for key modifiers below.
if (_closing ||
e.OriginalKey() == VirtualKey::Control ||
e.OriginalKey() == VirtualKey::Shift ||
e.OriginalKey() == VirtualKey::Menu ||
// e.OriginalKey() == VirtualKey::Control ||
// e.OriginalKey() == VirtualKey::Shift ||
// e.OriginalKey() == VirtualKey::Menu ||
e.OriginalKey() == VirtualKey::LeftWindows ||
e.OriginalKey() == VirtualKey::RightWindows)

Expand Down Expand Up @@ -778,7 +789,7 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation

if (!handled)
{
handled = _TrySendKeyEvent(vkey, scanCode, modifiers);
handled = _TrySendKeyEvent(vkey, scanCode, modifiers, keyDown);
}

// Manually prevent keyboard navigation with tab. We want to send tab to
Expand All @@ -800,7 +811,7 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
// Arguments:
// - vkey: The vkey of the key pressed.
// - states: The Microsoft::Terminal::Core::ControlKeyStates representing the modifier key states.
bool TermControl::_TrySendKeyEvent(const WORD vkey, const WORD scanCode, const ControlKeyStates modifiers)
bool TermControl::_TrySendKeyEvent(const WORD vkey, const WORD scanCode, const ControlKeyStates modifiers, const bool keyDown)
{
// When there is a selection active, escape should clear it and NOT flow through
// to the terminal. With any other keypress, it should clear the selection AND
Expand All @@ -825,7 +836,7 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
// If the terminal translated the key, mark the event as handled.
// This will prevent the system from trying to get the character out
// of it and sending us a CharacterReceived event.
const auto handled = vkey ? _terminal->SendKeyEvent(vkey, scanCode, modifiers) : true;
const auto handled = vkey ? _terminal->SendKeyEvent(vkey, scanCode, modifiers, keyDown) : true;

if (_cursorTimer.has_value())
{
Expand Down
4 changes: 3 additions & 1 deletion src/cascadia/TerminalControl/TermControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
void _SetFontSize(int fontSize);
void _TappedHandler(Windows::Foundation::IInspectable const& sender, Windows::UI::Xaml::Input::TappedRoutedEventArgs const& e);
void _KeyDownHandler(Windows::Foundation::IInspectable const& sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs const& e);
void _KeyUpHandler(Windows::Foundation::IInspectable const& sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs const& e);
void _CharacterHandler(Windows::Foundation::IInspectable const& sender, Windows::UI::Xaml::Input::CharacterReceivedRoutedEventArgs const& e);
void _PointerPressedHandler(Windows::Foundation::IInspectable const& sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs const& e);
void _PointerMovedHandler(Windows::Foundation::IInspectable const& sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs const& e);
Expand Down Expand Up @@ -218,8 +219,9 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
void _ScrollbarUpdater(Windows::UI::Xaml::Controls::Primitives::ScrollBar scrollbar, const int viewTop, const int viewHeight, const int bufferSize);
static Windows::UI::Xaml::Thickness _ParseThicknessFromPadding(const hstring padding);

void _KeyHandler(Windows::UI::Xaml::Input::KeyRoutedEventArgs const& e, const bool keyDown);
::Microsoft::Terminal::Core::ControlKeyStates _GetPressedModifierKeys() const;
bool _TrySendKeyEvent(const WORD vkey, const WORD scanCode, ::Microsoft::Terminal::Core::ControlKeyStates modifiers);
bool _TrySendKeyEvent(const WORD vkey, const WORD scanCode, ::Microsoft::Terminal::Core::ControlKeyStates modifiers, const bool keyDown);
bool _TrySendMouseEvent(Windows::UI::Input::PointerPoint const& point);
bool _CanSendVTMouseInput();

Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalControl/TermControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
Tapped="_TappedHandler"
PointerWheelChanged="_MouseWheelHandler"
PreviewKeyDown="_KeyDownHandler"
KeyUp="_KeyUpHandler"
CharacterReceived="_CharacterHandler"
GotFocus="_GotFocusHandler"
LostFocus="_LostFocusHandler">
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalCore/ITerminalApi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ namespace Microsoft::Terminal::Core
virtual bool SetDefaultForeground(const DWORD color) noexcept = 0;
virtual bool SetDefaultBackground(const DWORD color) noexcept = 0;

virtual bool EnableWin32InputMode(const bool win32InputMode) noexcept = 0;
virtual bool SetCursorKeysMode(const bool applicationMode) noexcept = 0;
virtual bool SetKeypadMode(const bool applicationMode) noexcept = 0;
virtual bool EnableVT200MouseMode(const bool enabled) noexcept = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalCore/ITerminalInput.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Microsoft::Terminal::Core
ITerminalInput& operator=(const ITerminalInput&) = default;
ITerminalInput& operator=(ITerminalInput&&) = default;

virtual bool SendKeyEvent(const WORD vkey, const WORD scanCode, const ControlKeyStates states) = 0;
virtual bool SendKeyEvent(const WORD vkey, const WORD scanCode, const ControlKeyStates states, const bool keyDown) = 0;
virtual bool SendMouseEvent(const COORD viewportPos, const unsigned int uiButton, const ControlKeyStates states, const short wheelDelta) = 0;
virtual bool SendCharEvent(const wchar_t ch, const WORD scanCode, const ControlKeyStates states) = 0;

Expand Down
4 changes: 2 additions & 2 deletions src/cascadia/TerminalCore/Terminal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ bool Terminal::IsTrackingMouseInput() const noexcept
// Return Value:
// - true if we translated the key event, and it should not be processed any further.
// - false if we did not translate the key, and it should be processed into a character.
bool Terminal::SendKeyEvent(const WORD vkey, const WORD scanCode, const ControlKeyStates states)
bool Terminal::SendKeyEvent(const WORD vkey, const WORD scanCode, const ControlKeyStates states, const bool keyDown)
{
TrySnapOnInput();
_StoreKeyEvent(vkey, scanCode);
Expand Down Expand Up @@ -443,7 +443,7 @@ bool Terminal::SendKeyEvent(const WORD vkey, const WORD scanCode, const ControlK
return false;
}

KeyEvent keyEv{ true, 0, vkey, scanCode, ch, states.Value() };
KeyEvent keyEv{ keyDown, 1, vkey, scanCode, ch, states.Value() };
return _terminalInput->HandleKey(&keyEv);
}

Expand Down
3 changes: 2 additions & 1 deletion src/cascadia/TerminalCore/Terminal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ class Microsoft::Terminal::Core::Terminal final :
bool SetDefaultForeground(const COLORREF color) noexcept override;
bool SetDefaultBackground(const COLORREF color) noexcept override;

bool EnableWin32InputMode(const bool win32InputMode) noexcept override;
bool SetCursorKeysMode(const bool applicationMode) noexcept override;
bool SetKeypadMode(const bool applicationMode) noexcept override;
bool EnableVT200MouseMode(const bool enabled) noexcept override;
Expand All @@ -115,7 +116,7 @@ class Microsoft::Terminal::Core::Terminal final :

#pragma region ITerminalInput
// These methods are defined in Terminal.cpp
bool SendKeyEvent(const WORD vkey, const WORD scanCode, const Microsoft::Terminal::Core::ControlKeyStates states) override;
bool SendKeyEvent(const WORD vkey, const WORD scanCode, const Microsoft::Terminal::Core::ControlKeyStates states, const bool keyDown) override;
bool SendMouseEvent(const COORD viewportPos, const unsigned int uiButton, const ControlKeyStates states, const short wheelDelta) override;
bool SendCharEvent(const wchar_t ch, const WORD scanCode, const ControlKeyStates states) override;

Expand Down
6 changes: 6 additions & 0 deletions src/cascadia/TerminalCore/TerminalApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,12 @@ try
}
CATCH_LOG_RETURN_FALSE()

bool Terminal::EnableWin32InputMode(const bool win32InputMode) noexcept
{
_terminalInput->ChangeWin32InputMode(win32InputMode);
return true;
}

bool Terminal::SetCursorKeysMode(const bool applicationMode) noexcept
{
_terminalInput->ChangeCursorKeysMode(applicationMode);
Expand Down
12 changes: 12 additions & 0 deletions src/cascadia/TerminalCore/TerminalDispatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,18 @@ bool TerminalDispatch::SetCursorKeysMode(const bool applicationMode) noexcept
return true;
}

// Method Description
// - TODO
// Arguments:
// - applicationMode - set to true to enable Application Mode Input, false for Numeric Mode Input.
// Return Value:
// - True if handled successfully. False otherwise.
bool TerminalDispatch::EnableWin32InputMode(const bool win32Mode) noexcept
{
_terminalApi.EnableWin32InputMode(win32Mode);
return true;
}

//Routine Description:
// Enable VT200 Mouse Mode - Enables/disables the mouse input handler in default tracking mode.
//Arguments:
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalCore/TerminalDispatch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class TerminalDispatch : public Microsoft::Console::VirtualTerminal::TermDispatc
bool CursorPosition(const size_t line,
const size_t column) noexcept override; // CUP

bool EnableWin32InputMode(const bool win32InputMode) noexcept override; // win32-input-mode

bool CursorVisibility(const bool isVisible) noexcept override; // DECTCEM
bool EnableCursorBlinking(const bool enable) noexcept override; // ATT610

Expand Down

1 comment on commit 6b2d952

@github-actions

This comment was marked as outdated.

Please sign in to comment.