微信交流群

老孟公众号

# RawKeyboardListener

此控件用于键盘的事件,包括物理按键,通常用于游戏类应用。

此控件目前在部分手机上无法监听到,无法确定是国内手机厂商修改所致,还是flutter系统的bug。也可以关注 github issue,实时了解其进展

用法:

TextEditingController _controller = new TextEditingController();
FocusNode _textNode = new FocusNode();

handleKey(RawKeyEvent key) {
  print("Event runtimeType is ${key.runtimeType}");
  if(key.runtimeType.toString() == 'RawKeyDownEvent'){
    RawKeyEventDataAndroid data = key.data as RawKeyEventDataAndroid;
    String _keyCode;
    _keyCode = data.keyCode.toString(); //keycode of key event (66 is return)

    print("why does this run twice $_keyCode");
  }
}


Widget build(BuildContext context) {
  TextField _textField = new TextField(
    controller: _controller,
  );
  FocusScope.of(context).requestFocus(_textNode);
  return Scaffold(
      appBar: AppBar(
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.close),
            onPressed: () {
              _key.currentState.removeItem(4);
            },
          )
        ],
      ),
      body: Scaffold(
        body: RawKeyboardListener(
            focusNode: _textNode,
            onKey: handleKey,
            child: _textField
        ),
      ));
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39