Skip to content

Commit edca3eb

Browse files
authored
macOS: Align scancode conversions with Chromium and Firefox (#4019)
Also fix missing codes in physicalkey_to_scancode - This had become out of sync with scancode_to_physicalkey.
1 parent ca46e29 commit edca3eb

File tree

2 files changed

+45
-25
lines changed

2 files changed

+45
-25
lines changed

src/changelog/unreleased.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ changelog entry.
7575
variables to test the respective modifiers of window creation.
7676
- Added `Window::surface_position`, which is the position of the surface inside the window.
7777
- Added `Window::safe_area`, which describes the area of the surface that is unobstructed.
78-
- On X11 and Wayland, improved scancode conversions for more obscure key codes.
79-
- On Windows, improved scancode conversions for more obscure key codes.
78+
- On X11, Wayland, Windows and macOS, improved scancode conversions for more obscure key codes.
8079

8180
### Changed
8281

@@ -210,3 +209,5 @@ changelog entry.
210209
- On X11, creating windows while passing `with_x11_screen(non_default_screen)` works again.
211210
- On X11, fix XInput handling that prevented a new window from getting the focus in some cases.
212211
- On iOS, fixed `SurfaceResized` and `Window::surface_size` not reporting the size of the actual surface.
212+
- On macOS, fixed the scancode conversion for audio volume keys.
213+
- On macOS, fixed the scancode conversion for `IntlBackslash`.

src/platform_impl/apple/appkit/event.rs

+42-23
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ pub(crate) fn physicalkey_to_scancode(physical_key: PhysicalKey) -> Option<u32>
377377
KeyCode::KeyX => Some(0x07),
378378
KeyCode::KeyC => Some(0x08),
379379
KeyCode::KeyV => Some(0x09),
380+
KeyCode::IntlBackslash => Some(0x0a),
380381
KeyCode::KeyB => Some(0x0b),
381382
KeyCode::KeyQ => Some(0x0c),
382383
KeyCode::KeyW => Some(0x0d),
@@ -422,18 +423,21 @@ pub(crate) fn physicalkey_to_scancode(physical_key: PhysicalKey) -> Option<u32>
422423
KeyCode::SuperRight => Some(0x36),
423424
KeyCode::SuperLeft => Some(0x37),
424425
KeyCode::ShiftLeft => Some(0x38),
426+
KeyCode::CapsLock => Some(0x39),
425427
KeyCode::AltLeft => Some(0x3a),
426428
KeyCode::ControlLeft => Some(0x3b),
427429
KeyCode::ShiftRight => Some(0x3c),
428430
KeyCode::AltRight => Some(0x3d),
429431
KeyCode::ControlRight => Some(0x3e),
432+
KeyCode::Fn => Some(0x3f),
430433
KeyCode::F17 => Some(0x40),
431434
KeyCode::NumpadDecimal => Some(0x41),
432435
KeyCode::NumpadMultiply => Some(0x43),
433436
KeyCode::NumpadAdd => Some(0x45),
434437
KeyCode::NumLock => Some(0x47),
435-
KeyCode::AudioVolumeUp => Some(0x49),
436-
KeyCode::AudioVolumeDown => Some(0x4a),
438+
KeyCode::AudioVolumeUp => Some(0x48),
439+
KeyCode::AudioVolumeDown => Some(0x49),
440+
KeyCode::AudioVolumeMute => Some(0x4a),
437441
KeyCode::NumpadDivide => Some(0x4b),
438442
KeyCode::NumpadEnter => Some(0x4c),
439443
KeyCode::NumpadSubtract => Some(0x4e),
@@ -452,17 +456,22 @@ pub(crate) fn physicalkey_to_scancode(physical_key: PhysicalKey) -> Option<u32>
452456
KeyCode::Numpad8 => Some(0x5b),
453457
KeyCode::Numpad9 => Some(0x5c),
454458
KeyCode::IntlYen => Some(0x5d),
459+
KeyCode::IntlRo => Some(0x5e),
460+
KeyCode::NumpadComma => Some(0x5f),
455461
KeyCode::F5 => Some(0x60),
456462
KeyCode::F6 => Some(0x61),
457463
KeyCode::F7 => Some(0x62),
458464
KeyCode::F3 => Some(0x63),
459465
KeyCode::F8 => Some(0x64),
460466
KeyCode::F9 => Some(0x65),
467+
KeyCode::Lang2 => Some(0x66),
461468
KeyCode::F11 => Some(0x67),
469+
KeyCode::Lang1 => Some(0x68),
462470
KeyCode::F13 => Some(0x69),
463471
KeyCode::F16 => Some(0x6a),
464472
KeyCode::F14 => Some(0x6b),
465473
KeyCode::F10 => Some(0x6d),
474+
KeyCode::ContextMenu => Some(0x6e),
466475
KeyCode::F12 => Some(0x6f),
467476
KeyCode::F15 => Some(0x71),
468477
KeyCode::Insert => Some(0x72),
@@ -478,11 +487,26 @@ pub(crate) fn physicalkey_to_scancode(physical_key: PhysicalKey) -> Option<u32>
478487
KeyCode::ArrowRight => Some(0x7c),
479488
KeyCode::ArrowDown => Some(0x7d),
480489
KeyCode::ArrowUp => Some(0x7e),
490+
KeyCode::Power => Some(0x7f),
481491
_ => None,
482492
}
483493
}
484494

