WindowsKeyConverter.cs 1.3 KB

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