Skip to content

Commit

Permalink
Key handling improvements (#12549)
Browse files Browse the repository at this point in the history
* Physical key handling for Windows

* Physical key handling for macOS

* Physical key handling for X11

* Physical keys: cleanup unused keys

* Key symbols: ensure consistent behavior between platforms

* Fix dead key symbol for Windows

* Physical key handling for browser

* Physical keys: use new overloads where possible

* Key symbol for VNC

* Physical key handling in previewer

* Key symbol for forwarded X11 IME key

* Key symbol for Android

* Obsolete old RawKeyEventArgs ctor

* Fix key symbols for macOS with modifiers

* Adjust PhysicalKey members naming

* Use explicit std::hash for AvnKey/AvnPhysicalKey

Should hopefully satisfy the older compiler on the CI server

* Headless: added KeyPressQwerty

---------

Co-authored-by: Dan Walmsley <dan@walms.co.uk>
Co-authored-by: Steven Kirk <grokys@users.noreply.github.com>
  • Loading branch information
3 people authored and maxkatz6 committed Dec 5, 2023
1 parent 9848d55 commit b2ef353
Show file tree
Hide file tree
Showing 40 changed files with 4,078 additions and 1,584 deletions.
23 changes: 12 additions & 11 deletions native/Avalonia.Native/src/OSX/AvnView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -447,24 +447,25 @@ - (void)mouseExited:(NSEvent *)event

- (void) keyboardEvent: (NSEvent *) event withType: (AvnRawKeyEventType)type
{
if([self ignoreUserInput: false])
if([self ignoreUserInput: false] || _parent == nullptr)
{
return;
}

auto key = s_KeyMap[[event keyCode]];
auto scanCode = [event keyCode];
auto key = VirtualKeyFromScanCode(scanCode, [event modifierFlags]);
auto physicalKey = PhysicalKeyFromScanCode(scanCode);
auto keySymbol = KeySymbolFromScanCode(scanCode, [event modifierFlags]);
auto keySymbolUtf8 = keySymbol == nullptr ? nullptr : [keySymbol UTF8String];

uint64_t timestamp = static_cast<uint64_t>([event timestamp] * 1000);
auto timestamp = static_cast<uint64_t>([event timestamp] * 1000);
auto modifiers = [self getModifiers:[event modifierFlags]];

if(_parent != nullptr)
{
auto handled = _parent->BaseEvents->RawKeyEvent(type, timestamp, modifiers, key);
if (key != LeftCtrl && key != RightCtrl) {
_lastKeyHandled = handled;
} else {
_lastKeyHandled = false;
}
auto handled = _parent->BaseEvents->RawKeyEvent(type, timestamp, modifiers, key, physicalKey, keySymbolUtf8);
if (key != AvnKeyLeftCtrl && key != AvnKeyRightCtrl) {
_lastKeyHandled = handled;
} else {
_lastKeyHandled = false;
}
}

Expand Down
11 changes: 6 additions & 5 deletions native/Avalonia.Native/src/OSX/KeyTransform.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#ifndef keytransform_h
#define keytransform_h

#import <cstdint>
#include "common.h"
#include <map>

extern std::map<int, AvnKey> s_KeyMap;
AvnPhysicalKey PhysicalKeyFromScanCode(uint16_t scanCode);

extern std::map<AvnKey, int> s_AvnKeyMap;
AvnKey VirtualKeyFromScanCode(uint16_t scanCode, NSEventModifierFlags modifierFlags);

extern std::map<int, const char*> s_QwertyKeyMap;
NSString* KeySymbolFromScanCode(uint16_t scanCode, NSEventModifierFlags modifierFlags);

extern std::map<AvnKey, int> s_UnicodeKeyMap;
uint16_t MenuCharFromVirtualKey(AvnKey key);

#endif
Loading

0 comments on commit b2ef353

Please sign in to comment.