Fix #8458: Handle all Ctrl-key combinations (#8870)

Pressing Ctrl+\ produces `^\` using the US keyboard layout, thanks to Ctrl-key mappings inside the keyboard layout, whereas some layouts, like the UK extended layout, don't contain those. This causes the character value to be zero and previously caused no VT sequence to be generated under these situations. This PR employs `MapVirtualKeyW` to infer the missing characters.
As a side effect this PR effectively causes _all_ major keys on the keyboard to produce Ctrl+combinations now.

## PR Checklist
* [x] Closes #8458
* [x] CLA signed. If not, go over [here](https://cla.opensource.microsoft.com/microsoft/Terminal) and sign the CLA
* [x] Tests passed

## Validation Steps Performed

Compared all major keys in combination with Ctrl with the app store version of Terminal using `showkey` in WSL. All keys that previously worked still appear to continue to work.
This commit is contained in:
Leonard Hecker 2021-02-02 18:26:48 +01:00 committed by GitHub
parent 92b23700d3
commit 3b9e6124e0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -627,9 +627,28 @@ bool TerminalInput::HandleKey(const IInputEvent* const pInEvent)
_SendNullInputSequence(keyEvent.GetActiveModifierKeys());
return true;
}
// Not all keyboard layouts contain mappings for Ctrl-key combinations.
// For instance the US one contains a mapping of Ctrl+\ to ^\,
// but the UK extended layout doesn't, in which case ch is null.
if (ch == UNICODE_NULL)
{
// -> Try to infer the character from the vkey.
auto mappedChar = LOWORD(MapVirtualKeyW(keyEvent.GetVirtualKeyCode(), MAPVK_VK_TO_CHAR));
if (mappedChar)
{
// Pressing the control key causes all bits but the 5 least
// significant ones to be zeroed out (when using ASCII).
mappedChar &= 0b11111;
_SendChar(mappedChar);
return true;
}
}
}
// Check any other key mappings (like those for the F1-F12 keys).
// These mappings will kick in no matter which modifiers are pressed and as such
// must be checked last, or otherwise we'd override more complex key combinations.
const auto mapping = _getKeyMapping(keyEvent, _ansiMode, _cursorApplicationMode, _keypadApplicationMode);
if (_translateDefaultMapping(keyEvent, mapping, senderFunc))
{