485495
pub(crate) fn scancode_to_physicalkey(scancode: u32) -> PhysicalKey {
496+
// Follows what Chromium and Firefox do:
497+
// https://chromium.googlesource.com/chromium/src.git/+/3e1a26c44c024d97dc9a4c09bbc6a2365398ca2c/ui/events/keycodes/dom/dom_code_data.inc
498+
// https://searchfox.org/mozilla-central/rev/c597e9c789ad36af84a0370d395be066b7dc94f4/widget/NativeKeyToDOMCodeName.h
499+
//
500+
// See also:
501+
// Carbon.framework/Versions/A/Frameworks/HIToolbox.framework/Versions/A/Headers/Events.h
502+
//
503+
// Also see https://developer.apple.com/documentation/appkit/function-key-unicode-values:
504+
//
505+
// > the system handles some function keys at a lower level and your app never sees them.
506+
// > Examples include the Volume Up key, Volume Down key, Volume Mute key, Eject key, and
507+
// > Function key found on many Macs.
508+
//
509+
// So the handling of some of these is mostly for show.
486510
PhysicalKey::Code(match scancode {
487511
0x00 => KeyCode::KeyA,
488512
0x01 => KeyCode::KeyS,
@@ -494,7 +518,11 @@ pub(crate) fn scancode_to_physicalkey(scancode: u32) -> PhysicalKey {
494518
0x07 => KeyCode::KeyX,
495519
0x08 => KeyCode::KeyC,
496520
0x09 => KeyCode::KeyV,
497-
// 0x0a => World 1,
521+
// This key is typically located near LeftShift key, roughly the same location as backquote
522+
// (`) on Windows' US layout.
523+
//
524+
// The keycap varies on international keyboards.
525+
0x0a => KeyCode::IntlBackslash,
498526
0x0b => KeyCode::KeyB,
499527
0x0c => KeyCode::KeyQ,
500528
0x0d => KeyCode::KeyW,
@@ -536,7 +564,7 @@ pub(crate) fn scancode_to_physicalkey(scancode: u32) -> PhysicalKey {
536564
0x31 => KeyCode::Space,
537565
0x32 => KeyCode::Backquote,
538566
0x33 => KeyCode::Backspace,
539-
// 0x34 => unknown,
567+
// 0x34 => unknown, // kVK_Powerbook_KeypadEnter
540568
0x35 => KeyCode::Escape,
541569
0x36 => KeyCode::SuperRight,
542570
0x37 => KeyCode::SuperLeft,
@@ -555,15 +583,10 @@ pub(crate) fn scancode_to_physicalkey(scancode: u32) -> PhysicalKey {
555583
// 0x44 => unknown,
556584
0x45 => KeyCode::NumpadAdd,
557585
// 0x46 => unknown,
558-
0x47 => KeyCode::NumLock,
559-
// 0x48 => KeyCode::NumpadClear,
560-
561-
// TODO: (Artur) for me, kVK_VolumeUp is 0x48
562-
// macOS 10.11
563-
// /System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/HIToolbox.framework/
564-
// Versions/A/Headers/Events.h
565-
0x49 => KeyCode::AudioVolumeUp,
566-
0x4a => KeyCode::AudioVolumeDown,
586+
0x47 => KeyCode::NumLock, // kVK_ANSI_KeypadClear
587+
0x48 => KeyCode::AudioVolumeUp,
588+
0x49 => KeyCode::AudioVolumeDown,
589+
0x4a => KeyCode::AudioVolumeMute,
567590
0x4b => KeyCode::NumpadDivide,
568591
0x4c => KeyCode::NumpadEnter,
569592
// 0x4d => unknown,
@@ -583,23 +606,23 @@ pub(crate) fn scancode_to_physicalkey(scancode: u32) -> PhysicalKey {
583606
0x5b => KeyCode::Numpad8,
584607
0x5c => KeyCode::Numpad9,
585608
0x5d => KeyCode::IntlYen,
586-
// 0x5e => JIS Ro,
587-
// 0x5f => unknown,
609+
0x5e => KeyCode::IntlRo,
610+
0x5f => KeyCode::NumpadComma,
588611
0x60 => KeyCode::F5,
589612
0x61 => KeyCode::F6,
590613
0x62 => KeyCode::F7,
591614
0x63 => KeyCode::F3,
592615
0x64 => KeyCode::F8,
593616
0x65 => KeyCode::F9,
594-
// 0x66 => JIS Eisuu (macOS),
617+
0x66 => KeyCode::Lang2,
595618
0x67 => KeyCode::F11,
596-
// 0x68 => JIS Kanna (macOS),
619+
0x68 => KeyCode::Lang1,
597620
0x69 => KeyCode::F13,
598621
0x6a => KeyCode::F16,
599622
0x6b => KeyCode::F14,
600623
// 0x6c => unknown,
601624
0x6d => KeyCode::F10,
602-
// 0x6e => unknown,
625+
0x6e => KeyCode::ContextMenu,
603626
0x6f => KeyCode::F12,
604627
// 0x70 => unknown,
605628
0x71 => KeyCode::F15,
@@ -616,11 +639,7 @@ pub(crate) fn scancode_to_physicalkey(scancode: u32) -> PhysicalKey {
616639
0x7c => KeyCode::ArrowRight,
617640
0x7d => KeyCode::ArrowDown,
618641
0x7e => KeyCode::ArrowUp,
619-
// 0x7f => unknown,
620-
621-
// 0xA is the caret (^) an macOS's German QERTZ layout. This key is at the same location as
622-
// backquote (`) on Windows' US layout.
623-
0xa => KeyCode::Backquote,
642+
0x7f => KeyCode::Power, // On 10.7 and 10.8 only
624643
_ => return PhysicalKey::Unidentified(NativeKeyCode::MacOS(scancode as u16)),
625644
})
626645
}

0 commit comments

Comments
 (0)