Browse Source

Moving MapKey method to the AnsiEscapeSequenceRequestUtils class.

BDisp 8 months ago
parent
commit
a64f68ca7f

+ 102 - 2
Terminal.Gui/ConsoleDrivers/AnsiEscapeSequence/AnsiEscapeSequenceRequestUtils.cs

@@ -1,5 +1,6 @@
 #nullable enable
 using Terminal.Gui.ConsoleDrivers;
+using static Terminal.Gui.ConsoleDrivers.ConsoleKeyMapping;
 
 namespace Terminal.Gui;
 
@@ -1135,12 +1136,11 @@ public static class AnsiEscapeSequenceRequestUtils
 
                 break;
             default:
-                uint ck = ConsoleKeyMapping.MapKeyCodeToConsoleKey ((KeyCode)consoleKeyInfo.KeyChar, out bool isConsoleKey);
+                uint ck = MapKeyCodeToConsoleKey ((KeyCode)consoleKeyInfo.KeyChar, out bool isConsoleKey);
 
                 if (isConsoleKey)
                 {
                     key = (ConsoleKey)ck;
-                    keyChar = (char)ck;
                 }
 
                 newConsoleKeyInfo = new (
@@ -1363,6 +1363,106 @@ public static class AnsiEscapeSequenceRequestUtils
         return mf;
     }
 
