import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class RawKeyTestPage extends StatefulWidget {
@override
State<StatefulWidget> createState() => _RawKeyTestState();
}
class _RawKeyTestState extends State<RawKeyTestPage> {
@override
void initState() {
super.initState();
}
String _value = '';
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('RawKeyboardListener Page')),
body: RawKeyboardListener(
autofocus: true,
onKey: (event) {
if (event.runtimeType == RawKeyDownEvent) {
if(event.data is RawKeyEventDataAndroid){
RawKeyEventDataAndroid datga = event.data as RawKeyEventDataAndroid;
///获取按键键值 keycode
_value = datga.keyCode.toString();
setState(() {
});
// print('flutter down'+datga.keyCode.toString());
}
}
},
focusNode: FocusNode(),
child: Text(_value),
)
,
));
}
@override
void dispose() {
super.dispose();
}
}
PS: 监听蓝牙游戏手柄的按键事件,可以RawKeyboardListener,可以监听到X,Y,A,B等事件,但像左右摇杆的无法监听,只能在容器的Activity中的 onGenericMotionEvent中处理,不知道在Flutter层怎么监听?