From b10ae8852f74919cb03559f3f793d6a2e79e1d12 Mon Sep 17 00:00:00 2001 From: Julien Lebosquain Date: Tue, 15 Aug 2023 12:43:21 +0200 Subject: [PATCH] Fix key symbols for macOS with modifiers --- native/Avalonia.Native/src/OSX/KeyTransform.h | 2 +- native/Avalonia.Native/src/OSX/KeyTransform.mm | 10 +++++++++- src/Avalonia.Base/Input/KeyEventArgs.cs | 16 ++++++++-------- .../Avalonia.Win32/Interop/UnmanagedMethods.cs | 4 +--- 4 files changed, 19 insertions(+), 13 deletions(-) diff --git a/native/Avalonia.Native/src/OSX/KeyTransform.h b/native/Avalonia.Native/src/OSX/KeyTransform.h index 587a9c00cd0..95b483ea9a9 100644 --- a/native/Avalonia.Native/src/OSX/KeyTransform.h +++ b/native/Avalonia.Native/src/OSX/KeyTransform.h @@ -8,7 +8,7 @@ AvnPhysicalKey PhysicalKeyFromScanCode(uint16_t scanCode); AvnKey VirtualKeyFromScanCode(uint16_t scanCode, NSEventModifierFlags modifierFlags); -NSString* KeySymbolFromScanCode(uint16_t scanCode); +NSString* KeySymbolFromScanCode(uint16_t scanCode, NSEventModifierFlags modifierFlags); uint16_t MenuCharFromVirtualKey(AvnKey key); diff --git a/native/Avalonia.Native/src/OSX/KeyTransform.mm b/native/Avalonia.Native/src/OSX/KeyTransform.mm index f790f98a143..86c62b20679 100644 --- a/native/Avalonia.Native/src/OSX/KeyTransform.mm +++ b/native/Avalonia.Native/src/OSX/KeyTransform.mm @@ -450,12 +450,20 @@ static UniCharCount CharsFromScanCode(UInt16 scanCode, NSEventModifierFlags modi UInt32 deadKeyState = 0; UniCharCount length = 0; + + int glyphModifiers = 0; + if (modifierFlags & NSEventModifierFlagShift) + glyphModifiers |= shiftKey; + if (modifierFlags & NSEventModifierFlagCapsLock) + glyphModifiers |= alphaLock; + if (modifierFlags & NSEventModifierFlagOption) + glyphModifiers |= optionKey; auto result = UCKeyTranslate( keyboardLayout, scanCode, keyAction, - (modifierFlags >> 8) & 0xFF, + (glyphModifiers >> 8) & 0xFF, LMGetKbdType(), kUCKeyTranslateNoDeadKeysBit, &deadKeyState, diff --git a/src/Avalonia.Base/Input/KeyEventArgs.cs b/src/Avalonia.Base/Input/KeyEventArgs.cs index 48b279f4186..3de078fb37b 100644 --- a/src/Avalonia.Base/Input/KeyEventArgs.cs +++ b/src/Avalonia.Base/Input/KeyEventArgs.cs @@ -16,10 +16,10 @@ public class KeyEventArgs : RoutedEventArgs /// /// For example, when pressing the key located at the Z position on standard US English QWERTY keyboard, /// this property returns:
- /// - for an English (QWERTY) layout
- /// - for a French (AZERTY) layout
- /// - for a German (QWERTZ) layout
- /// - for a Russian (JCUKEN) layout + /// - for an English (QWERTY) layout
+ /// - for a French (AZERTY) layout
+ /// - for a German (QWERTZ) layout
+ /// - for a Russian (JCUKEN) layout ///
/// /// @@ -62,10 +62,10 @@ public class KeyEventArgs : RoutedEventArgs /// /// For example, when pressing the key located at the Z position on standard US English QWERTY keyboard, /// this property returns:
- /// - Z for an English (QWERTY) layout
- /// - W for a French (AZERTY) layout
- /// - Y for a German (QWERTZ) layout
- /// - Я for a Russian (JCUKEN) layout + /// - z for an English (QWERTY) layout
+ /// - w for a French (AZERTY) layout
+ /// - y for a German (QWERTZ) layout
+ /// - я for a Russian (JCUKEN) layout ///
/// /// diff --git a/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs b/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs index 7d866d65d38..445bec979e5 100644 --- a/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs +++ b/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs @@ -1205,8 +1205,7 @@ public static extern IntPtr CreateWindowEx( [DllImport("user32.dll")] public static extern uint GetDoubleClickTime(); - [DllImport("USER32.dll", ExactSpelling = true, SetLastError = true)] - [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] + [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool GetKeyboardState(byte* lpKeyState); @@ -1404,7 +1403,6 @@ public static extern bool CreateTimerQueueTimer( public static extern bool DeleteTimerQueueTimer(IntPtr TimerQueue, IntPtr Timer, IntPtr CompletionEvent); [DllImport("user32.dll", ExactSpelling = true)] - [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] public static extern int ToUnicodeEx( uint wVirtKey, uint wScanCode,