简体   繁体   English

如何在Flutter中检测虚拟键盘事件

[英]How Detect virtual keyboard events in Flutter

How to monitor the key events of the virtual keyboard, including the keycode. 如何监控虚拟键盘的关键事件,包括键码。 RawKeyboardListener has no effect on virtual keyboard keys. RawKeyboardListener对虚拟键盘键没有影响。

    void _onkeyclick(RawKeyEvent event) {
    if (event is RawKeyDownEvent) {
      if (event.data is RawKeyEventDataAndroid) {
        RawKeyDownEvent rawKeyDownEvent = event;
        RawKeyEventDataAndroid rawKeyEventDataAndroid = rawKeyDownEvent.data;
        print(rawKeyEventDataAndroid.keyCode);
        switch (rawKeyEventDataAndroid.keyCode) {
          case 66:
        }
      }
    }
  }
Widget getItem(Pbtem pb) {
    if (pb.type == 1) {
      TextEditingController _c = new TextEditingController();
      return new RawKeyboardListener(
          focusNode: _focusNode,
          onKey: _onkeyclick,
          child: new EditableText(
            controller: _c,
            focusNode: _focusNode,
            style: TextStyle(
              color: Colors.black,
              fontSize: 18.0,
            ),
            keyboardType: TextInputType.multiline,
            cursorColor: Colors.blue,
          ));
    } else if (pb.type == 2) {}
  }

I had a similar problem and I solved it in this way. 我有类似的问题,我以这种方式解决了它。 Try to get your cursor position index in the text via TextEditingController and with that index get last typed value. 尝试通过TextEditingController在文本中获取光标位置索引,并使用该索引获取最后输入的值。 Imagine below example is in TextField 's onChanged function which gives you inserted text. 想象一下下面的例子是在TextFieldonChanged函数中,它为你提供插入的文本。

Something like this 像这样的东西

String key = value.substring(_textController.selection.end-1, _textController.selection.end);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM