Commit 5801119d authored by Adam Barth's avatar Adam Barth Committed by GitHub

Update raw keyboard example (#6871)

We now center the body correctly and handle Fuchsia key events. Also, add
support for modifiers.
parent 9cfde147
...@@ -14,7 +14,7 @@ void main() { ...@@ -14,7 +14,7 @@ void main() {
appBar: new AppBar( appBar: new AppBar(
title: new Text("Hardware Key Demo") title: new Text("Hardware Key Demo")
), ),
body: new Material( body: new Center(
child: new RawKeyboardDemo( child: new RawKeyboardDemo(
key: _key key: _key
) )
...@@ -42,28 +42,28 @@ class _HardwareKeyDemoState extends State<RawKeyboardDemo> { ...@@ -42,28 +42,28 @@ class _HardwareKeyDemoState extends State<RawKeyboardDemo> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final TextTheme textTheme = Theme.of(context).textTheme; final TextTheme textTheme = Theme.of(context).textTheme;
bool focused = Focus.at(context); final bool focused = Focus.at(context);
Widget child; Widget child;
if (!focused) { if (!focused) {
child = new GestureDetector( child = new GestureDetector(
onTap: () { onTap: () {
Focus.moveTo(config.key); Focus.moveTo(config.key);
}, },
child: new Center( child: new Text('Tap to focus', style: textTheme.display1),
child: new Text('Tap to focus', style: textTheme.display1),
),
); );
} else if (_event == null) { } else if (_event == null) {
child = new Center( child = new Text('Press a key', style: textTheme.display1);
child: new Text('Press a key', style: textTheme.display1),
);
} else { } else {
int codePoint; int codePoint;
int keyCode; int keyCode;
int hidUsage;
final RawKeyEventData data = _event.data; final RawKeyEventData data = _event.data;
if (data is RawKeyEventDataAndroid) { if (data is RawKeyEventDataAndroid) {
codePoint = data.codePoint; codePoint = data.codePoint;
keyCode = data.keyCode; keyCode = data.keyCode;
} else if (data is RawKeyEventDataFuchsia) {
codePoint = data.codePoint;
hidUsage = data.hidUsage;
} }
child = new Column( child = new Column(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
...@@ -71,6 +71,7 @@ class _HardwareKeyDemoState extends State<RawKeyboardDemo> { ...@@ -71,6 +71,7 @@ class _HardwareKeyDemoState extends State<RawKeyboardDemo> {
new Text('${_event.runtimeType}', style: textTheme.body2), new Text('${_event.runtimeType}', style: textTheme.body2),
new Text('codePoint: $codePoint', style: textTheme.display4), new Text('codePoint: $codePoint', style: textTheme.display4),
new Text('keyCode: $keyCode', style: textTheme.display4), new Text('keyCode: $keyCode', style: textTheme.display4),
new Text('hidUsage: $hidUsage', style: textTheme.display4),
], ],
); );
} }
...@@ -81,3 +82,4 @@ class _HardwareKeyDemoState extends State<RawKeyboardDemo> { ...@@ -81,3 +82,4 @@ class _HardwareKeyDemoState extends State<RawKeyboardDemo> {
); );
} }
} }
...@@ -70,6 +70,7 @@ class RawKeyEventDataFuchsia extends RawKeyEventData { ...@@ -70,6 +70,7 @@ class RawKeyEventDataFuchsia extends RawKeyEventData {
const RawKeyEventDataFuchsia({ const RawKeyEventDataFuchsia({
this.hidUsage: 0, this.hidUsage: 0,
this.codePoint: 0, this.codePoint: 0,
this.modifiers: 0,
}); });
/// The USB HID usage. /// The USB HID usage.
...@@ -81,6 +82,12 @@ class RawKeyEventDataFuchsia extends RawKeyEventData { ...@@ -81,6 +82,12 @@ class RawKeyEventDataFuchsia extends RawKeyEventData {
/// ///
/// If there is no unicode code point, this value is zero. /// If there is no unicode code point, this value is zero.
final int codePoint; final int codePoint;
/// The modifiers that we present when the key event occured.
///
/// See <https://fuchsia.googlesource.com/mozart/+/master/services/input/input_event_constants.fidl>
/// for the numerical values of the modifiers.
final int modifiers;
} }
/// Base class for raw key events. /// Base class for raw key events.
...@@ -138,6 +145,7 @@ RawKeyEvent _toRawKeyEvent(Map<String, dynamic> message) { ...@@ -138,6 +145,7 @@ RawKeyEvent _toRawKeyEvent(Map<String, dynamic> message) {
data = new RawKeyEventDataFuchsia( data = new RawKeyEventDataFuchsia(
hidUsage: message['hidUsage'] ?? 0, hidUsage: message['hidUsage'] ?? 0,
codePoint: message['codePoint'] ?? 0, codePoint: message['codePoint'] ?? 0,
modifiers: message['modifiers'] ?? 0,
); );
break; break;
default: default:
......
...@@ -38,6 +38,7 @@ void main() { ...@@ -38,6 +38,7 @@ void main() {
'keymap': 'fuchsia', 'keymap': 'fuchsia',
'hidUsage': 0x04, 'hidUsage': 0x04,
'codePoint': 0x64, 'codePoint': 0x64,
'modifiers': 0x08,
}); });
await tester.idle(); await tester.idle();
...@@ -48,5 +49,6 @@ void main() { ...@@ -48,5 +49,6 @@ void main() {
RawKeyEventDataFuchsia typedData = events[0].data; RawKeyEventDataFuchsia typedData = events[0].data;
expect(typedData.hidUsage, 0x04); expect(typedData.hidUsage, 0x04);
expect(typedData.codePoint, 0x64); expect(typedData.codePoint, 0x64);
expect(typedData.modifiers, 0x08);
}); });
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment