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



如何监视虚拟键盘的按键事件,包括键码。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) {}
}

我有一个类似的问题,我用这种方式解决了它。 尝试通过TextEditingController获取文本中的光标位置索引,并使用该索引获取最后键入的值。 想象一下,下面的示例是在TextFieldonChanged函数中,它为您提供插入的文本。

像这样的东西

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

最新更新