Add support for detecting which modifier keys have been pressed on RawKeyboardEvents (#26265)
This adds some functions to the interface for RawKeyEventData and all subclasses that allow the recipient of an event to determine which modifier keys are currently being pressed without needing to know the specific modifier bitmasks for the platform. Also adds constants for the modifier bitmasks for each platform, for completeness (and because I needed them anyhow to implement the above). Added tests for the RawKeyEventData subclasses, and modified the raw_keyboard manual test app to show modifier keys being pressed. I also separated the different platform-specific subclasses into separate files. Fixes #26155.
This commit is contained in:
@@ -20,7 +20,7 @@ void main() {
|
||||
}
|
||||
|
||||
class RawKeyboardDemo extends StatefulWidget {
|
||||
const RawKeyboardDemo({ Key key }) : super(key: key);
|
||||
const RawKeyboardDemo({Key key}) : super(key: key);
|
||||
|
||||
@override
|
||||
_HardwareKeyDemoState createState() => _HardwareKeyDemoState();
|
||||
@@ -42,6 +42,14 @@ class _HardwareKeyDemoState extends State<RawKeyboardDemo> {
|
||||
});
|
||||
}
|
||||
|
||||
String _asHex(int value) => value != null ? '0x${value.toRadixString(16)}' : 'null';
|
||||
|
||||
String _getEnumName(dynamic enumItem) {
|
||||
final String name = '$enumItem';
|
||||
final int index = name.indexOf('.');
|
||||
return index == -1 ? name : name.substring(index + 1);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final TextTheme textTheme = Theme.of(context).textTheme;
|
||||
@@ -60,28 +68,42 @@ class _HardwareKeyDemoState extends State<RawKeyboardDemo> {
|
||||
);
|
||||
}
|
||||
|
||||
if (_event == null)
|
||||
if (_event == null) {
|
||||
return Text('Press a key', style: textTheme.display1);
|
||||
|
||||
int codePoint;
|
||||
int keyCode;
|
||||
int hidUsage;
|
||||
final RawKeyEventData data = _event.data;
|
||||
if (data is RawKeyEventDataAndroid) {
|
||||
codePoint = data.codePoint;
|
||||
keyCode = data.keyCode;
|
||||
} else if (data is RawKeyEventDataFuchsia) {
|
||||
codePoint = data.codePoint;
|
||||
hidUsage = data.hidUsage;
|
||||
}
|
||||
return Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
Text('${_event.runtimeType}', style: textTheme.body2),
|
||||
Text('codePoint: $codePoint', style: textTheme.display4),
|
||||
Text('keyCode: $keyCode', style: textTheme.display4),
|
||||
Text('hidUsage: $hidUsage', style: textTheme.display4),
|
||||
],
|
||||
|
||||
final RawKeyEventData data = _event.data;
|
||||
final String modifierList = data.modifiersPressed.keys.map<String>(_getEnumName).join(', ').replaceAll('Modifier', '');
|
||||
final List<Widget> dataText = <Widget>[
|
||||
Text('${_event.runtimeType}'),
|
||||
Text('modifiers set: $modifierList'),
|
||||
];
|
||||
if (data is RawKeyEventDataAndroid) {
|
||||
dataText.add(Text('codePoint: ${data.codePoint} (${_asHex(data.codePoint)})'));
|
||||
dataText.add(Text('keyCode: ${data.keyCode} (${_asHex(data.keyCode)})'));
|
||||
dataText.add(Text('scanCode: ${data.scanCode} (${_asHex(data.scanCode)})'));
|
||||
dataText.add(Text('metaState: ${data.metaState} (${_asHex(data.metaState)})'));
|
||||
dataText.add(Text('flags: ${data.flags} (${_asHex(data.flags)})'));
|
||||
} else if (data is RawKeyEventDataFuchsia) {
|
||||
dataText.add(Text('codePoint: ${data.codePoint} (${_asHex(data.codePoint)})'));
|
||||
dataText.add(Text('hidUsage: ${data.hidUsage} (${_asHex(data.hidUsage)})'));
|
||||
dataText.add(Text('modifiers: ${data.modifiers} (${_asHex(data.modifiers)})'));
|
||||
}
|
||||
for (ModifierKey modifier in data.modifiersPressed.keys) {
|
||||
for (KeyboardSide side in KeyboardSide.values) {
|
||||
if (data.isModifierPressed(modifier, side: side)) {
|
||||
dataText.add(
|
||||
Text('${_getEnumName(side)} ${_getEnumName(modifier).replaceAll('Modifier', '')} pressed'),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
return DefaultTextStyle(
|
||||
style: textTheme.headline,
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: dataText,
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user