Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Detect virtual keyboard events in Flutter

Tags:

flutter

How to monitor the key events of the virtual keyboard, including the keycode. RawKeyboardListener has no effect on virtual keyboard keys.

    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) {}
  }
like image 440
cofl Avatar asked Aug 08 '18 09:08

cofl


1 Answers

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. Imagine below example is in TextField's onChanged function which gives you inserted text.

Something like this

String key = value.substring(_textController.selection.end-1, _textController.selection.end);
like image 110
Babken Avatar answered Nov 02 '22 05:11

Babken