Skip to content

Commit

Permalink
[#334] Fix keyboard on macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Pick committed May 16, 2022
1 parent e16fbc2 commit ebe435b
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Emulator/Main/Peripherals/Input/KeyScanCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public enum KeyScanCode
OemSemicolon = 0x33,
OemQuotes = 0x34,
CapsLock = 0x39,
OemPeriod= 0x36,
OemPeriod = 0x36,
OemComma = 0x37,
OemQuestion = 0x38,
NumLock = 0x53,
Expand Down
1 change: 1 addition & 0 deletions src/UI/UI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
<Compile Include="VideoAnalyzer\Events\X11EventSource.cs" />
<Compile Include="VideoAnalyzer\Events\XWTEventSource.cs" />
<Compile Include="VideoAnalyzer\Events\IEventSource.cs" />
<Compile Include="VideoAnalyzer\Events\OSXToKeyScanCodeConverter.cs" />
<Compile Include="VideoAnalyzer\Events\X11ToKeyScanCodeConverter.cs" />
<Compile Include="VideoAnalyzer\Events\WPFToKeyScanCodeConverter.cs" />
<Compile Include="VideoAnalyzer\VideoAnalyzer.cs" />
Expand Down
171 changes: 171 additions & 0 deletions src/UI/VideoAnalyzer/Events/OSXToKeyScanCodeConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
//
// Copyright (c) 2010-2018 Antmicro
// Copyright (c) 2011-2015 Realtime Embedded
//
// This file is licensed under the MIT License.
// Full license text is available in 'licenses/MIT.txt'.
//
using Antmicro.Renode.Peripherals.Input;
using System.Collections.Generic;
using Xwt;

namespace Antmicro.Renode.Extensions.Analyzers.Video.Events
{
internal class OSXToKeyScanCodeConverter
{
static OSXToKeyScanCodeConverter()
{
Instance = new OSXToKeyScanCodeConverter();
}

public static OSXToKeyScanCodeConverter Instance { get; private set; }

public KeyScanCode? GetScanCode(Key key)
{
KeyScanCode result;
return UntranslatedScanCode.TryGetValue(key, out result) ? (KeyScanCode?)result : null;
}

private readonly Dictionary<Key, KeyScanCode> UntranslatedScanCode = new Dictionary<Key, KeyScanCode>
{
{ Key.AltLeft, KeyScanCode.AltL },
{ Key.MetaLeft, KeyScanCode.WinL },
{ Key.Space, KeyScanCode.Space },
{ Key.AltRight, KeyScanCode.AltR },
{ Key.MetaRight, KeyScanCode.WinR },
{ Key.Menu, KeyScanCode.WinMenu },
{ Key.Z, KeyScanCode.Z },
{ Key.z, KeyScanCode.Z },
{ Key.X, KeyScanCode.X },
{ Key.x, KeyScanCode.X },
{ Key.C, KeyScanCode.C },
{ Key.c, KeyScanCode.C },
{ Key.V, KeyScanCode.V },
{ Key.v, KeyScanCode.V },
{ Key.B, KeyScanCode.B },
{ Key.b, KeyScanCode.B },
{ Key.N, KeyScanCode.N },
{ Key.n, KeyScanCode.N },
{ Key.M, KeyScanCode.M },
{ Key.m, KeyScanCode.M },
{ Key.Period, KeyScanCode.OemPeriod },
{ Key.Comma, KeyScanCode.OemComma },
{ Key.Question, KeyScanCode.OemQuestion },
{ Key.ShiftRight, KeyScanCode.ShiftR },

{ Key.CapsLock, KeyScanCode.CapsLock },
{ Key.A, KeyScanCode.A },
{ Key.a, KeyScanCode.A },
{ Key.S, KeyScanCode.S },
{ Key.s, KeyScanCode.S },
{ Key.D, KeyScanCode.D },
{ Key.d, KeyScanCode.D },
{ Key.F, KeyScanCode.F },
{ Key.f, KeyScanCode.F },
{ Key.G, KeyScanCode.G },
{ Key.g, KeyScanCode.G },
{ Key.H, KeyScanCode.H },
{ Key.h, KeyScanCode.H },
{ Key.J, KeyScanCode.J },
{ Key.j, KeyScanCode.J },
{ Key.K, KeyScanCode.K },
{ Key.k, KeyScanCode.K },
{ Key.L, KeyScanCode.L },
{ Key.l, KeyScanCode.L },
{ Key.Semicolon, KeyScanCode.OemSemicolon },
{ Key.Quote, KeyScanCode.OemQuotes },
{ Key.Return, KeyScanCode.Enter },

{ Key.Tab, KeyScanCode.Tab },
{ Key.Q, KeyScanCode.Q },
{ Key.q, KeyScanCode.Q },
{ Key.W, KeyScanCode.W },
{ Key.w, KeyScanCode.W },
{ Key.E, KeyScanCode.E },
{ Key.e, KeyScanCode.E },
{ Key.R, KeyScanCode.R },
{ Key.r, KeyScanCode.R },
{ Key.T, KeyScanCode.T },
{ Key.t, KeyScanCode.T },
{ Key.Y, KeyScanCode.Y },
{ Key.y, KeyScanCode.Y },
{ Key.U, KeyScanCode.U },
{ Key.u, KeyScanCode.U },
{ Key.I, KeyScanCode.I },
{ Key.i, KeyScanCode.I },
{ Key.O, KeyScanCode.O },
{ Key.o, KeyScanCode.O },
{ Key.P, KeyScanCode.P },
{ Key.p, KeyScanCode.P },
{ Key.OpenCurlyBracket, KeyScanCode.OemOpenBrackets },
{ Key.OpenSquareBracket, KeyScanCode.OemOpenBrackets },
{ Key.CloseCurlyBracket, KeyScanCode.OemCloseBrackets },
{ Key.CloseSquareBracket, KeyScanCode.OemCloseBrackets },

{ Key.Tilde, KeyScanCode.Tilde },
{ Key.BackQuote, KeyScanCode.Tilde },
{ Key.K1, KeyScanCode.Number1 },
{ Key.K2, KeyScanCode.Number2 },
{ Key.K3, KeyScanCode.Number3 },
{ Key.K4, KeyScanCode.Number4 },
{ Key.K5, KeyScanCode.Number5 },
{ Key.K6, KeyScanCode.Number6 },
{ Key.K7, KeyScanCode.Number7 },
{ Key.K8, KeyScanCode.Number8 },
{ Key.K9, KeyScanCode.Number9 },
{ Key.K0, KeyScanCode.Number0 },
{ Key.Minus, KeyScanCode.OemMinus },
{ Key.Underscore, KeyScanCode.OemMinus },
{ Key.Plus, KeyScanCode.OemPlus },
{ Key.Equal, KeyScanCode.OemPlus },
{ Key.Backslash, KeyScanCode.OemPipe },
{ Key.Pipe, KeyScanCode.OemPipe },
{ Key.BackSpace, KeyScanCode.BackSpace },

{ Key.Escape, KeyScanCode.Escape },
{ Key.F1, KeyScanCode.F1 },
{ Key.F2, KeyScanCode.F2 },
{ Key.F3, KeyScanCode.F3 },
{ Key.F4, KeyScanCode.F4 },
{ Key.F5, KeyScanCode.F5 },
{ Key.F6, KeyScanCode.F6 },
{ Key.F7, KeyScanCode.F7 },
{ Key.F8, KeyScanCode.F8 },
{ Key.F9, KeyScanCode.F9 },
{ Key.F10, KeyScanCode.F10 },
{ Key.Print, KeyScanCode.PrtSc },
{ Key.ScrollLock, KeyScanCode.ScrollLock },
{ Key.Pause, KeyScanCode.Pause },

{ Key.Insert, KeyScanCode.Insert },
{ Key.Home, KeyScanCode.Home },
{ Key.PageUp, KeyScanCode.PageUp },
{ Key.Delete, KeyScanCode.Delete },
{ Key.End, KeyScanCode.End },
{ Key.PageDown, KeyScanCode.PageDown },

{ Key.Up, KeyScanCode.Up },
{ Key.Down, KeyScanCode.Down },
{ Key.Left, KeyScanCode.Left },
{ Key.Right, KeyScanCode.Right },

{ Key.NumLock, KeyScanCode.NumLock },
{ Key.NumPad0, KeyScanCode.Keypad0 },
{ Key.NumPad1, KeyScanCode.Keypad1 },
{ Key.NumPad2, KeyScanCode.Keypad2 },
{ Key.NumPad3, KeyScanCode.Keypad3 },
{ Key.NumPad4, KeyScanCode.Keypad4 },
{ Key.NumPad5, KeyScanCode.Keypad5 },
{ Key.NumPad6, KeyScanCode.Keypad6 },
{ Key.NumPad7, KeyScanCode.Keypad7 },
{ Key.NumPad8, KeyScanCode.Keypad8 },
{ Key.NumPad9, KeyScanCode.Keypad9 },
{ Key.NumPadDivide, KeyScanCode.KeypadDivide },
{ Key.NumPadMultiply, KeyScanCode.KeypadMultiply },
{ Key.NumPadSubtract, KeyScanCode.KeypadMinus },
{ Key.NumPadAdd, KeyScanCode.KeypadPlus },
{ Key.NumPadDecimal, KeyScanCode.KeypadComma },
{ Key.NumPadEnter, KeyScanCode.KeypadEnter },
};
}
}
8 changes: 6 additions & 2 deletions src/UI/VideoAnalyzer/Events/XWTEventSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ private void HandleKeyReleased(object sender, KeyEventArgs e)
var vks = VkKeyScanEx((char)e.Key, keyboardLayout);
var vsc = MapVirtualKeyEx((uint)vks & 0xff, MAPVK_VK_TO_VSC, keyboardLayout);
var key = WPFToKeyScanCodeConverter.Instance.GetScanCode((int)vsc, e.Key);
#elif PLATFORM_OSX
var key = OSXToKeyScanCodeConverter.Instance.GetScanCode(e.Key);
#else
var entryKey = Gdk.Keymap.Default.GetEntriesForKeyval((uint)e.Key)[0].Keycode;

var key = X11ToKeyScanCodeConverter.Instance.GetScanCode((int)entryKey);
#endif // !PLATFORM_WINDOWS
#endif // !(PLATFORM_WINDOWS || PLATFORM_OSX)
if(key != null)
{
handler.KeyReleased(key.Value);
Expand All @@ -63,11 +65,13 @@ private void HandleKeyPressed(object sender, KeyEventArgs e)
var vks = VkKeyScanEx((char)e.Key, keyboardLayout);
var vsc = MapVirtualKeyEx((uint)vks & 0xff, MAPVK_VK_TO_VSC, keyboardLayout);
var key = WPFToKeyScanCodeConverter.Instance.GetScanCode((int)vsc, e.Key);
#elif PLATFORM_OSX
var key = OSXToKeyScanCodeConverter.Instance.GetScanCode(e.Key);
#else
var entryKey = Gdk.Keymap.Default.GetEntriesForKeyval((uint)e.Key)[0].Keycode;

var key = X11ToKeyScanCodeConverter.Instance.GetScanCode((int)entryKey);
#endif // !PLATFORM_WINDOWS
#endif // !(PLATFORM_WINDOWS || PLATFORM_OSX)
if(key != null)
{
handler.KeyPressed(key.Value);
Expand Down

0 comments on commit ebe435b

Please sign in to comment.