Add macOS fn key support. (#44410)

This adds support for the fn key on macOS. It adds it to the key mappings as a supplemental mapping that overwrites the one from the Chrome headers, since the chrome headers have a TODO, but no implementation of the key.

Also, ignore the fn key entirely on macOS. This is because On macOS laptop keyboards, the fn key is used to generate home/end and f1-f12, but it ALSO generates a separate down/up event for the fn key itself. Other platforms hide the fn key, and just produce the key that it is combined with, so to keep it possible to write cross platform code that looks at which keys are pressed, the fn key is ignored.
This commit is contained in:
Greg Spencer
2019-11-08 16:30:38 -08:00
committed by GitHub
parent 9fa0f3b2ea
commit c921c5ae67
10 changed files with 167 additions and 48 deletions

View File

@@ -66,33 +66,6 @@
"glfw": null
}
},
"fn": {
"names": {
"domkey": "Fn",
"android": [
"FUNCTION"
],
"english": "Fn",
"chromium": "fn",
"glfw": null
},
"scanCodes": {
"android": [
464
],
"usb": 18,
"linux": null,
"xkb": null,
"windows": null,
"macos": null
},
"keyCodes": {
"android": [
119
],
"glfw": null
}
},
"fnLock": {
"names": {
"domkey": "FnLock",
@@ -7222,5 +7195,32 @@
],
"glfw": null
}
},
"fn": {
"names": {
"domkey": "Fn",
"android": [
"FUNCTION"
],
"english": "Fn",
"chromium": "fn",
"glfw": null
},
"scanCodes": {
"android": [
464
],
"usb": 18,
"linux": null,
"xkb": null,
"windows": null,
"macos": 63
},
"keyCodes": {
"android": [
119
],
"glfw": null
}
}
}

View File

@@ -238,6 +238,21 @@ class LogicalKeyboardKey extends KeyboardKey {
return result == null ? <LogicalKeyboardKey>{} : <LogicalKeyboardKey>{result};
}
/// Takes a set of keys, and returns the same set, but with any keys that have
/// synonyms replaced.
///
/// It is used, for example, to make sets of keys with members like
/// [controlRight] and [controlLeft] and convert that set to contain just
/// [control], so that the question "is any control key down?" can be asked.
static Set<LogicalKeyboardKey> collapseSynonyms(Set<LogicalKeyboardKey> input) {
final Set<LogicalKeyboardKey> result = <LogicalKeyboardKey>{};
for (LogicalKeyboardKey key in input) {
final LogicalKeyboardKey synonym = _synonyms[key];
result.add(synonym ?? key);
}
return result;
}
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);

View File

@@ -53,6 +53,12 @@ const Map<int, LogicalKeyboardKey> kMacOsNumPadMap = <int, LogicalKeyboardKey>{
@@@MACOS_NUMPAD_MAP@@@
};
/// A map of macOS key codes which are numbered function keys, so that they
/// can be excluded when asking "is the Fn modifier down?".
const Map<int, LogicalKeyboardKey> kMacOsFunctionKeyMap = <int, LogicalKeyboardKey>{
@@@MACOS_FUNCTION_KEY_MAP@@@
};
/// Maps GLFW-specific key codes to the matching [LogicalKeyboardKey].
const Map<int, LogicalKeyboardKey> kGlfwToLogicalKey = <int, LogicalKeyboardKey>{
@@@GLFW_KEY_CODE_MAP@@@

View File

@@ -43,3 +43,10 @@
USB_KEYMAP(0x05ff1d, 0x0000, 0x0000, 0x0000, 0xffff, "GameButtonX", BUTTON_X),
USB_KEYMAP(0x05ff1e, 0x0000, 0x0000, 0x0000, 0xffff, "GameButtonY", BUTTON_Y),
USB_KEYMAP(0x05ff1f, 0x0000, 0x0000, 0x0000, 0xffff, "GameButtonZ", BUTTON_Z),
// The Mac defines a key code for the Fn key on Mac keyboards, but it's not
// defined on other platforms. Chromium does define an "Fn" row, but doesn't
// give it a Mac keycode. This overrides their definition.
// USB HID evdev XKB Win Mac DOMKey Code
USB_KEYMAP(0x000012, 0x0000, 0x0000, 0x0000, 0x003f, "Fn", FN),

View File

@@ -114,6 +114,13 @@ $otherComments static const LogicalKeyboardKey $constantName = LogicalKeyboardK
}).toList();
}
List<Key> get functionKeyData {
final RegExp functionKeyRe = RegExp(r'^f[0-9]+$');
return keyData.data.where((Key entry) {
return functionKeyRe.hasMatch(entry.constantName);
}).toList();
}
/// This generates the map of USB HID codes to physical keys.
String get predefinedHidCodeMap {
final StringBuffer scanCodeMap = StringBuffer();
@@ -240,6 +247,16 @@ $otherComments static const LogicalKeyboardKey $constantName = LogicalKeyboardK
return macOsNumPadMap.toString().trimRight();
}
String get macOsFunctionKeyMap {
final StringBuffer macOsFunctionKeyMap = StringBuffer();
for (Key entry in functionKeyData) {
if (entry.macOsScanCode != null) {
macOsFunctionKeyMap.writeln(' ${toHex(entry.macOsScanCode)}: LogicalKeyboardKey.${entry.constantName},');
}
}
return macOsFunctionKeyMap.toString().trimRight();
}
/// This generates the map of Fuchsia key codes to logical keys.
String get fuchsiaKeyCodeMap {
final StringBuffer fuchsiaKeyCodeMap = StringBuffer();
@@ -324,6 +341,7 @@ $otherComments static const LogicalKeyboardKey $constantName = LogicalKeyboardK
'FUCHSIA_KEY_CODE_MAP': fuchsiaKeyCodeMap,
'MACOS_SCAN_CODE_MAP': macOsScanCodeMap,
'MACOS_NUMPAD_MAP': macOsNumpadMap,
'MACOS_FUNCTION_KEY_MAP': macOsFunctionKeyMap,
'GLFW_KEY_CODE_MAP': glfwKeyCodeMap,
'GLFW_NUMPAD_MAP': glfwNumpadMap,
'XKB_SCAN_CODE_MAP': xkbScanCodeMap,

View File

@@ -244,6 +244,9 @@ class KeyData {
// Skip key that is not actually generated by any keyboard.
return '';
}
// Remove duplicates: last one wins, so that supplemental codes
// override.
entries.removeWhere((Key entry) => entry.usbHidCode == newEntry.usbHidCode);
entries.add(newEntry);
}
return match.group(0);