+    internal static KeyCode MapKey (ConsoleKeyInfo keyInfo)
+    {
+        switch (keyInfo.Key)
+        {
+            case ConsoleKey.OemPeriod:
+            case ConsoleKey.OemComma:
+            case ConsoleKey.OemPlus:
+            case ConsoleKey.OemMinus:
+            case ConsoleKey.Packet:
+            case ConsoleKey.Oem1:
+            case ConsoleKey.Oem2:
+            case ConsoleKey.Oem3:
+            case ConsoleKey.Oem4:
+            case ConsoleKey.Oem5:
+            case ConsoleKey.Oem6:
+            case ConsoleKey.Oem7:
+            case ConsoleKey.Oem8:
+            case ConsoleKey.Oem102:
+                if (keyInfo.KeyChar == 0)
+                {
+                    // If the keyChar is 0, keyInfo.Key value is not a printable character. 
+
+                    return KeyCode.Null; // MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode)keyInfo.Key);
+                }
+
+                if (keyInfo.Modifiers != ConsoleModifiers.Shift)
+                {
+                    // If Shift wasn't down we don't need to do anything but return the keyInfo.KeyChar
+                    return MapToKeyCodeModifiers (keyInfo.Modifiers, (KeyCode)keyInfo.KeyChar);
+                }
+
+                // Strip off Shift - We got here because they KeyChar from Windows is the shifted char (e.g. "Ç")
+                // and passing on Shift would be redundant.
+                return MapToKeyCodeModifiers (keyInfo.Modifiers & ~ConsoleModifiers.Shift, (KeyCode)keyInfo.KeyChar);
+        }
+
+        // Handle control keys whose VK codes match the related ASCII value (those below ASCII 33) like ESC
+        if (keyInfo.Key != ConsoleKey.None && Enum.IsDefined (typeof (KeyCode), (uint)keyInfo.Key))
+        {
+            if (keyInfo.Modifiers.HasFlag (ConsoleModifiers.Control) && keyInfo.Key == ConsoleKey.I)
+            {
+                return KeyCode.Tab;
+            }
+
+            return MapToKeyCodeModifiers (keyInfo.Modifiers, (KeyCode)(uint)keyInfo.Key);
+        }
+
+        // Handle control keys (e.g. CursorUp)
+        if (keyInfo.Key != ConsoleKey.None
+            && Enum.IsDefined (typeof (KeyCode), (uint)keyInfo.Key + (uint)KeyCode.MaxCodePoint))
+        {
+            return MapToKeyCodeModifiers (keyInfo.Modifiers, (KeyCode)((uint)keyInfo.Key + (uint)KeyCode.MaxCodePoint));
+        }
+
+        if ((ConsoleKey)keyInfo.KeyChar is >= ConsoleKey.A and <= ConsoleKey.Z)
+        {
+            // Shifted
+            keyInfo = new (
+                           keyInfo.KeyChar,
+                           (ConsoleKey)keyInfo.KeyChar,
+                           true,
+                           keyInfo.Modifiers.HasFlag (ConsoleModifiers.Alt),
+                           keyInfo.Modifiers.HasFlag (ConsoleModifiers.Control));
+        }
+
+        if ((ConsoleKey)keyInfo.KeyChar - 32 is >= ConsoleKey.A and <= ConsoleKey.Z)
+        {
+            // Unshifted
+            keyInfo = new (
+                           keyInfo.KeyChar,
+                           (ConsoleKey)(keyInfo.KeyChar - 32),
+                           false,
+                           keyInfo.Modifiers.HasFlag (ConsoleModifiers.Alt),
+                           keyInfo.Modifiers.HasFlag (ConsoleModifiers.Control));
+        }
+
+        if (keyInfo.Key is >= ConsoleKey.A and <= ConsoleKey.Z)
+        {
+            if (keyInfo.Modifiers.HasFlag (ConsoleModifiers.Alt)
+                || keyInfo.Modifiers.HasFlag (ConsoleModifiers.Control))
+            {
+                // NetDriver doesn't support Shift-Ctrl/Shift-Alt combos
+                return MapToKeyCodeModifiers (keyInfo.Modifiers & ~ConsoleModifiers.Shift, (KeyCode)keyInfo.Key);
+            }
+
+            if (keyInfo.Modifiers == ConsoleModifiers.Shift)
+            {
+                // If ShiftMask is on  add the ShiftMask
+                if (char.IsUpper (keyInfo.KeyChar))
+                {
+                    return (KeyCode)keyInfo.Key | KeyCode.ShiftMask;
+                }
+            }
+
+            return (KeyCode)keyInfo.Key;
+        }
+
+        return MapToKeyCodeModifiers (keyInfo.Modifiers, (KeyCode)keyInfo.KeyChar);
+    }
+
     private static async Task ProcessButtonClickedAsync ()
     {
         await Task.Delay (300);

+ 1 - 1
Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs

@@ -783,7 +783,7 @@ internal class CursesDriver : ConsoleDriver
             case UnixMainLoop.EventType.Key:
                 ConsoleKeyInfo consoleKeyInfo = inputEvent.KeyEvent;
 
-                KeyCode map = ConsoleKeyMapping.MapConsoleKeyInfoToKeyCode (consoleKeyInfo);
+                KeyCode map = AnsiEscapeSequenceRequestUtils.MapKey (consoleKeyInfo);
 
                 if (map == KeyCode.Null)
                 {

+ 1 - 101
Terminal.Gui/ConsoleDrivers/NetDriver/NetDriver.cs

@@ -315,7 +315,7 @@ internal class NetDriver : ConsoleDriver
 
                 //Debug.WriteLine ($"event: {inputEvent}");
 
-                KeyCode map = MapKey (consoleKeyInfo);
+                KeyCode map = AnsiEscapeSequenceRequestUtils.MapKey (consoleKeyInfo);
 
                 if (map == KeyCode.Null)
                 {
@@ -722,106 +722,6 @@ internal class NetDriver : ConsoleDriver
         return new (cKeyInfo.KeyChar, cKeyInfo.Key, shift, alt, control);
     }
 
-    private KeyCode MapKey (ConsoleKeyInfo keyInfo)
-    {
-        switch (keyInfo.Key)
-        {
-            case ConsoleKey.OemPeriod:
-            case ConsoleKey.OemComma:
-            case ConsoleKey.OemPlus:
-            case ConsoleKey.OemMinus:
-            case ConsoleKey.Packet:
-            case ConsoleKey.Oem1:
-            case ConsoleKey.Oem2:
-            case ConsoleKey.Oem3:
-            case ConsoleKey.Oem4:
-            case ConsoleKey.Oem5:
-            case ConsoleKey.Oem6:
-            case ConsoleKey.Oem7:
-            case ConsoleKey.Oem8:
-            case ConsoleKey.Oem102:
-                if (keyInfo.KeyChar == 0)
-                {
-                    // If the keyChar is 0, keyInfo.Key value is not a printable character. 
-
-                    return KeyCode.Null; // MapToKeyCodeModifiers (keyInfo.Modifiers, KeyCode)keyInfo.Key);
-                }
-
-                if (keyInfo.Modifiers != ConsoleModifiers.Shift)
-                {
-                    // If Shift wasn't down we don't need to do anything but return the keyInfo.KeyChar
-                    return MapToKeyCodeModifiers (keyInfo.Modifiers, (KeyCode)keyInfo.KeyChar);
-                }
-
-                // Strip off Shift - We got here because they KeyChar from Windows is the shifted char (e.g. "Ç")
-                // and passing on Shift would be redundant.
-                return MapToKeyCodeModifiers (keyInfo.Modifiers & ~ConsoleModifiers.Shift, (KeyCode)keyInfo.KeyChar);
-        }
-
-        // Handle control keys whose VK codes match the related ASCII value (those below ASCII 33) like ESC
-        if (keyInfo.Key != ConsoleKey.None && Enum.IsDefined (typeof (KeyCode), (uint)keyInfo.Key))
-        {
-            if (keyInfo.Modifiers.HasFlag (ConsoleModifiers.Control) && keyInfo.Key == ConsoleKey.I)
-            {
-                return KeyCode.Tab;
-            }
-
-            return MapToKeyCodeModifiers (keyInfo.Modifiers, (KeyCode)(uint)keyInfo.Key);
-        }
-
-        // Handle control keys (e.g. CursorUp)
-        if (keyInfo.Key != ConsoleKey.None
-            && Enum.IsDefined (typeof (KeyCode), (uint)keyInfo.Key + (uint)KeyCode.MaxCodePoint))
-        {
-            return MapToKeyCodeModifiers (keyInfo.Modifiers, (KeyCode)((uint)keyInfo.Key + (uint)KeyCode.MaxCodePoint));
-        }
-
-        if ((ConsoleKey)keyInfo.KeyChar is >= ConsoleKey.A and <= ConsoleKey.Z)
-        {
-            // Shifted
-            keyInfo = new (
-                           keyInfo.KeyChar,
-                           (ConsoleKey)keyInfo.KeyChar,
-                           true,
-                           keyInfo.Modifiers.HasFlag (ConsoleModifiers.Alt),
-                           keyInfo.Modifiers.HasFlag (ConsoleModifiers.Control));
-        }
-
-        if ((ConsoleKey)keyInfo.KeyChar - 32 is >= ConsoleKey.A and <= ConsoleKey.Z)
-        {
-            // Unshifted
-            keyInfo = new (
-                           keyInfo.KeyChar,
-                           (ConsoleKey)(keyInfo.KeyChar - 32),
-                           false,
-                           keyInfo.Modifiers.HasFlag (ConsoleModifiers.Alt),
-                           keyInfo.Modifiers.HasFlag (ConsoleModifiers.Control));
-        }
-
-        if (keyInfo.Key is >= ConsoleKey.A and <= ConsoleKey.Z)
-        {
-            if (keyInfo.Modifiers.HasFlag (ConsoleModifiers.Alt)
-                || keyInfo.Modifiers.HasFlag (ConsoleModifiers.Control))
-            {
-                // NetDriver doesn't support Shift-Ctrl/Shift-Alt combos
-                return MapToKeyCodeModifiers (keyInfo.Modifiers & ~ConsoleModifiers.Shift, (KeyCode)keyInfo.Key);
-            }
-
-            if (keyInfo.Modifiers == ConsoleModifiers.Shift)
-            {
-                // If ShiftMask is on  add the ShiftMask
-                if (char.IsUpper (keyInfo.KeyChar))
-                {
-                    return (KeyCode)keyInfo.Key | KeyCode.ShiftMask;
-                }
-            }
-
-            return (KeyCode)keyInfo.Key;
-        }
-
-        return MapToKeyCodeModifiers (keyInfo.Modifiers, (KeyCode)keyInfo.KeyChar);
-    }
-
     #endregion Keyboard Handling
 
     #region Low-Level DotNet tuff