Skip to content

Commit

Permalink
Fix key symbols for macOS with modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
MrJul committed Aug 17, 2023
1 parent 97410ec commit b10ae88
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion native/Avalonia.Native/src/OSX/KeyTransform.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
10 changes: 9 additions & 1 deletion native/Avalonia.Native/src/OSX/KeyTransform.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 8 additions & 8 deletions src/Avalonia.Base/Input/KeyEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ public class KeyEventArgs : RoutedEventArgs
/// <para>
/// For example, when pressing the key located at the <c>Z</c> position on standard US English QWERTY keyboard,
/// this property returns:<br/>
/// - <see cref="Input.PhysicalKey.KeyZ"/> for an English (QWERTY) layout<br/>
/// - <see cref="Input.PhysicalKey.KeyW"/> for a French (AZERTY) layout<br/>
/// - <see cref="Input.PhysicalKey.KeyY"/> for a German (QWERTZ) layout<br/>
/// - <see cref="Input.PhysicalKey.KeyZ"/> for a Russian (JCUKEN) layout
/// - <see cref="Input.Key.Z"/> for an English (QWERTY) layout<br/>
/// - <see cref="Input.Key.W"/> for a French (AZERTY) layout<br/>
/// - <see cref="Input.Key.Y"/> for a German (QWERTZ) layout<br/>
/// - <see cref="Input.Key.Z"/> for a Russian (JCUKEN) layout
/// </para>
/// </summary>
/// <remarks>
Expand Down Expand Up @@ -62,10 +62,10 @@ public class KeyEventArgs : RoutedEventArgs
/// <para>
/// For example, when pressing the key located at the <c>Z</c> position on standard US English QWERTY keyboard,
/// this property returns:<br/>
/// - <c>Z</c> for an English (QWERTY) layout<br/>
/// - <c>W</c> for a French (AZERTY) layout<br/>
/// - <c>Y</c> for a German (QWERTZ) layout<br/>
/// - <c>Я</c> for a Russian (JCUKEN) layout
/// - <c>z</c> for an English (QWERTY) layout<br/>
/// - <c>w</c> for a French (AZERTY) layout<br/>
/// - <c>y</c> for a German (QWERTZ) layout<br/>
/// - <c>я</c> for a Russian (JCUKEN) layout
/// </para>
/// </summary>
/// <see cref="Key"/>
Expand Down
4 changes: 1 addition & 3 deletions src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit b10ae88

Please sign in to comment.