mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00
Merge pull request #1380 from abarth/raw_keyboard_listener
Add RawKeyboardListener
This commit is contained in:
commit
dc7451d642
@ -1,81 +1,79 @@
|
||||
// Copyright (c) 2015, the Flutter project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
// Copyright 2016 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
import 'package:sky_services/raw_keyboard/raw_keyboard.mojom.dart' as mojo;
|
||||
import 'package:sky_services/sky/input_event.mojom.dart' as mojo;
|
||||
import 'package:sky_services/sky/input_event.mojom.dart' as mojom;
|
||||
|
||||
GlobalKey _key = new GlobalKey();
|
||||
|
||||
void main() {
|
||||
runApp(
|
||||
new MaterialApp(
|
||||
title: "Hardware Key Demo",
|
||||
routes: {
|
||||
'/': (RouteArguments args) => const HardwareKeyDemo()
|
||||
routes: <String, RouteBuilder>{
|
||||
'/': (RouteArguments args) {
|
||||
return new Scaffold(
|
||||
toolBar: new ToolBar(
|
||||
center: new Text("Hardware Key Demo")
|
||||
),
|
||||
body: new Material(
|
||||
child: new RawKeyboardDemo(
|
||||
key: _key
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
class HardwareKeyDemo extends StatefulComponent {
|
||||
const HardwareKeyDemo();
|
||||
HardwareKeyDemoState createState() => new HardwareKeyDemoState();
|
||||
class RawKeyboardDemo extends StatefulComponent {
|
||||
RawKeyboardDemo({ GlobalKey key }) : super(key: key);
|
||||
|
||||
_HardwareKeyDemoState createState() => new _HardwareKeyDemoState();
|
||||
}
|
||||
|
||||
class HardwareKeyDemoState extends State<HardwareKeyDemo> implements mojo.RawKeyboardListener {
|
||||
class _HardwareKeyDemoState extends State<RawKeyboardDemo> {
|
||||
mojom.InputEvent _event = null;
|
||||
|
||||
mojo.InputEvent _event = null;
|
||||
|
||||
void initState() {
|
||||
mojo.RawKeyboardServiceProxy rawKeyboardService = new mojo.RawKeyboardServiceProxy.unbound();
|
||||
try {
|
||||
shell.connectToService(null, rawKeyboardService);
|
||||
mojo.RawKeyboardListenerStub listener = new mojo.RawKeyboardListenerStub.unbound()
|
||||
..impl = this;
|
||||
rawKeyboardService.ptr.addListener(listener);
|
||||
} finally {
|
||||
rawKeyboardService.close();
|
||||
}
|
||||
super.initState();
|
||||
}
|
||||
|
||||
void onKey(mojo.InputEvent event) {
|
||||
void _handleKey(mojom.InputEvent event) {
|
||||
setState(() {
|
||||
_event = event;
|
||||
});
|
||||
}
|
||||
|
||||
Widget _buildBody() {
|
||||
if (_event == null) {
|
||||
return new Center(
|
||||
child: new Text("Press a key", style: Typography.black.display1)
|
||||
Widget build(BuildContext context) {
|
||||
bool focused = Focus.at(context);
|
||||
Widget child;
|
||||
if (!focused) {
|
||||
child = new GestureDetector(
|
||||
onTap: () {
|
||||
Focus.moveTo(config.key);
|
||||
},
|
||||
child: new Center(
|
||||
child: new Text('Tap to focus', style: Typography.black.display1)
|
||||
)
|
||||
);
|
||||
} else if (_event == null) {
|
||||
child = new Center(
|
||||
child: new Text('Press a key', style: Typography.black.display1)
|
||||
);
|
||||
} else {
|
||||
child = new Column(
|
||||
children: <Widget>[
|
||||
new Text('${_event.type}', style: Typography.black.body2),
|
||||
new Text('${_event.keyData.keyCode}', style: Typography.black.display4)
|
||||
],
|
||||
justifyContent: FlexJustifyContent.center
|
||||
);
|
||||
}
|
||||
return new Column(
|
||||
children: <Widget>[
|
||||
new Text(
|
||||
'${_event.type}',
|
||||
style: Typography.black.body2
|
||||
),
|
||||
new Text(
|
||||
'${_event.keyData.keyCode}',
|
||||
style: Typography.black.display4
|
||||
)
|
||||
],
|
||||
justifyContent: FlexJustifyContent.center
|
||||
);
|
||||
}
|
||||
|
||||
Widget build(BuildContext context) {
|
||||
return new Scaffold(
|
||||
toolBar: new ToolBar(
|
||||
center: new Text("Hardware Key Demo")
|
||||
),
|
||||
body: new Material(
|
||||
child: _buildBody()
|
||||
)
|
||||
return new RawKeyboardListener(
|
||||
focused: focused,
|
||||
onKey: _handleKey,
|
||||
child: child
|
||||
);
|
||||
}
|
||||
}
|
||||
|
76
packages/flutter/lib/src/widgets/raw_keyboard_listener.dart
Normal file
76
packages/flutter/lib/src/widgets/raw_keyboard_listener.dart
Normal file
@ -0,0 +1,76 @@
|
||||
// Copyright 2016 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:sky_services/raw_keyboard/raw_keyboard.mojom.dart' as mojom;
|
||||
import 'package:sky_services/sky/input_event.mojom.dart' as mojom;
|
||||
|
||||
import 'basic.dart';
|
||||
import 'framework.dart';
|
||||
|
||||
class RawKeyboardListener extends StatefulComponent {
|
||||
RawKeyboardListener({
|
||||
Key key,
|
||||
this.focused: false,
|
||||
this.onKey,
|
||||
this.child
|
||||
}) : super(key: key) {
|
||||
assert(child != null);
|
||||
}
|
||||
|
||||
final bool focused;
|
||||
final ValueChanged<mojom.InputEvent> onKey;
|
||||
final Widget child;
|
||||
|
||||
_RawKeyboardListenerState createState() => new _RawKeyboardListenerState();
|
||||
}
|
||||
|
||||
class _RawKeyboardListenerState extends State<RawKeyboardListener> implements mojom.RawKeyboardListener {
|
||||
void initState() {
|
||||
super.initState();
|
||||
_attachOrDetachKeyboard();
|
||||
}
|
||||
|
||||
mojom.RawKeyboardListenerStub _stub;
|
||||
|
||||
void didUpdateConfig(RawKeyboardListener oldConfig) {
|
||||
_attachOrDetachKeyboard();
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
_detachKeyboard();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _attachOrDetachKeyboard() {
|
||||
if (config.focused && _stub == null)
|
||||
_attachKeyboard();
|
||||
else if (!config.focused && _stub != null)
|
||||
_detachKeyboard();
|
||||
}
|
||||
|
||||
void _attachKeyboard() {
|
||||
assert(_stub == null);
|
||||
_stub = new mojom.RawKeyboardListenerStub.unbound()..impl = this;
|
||||
mojom.RawKeyboardServiceProxy keyboard = new mojom.RawKeyboardServiceProxy.unbound();
|
||||
shell.connectToService(null, keyboard);
|
||||
keyboard.ptr.addListener(_stub);
|
||||
keyboard.close();
|
||||
}
|
||||
|
||||
void _detachKeyboard() {
|
||||
assert(_stub != null);
|
||||
_stub.close();
|
||||
_stub = null;
|
||||
}
|
||||
|
||||
void onKey(mojom.InputEvent event) {
|
||||
if (config.onKey != null)
|
||||
config.onKey(event);
|
||||
}
|
||||
|
||||
Widget build(BuildContext context) {
|
||||
return config.child;
|
||||
}
|
||||
}
|
@ -31,6 +31,7 @@ export 'src/widgets/pageable_list.dart';
|
||||
export 'src/widgets/pages.dart';
|
||||
export 'src/widgets/performance_overlay.dart';
|
||||
export 'src/widgets/placeholder.dart';
|
||||
export 'src/widgets/raw_keyboard_listener.dart';
|
||||
export 'src/widgets/routes.dart';
|
||||
export 'src/widgets/scrollable.dart';
|
||||
export 'src/widgets/scrollable_grid.dart';
|
||||
|
Loading…
Reference in New Issue
Block a user