Skip to content

Commit

Permalink
Make KeyEvent char data a little less confusing (#14747)
Browse files Browse the repository at this point in the history
When working on #14745 I found `KeyEvent`s a little hard to read in the
debugger. I noticed that this is because of sign extension when converting
`char`s to `wchar_t`s in `KeyEvent::SetCharData`.
  • Loading branch information
lhecker authored Feb 2, 2023
1 parent 282c583 commit ddc349b
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/host/dbcs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ _Ret_range_(0, cbAnsi)

memcpy(TmpUni, pwchUnicode, cchUnicode * sizeof(WCHAR));

BYTE AsciiDbcs[2];
CHAR AsciiDbcs[2];
AsciiDbcs[1] = 0;

ULONG i, j;
Expand Down
4 changes: 2 additions & 2 deletions src/interactivity/win32/windowio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ void HandleKeyEvent(const HWND hWnd,
}
else
{
keyEvent.SetCharData(0);
keyEvent.SetCharData(L'\0');
}
}
else
Expand All @@ -220,7 +220,7 @@ void HandleKeyEvent(const HWND hWnd,
return;
}
keyEvent.SetActiveModifierKeys(ControlKeyState);
keyEvent.SetCharData(0);
keyEvent.SetCharData(L'\0');
}

const INPUT_KEY_INFO inputKeyInfo(VirtualKeyCode, ControlKeyState);
Expand Down
8 changes: 8 additions & 0 deletions src/types/KeyEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ void KeyEvent::SetVirtualScanCode(const WORD virtualScanCode) noexcept
_virtualScanCode = virtualScanCode;
}

void KeyEvent::SetCharData(const char character) noexcept
{
// With MSVC char is signed by default and the conversion to wchar_t (unsigned) would turn negative
// chars into very large wchar_t values. While this doesn't pose a problem per se (even with such sign
// extension, the lower 8 bit stay the same), it makes debugging and reading key events more difficult.
_charData = til::as_unsigned(character);
}

void KeyEvent::SetCharData(const wchar_t character) noexcept
{
_charData = character;
Expand Down
1 change: 1 addition & 0 deletions src/types/inc/IInputEvent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ class KeyEvent : public IInputEvent
void SetRepeatCount(const WORD repeatCount) noexcept;
void SetVirtualKeyCode(const WORD virtualKeyCode) noexcept;
void SetVirtualScanCode(const WORD virtualScanCode) noexcept;
void SetCharData(const char character) noexcept;
void SetCharData(const wchar_t character) noexcept;

void SetActiveModifierKeys(const DWORD activeModifierKeys) noexcept;
Expand Down

0 comments on commit ddc349b

Please sign in to comment.