WindowsKeyConverter.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #nullable enable
  2. namespace Terminal.Gui.Drivers;
  3. /// <summary>
  4. /// <see cref="IKeyConverter{T}"/> capable of converting the
  5. /// windows native <see cref="WindowsConsole.InputRecord"/> class
  6. /// into Terminal.Gui shared <see cref="Key"/> representation
  7. /// (used by <see cref="View"/> etc).
  8. /// </summary>
  9. internal class WindowsKeyConverter : IKeyConverter<WindowsConsole.InputRecord>
  10. {
  11. /// <inheritdoc/>
  12. public Key ToKey (WindowsConsole.InputRecord inputEvent)
  13. {
  14. if (inputEvent.KeyEvent.wVirtualKeyCode == (ConsoleKeyMapping.VK)ConsoleKey.Packet)
  15. {
  16. // Used to pass Unicode characters as if they were keystrokes.
  17. // The VK_PACKET key is the low word of a 32-bit
  18. // Virtual Key value used for non-keyboard input methods.
  19. inputEvent.KeyEvent = WindowsDriver.FromVKPacketToKeyEventRecord (inputEvent.KeyEvent);
  20. }
  21. var keyInfo = WindowsDriver.ToConsoleKeyInfoEx (inputEvent.KeyEvent);
  22. //Debug.WriteLine ($"event: KBD: {GetKeyboardLayoutName()} {inputEvent.ToString ()} {keyInfo.ToString (keyInfo)}");
  23. KeyCode map = WindowsDriver.MapKey (keyInfo);
  24. if (map == KeyCode.Null)
  25. {
  26. return (Key)0;
  27. }
  28. return new (map);
  29. }
  30. }