近期使用Flutter开发了一个Windows项目,不使用TextField等控件,获取扫码枪返回的数据。
翻文档,发现官方提供了RawKeyboard 添加全局监听方法,既然找到方法了,开始愉快的编码吧。
编码完成,测试发现,在中文输入法启动的时候,RawKeyboard提供的监听不能正常回调,这是为啥呢?翻源码吧,终于,在RawKeyboard的handleRawKeyEvent方法里发现了罪魁祸首
这条路行不通了,走其他的路吧。
继续翻文档,发现Flutter提供了Focus控件,可以获取焦点,中文输入状态仍然可以正常执行回调方法。
改造一下,就可以用啦。
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../util/logger.dart';
final _stringBuffer = StringBuffer();
class ScanMonitor extends StatelessWidget {
///
final Widget child;
///
final ValueChanged<String>? onScanEnd;
ScanMonitor({Key? key, required this.child, this.onScanEnd})
: super(key: key);
Timer? _timer;
@override
Widget build(BuildContext context) {
return Focus(
autofocus: true,
canRequestFocus: true,
child: child,
onKey: (node, event) {
if (event is RawKeyDownEvent) {
_analysisKeyEvent(event);
}
return KeyEventResult.ignored;
},
);
}
///
void _analysisKeyEvent(RawKeyDownEvent event) {
String? str;
/* 键盘第一行 */
// ~ `
if (PhysicalKeyboardKey.backquote == event.physicalKey) {
str = event.isShiftPressed ? '~' : '`';
}
// 1 !
if (PhysicalKeyboardKey.digit1 == event.physicalKey) {
str = event.isShiftPressed ? '!' : '1';
}
// 2 @
else if (PhysicalKeyboardKey.digit2 == event.physicalKey) {
str = event.isShiftPressed ? '@' : '2';
}
// 3 #
else if (PhysicalKeyboardKey.digit3 == event.physicalKey) {
str = event.isShiftPressed ? '#' : '3';
}
// 4 $
else if (PhysicalKeyboardKey.digit4 == event.physicalKey) {
str = event.isShiftPressed ? r'$' : '4';
}
// 5 %
else if (PhysicalKeyboardKey.digit5 == event.physicalKey) {
str = event.isShiftPressed ? '%' : '5';
}
// 6 ^
else if (PhysicalKeyboardKey.digit6 == event.physicalKey) {
str = event.isShiftPressed ? '^' : '6';
}
// 7 &
else if (PhysicalKeyboardKey.digit7 == event.physicalKey) {
str = event.isShiftPressed ? '&' : '7';
}
// 8 *
else if (PhysicalKeyboardKey.digit8 == event.physicalKey) {
str = event.isShiftPressed ? '*' : '8';
}
// 9 (
else if (PhysicalKeyboardKey.digit9 == event.physicalKey) {
str = event.isShiftPressed ? '(' : '9';
}
// 0 )
else if (PhysicalKeyboardKey.digit0 == event.physicalKey) {
str = event.isShiftPressed ? ')' : '0';
}
// - _
else if (PhysicalKeyboardKey.minus == event.physicalKey) {
str = event.isShiftPressed ? '_' : '-';
}
// = +
else if (PhysicalKeyboardKey.equal == event.physicalKey) {
str = event.isShiftPressed ? '+' : '=';
}
/* 键盘第二行 */
// Q q
else if (PhysicalKeyboardKey.keyQ == event.physicalKey) {
str = event.isShiftPressed ? 'Q' : 'q';
}
// W w
else if (PhysicalKeyboardKey.keyW == event.physicalKey) {
str = event.isShiftPressed ? 'W' : 'w';
}
// E e
else if (PhysicalKeyboardKey.keyE == event.physicalKey) {
str = event.isShiftPressed ? 'E' : 'e';
}
// R r
else if (PhysicalKeyboardKey.keyR == event.physicalKey) {
str = event.isShiftPressed ? 'R' : 'r';
}
// T t
else if (PhysicalKeyboardKey.keyT == event.physicalKey) {
str = event.isShiftPressed ? 'T' : 't';
}
// Y y
else if (PhysicalKeyboardKey.keyY == event.physicalKey) {
str = event.isShiftPressed ? 'Y' : 'y';
}
// U u
else if (PhysicalKeyboardKey.keyU == event.physicalKey) {
str = event.isShiftPressed ? 'U' : 'u';
}
// I i
else if (PhysicalKeyboardKey.keyI == event.physicalKey) {
str = event.isShiftPressed ? 'I' : 'i';
}
// O o
else if (PhysicalKeyboardKey.keyO == event.physicalKey) {
str = event.isShiftPressed ? 'O' : 'o';
}
// P p
else if (PhysicalKeyboardKey.keyP == event.physicalKey) {
str = event.isShiftPressed ? 'P' : 'p';
}
// { [
else if (PhysicalKeyboardKey.bracketLeft == event.physicalKey) {
str = event.isShiftPressed ? '{' : '[';
}
// } ]
else if (PhysicalKeyboardKey.bracketRight == event.physicalKey) {
str = event.isShiftPressed ? '}' : ']';
}
// | \
else if (PhysicalKeyboardKey.backslash == event.physicalKey) {
str = event.isShiftPressed ? '|' : r'\';
}
/* 键盘第三行 */
// A a
else if (PhysicalKeyboardKey.keyA == event.physicalKey) {
str = event.isShiftPressed ? 'A' : 'a';
}
// S s
else if (PhysicalKeyboardKey.keyS == event.physicalKey) {
str = event.isShiftPressed ? 'S' : 's';
}
// D d
else if (PhysicalKeyboardKey.keyD == event.physicalKey) {
str = event.isShiftPressed ? 'D' : 'd';
}
// F f
else if (PhysicalKeyboardKey.keyF == event.physicalKey) {
str = event.isShiftPressed ? 'F' : 'f';
}
// G g
else if (PhysicalKeyboardKey.keyG == event.physicalKey) {
str = event.isShiftPressed ? 'G' : 'g';
}
// H h
else if (PhysicalKeyboardKey.keyH == event.physicalKey) {
str = event.isShiftPressed ? 'H' : 'h';
}
// J j
else if (PhysicalKeyboardKey.keyJ == event.physicalKey) {
str = event.isShiftPressed ? 'J' : 'j';
}
// K k
else if (PhysicalKeyboardKey.keyK == event.physicalKey) {
str = event.isShiftPressed ? 'K' : 'k';
}
// L l
else if (PhysicalKeyboardKey.keyL == event.physicalKey) {
str = event.isShiftPressed ? 'L' : 'l';
}
// : ;
else if (PhysicalKeyboardKey.semicolon == event.physicalKey) {
str = event.isShiftPressed ? ':' : ';';
}
// " '
else if (PhysicalKeyboardKey.quote == event.physicalKey) {
str = event.isShiftPressed ? '"' : '\'';
}
/* 键盘第四行 */
// Z z
else if (PhysicalKeyboardKey.keyZ == event.physicalKey) {
str = event.isShiftPressed ? 'Z' : 'z';
}
// X x
else if (PhysicalKeyboardKey.keyX == event.physicalKey) {
str = event.isShiftPressed ? 'X' : 'x';
}
// C c
else if (PhysicalKeyboardKey.keyC == event.physicalKey) {
str = event.isShiftPressed ? 'C' : 'c';
}
// V v
else if (PhysicalKeyboardKey.keyV == event.physicalKey) {
str = event.isShiftPressed ? 'V' : 'v';
}
// B b
else if (PhysicalKeyboardKey.keyB == event.physicalKey) {
str = event.isShiftPressed ? 'B' : 'b';
}
// N n
else if (PhysicalKeyboardKey.keyN == event.physicalKey) {
str = event.isShiftPressed ? 'N' : 'n';
}
// M m
else if (PhysicalKeyboardKey.keyM == event.physicalKey) {
str = event.isShiftPressed ? 'M' : 'm';
}
// < ,
else if (PhysicalKeyboardKey.comma == event.physicalKey) {
str = event.isShiftPressed ? '<' : ',';
}
// > .
else if (PhysicalKeyboardKey.period == event.physicalKey) {
str = event.isShiftPressed ? '>' : '.';
}
// ? /
else if (PhysicalKeyboardKey.slash == event.physicalKey) {
str = event.isShiftPressed ? '?' : '/';
}
/* 键盘第五行 */
// 空格
else if (PhysicalKeyboardKey.space == event.physicalKey) {
str = ' ';
}
/* 小键盘 */
// 0
else if (PhysicalKeyboardKey.numpad0 == event.physicalKey) {
str = '0';
}
// 1
else if (PhysicalKeyboardKey.numpad1 == event.physicalKey) {
str = '1';
}
// 2
else if (PhysicalKeyboardKey.numpad2 == event.physicalKey) {
str = '2';
}
// 3
else if (PhysicalKeyboardKey.numpad3 == event.physicalKey) {
str = '3';
}
// 4
else if (PhysicalKeyboardKey.numpad4 == event.physicalKey) {
str = '4';
}
// 5
else if (PhysicalKeyboardKey.numpad5 == event.physicalKey) {
str = '5';
}
// 6
else if (PhysicalKeyboardKey.numpad6 == event.physicalKey) {
str = '6';
}
// 7
else if (PhysicalKeyboardKey.numpad7 == event.physicalKey) {
str = '7';
}
// 8
else if (PhysicalKeyboardKey.numpad8 == event.physicalKey) {
str = '8';
}
// 9
else if (PhysicalKeyboardKey.numpad9 == event.physicalKey) {
str = '9';
}
// +
else if (PhysicalKeyboardKey.numpadAdd == event.physicalKey) {
str = '+';
}
// -
else if (PhysicalKeyboardKey.numpadSubtract == event.physicalKey) {
str = '-';
}
// *
else if (PhysicalKeyboardKey.numpadMultiply == event.physicalKey) {
str = '*';
}
// /
else if (PhysicalKeyboardKey.numpadDivide == event.physicalKey) {
str = '/';
}
// .
else if (PhysicalKeyboardKey.numpadDecimal == event.physicalKey) {
str = '.';
}
// 处理结果
if (str != null && str.isNotEmpty) {
_stringBuffer.write(str);
}
if (LogicalKeyboardKey.enter == event.logicalKey) {
_timer = _buildTimber();
} else {
_timer = _buildTimber(milliseconds: 500);
}
}
/// 生成计时器
Timer _buildTimber({int milliseconds = 500}) {
// 取消定时器
_timer?.cancel();
_timer = null;
// 创建新的定时器对象
return Timer(Duration(milliseconds: milliseconds), () {
// 解析数据
String result = _stringBuffer.toString();
_stringBuffer.clear();
if (result.isNotEmpty) {
logger.d(result);
onScanEnd?.call(result);
}
});
}
}
将ScanMonitor作为父节点,嵌套自己的页面作为子类。在onScanEnd方法里处理收到的数据。
@override
Widget build(BuildContext context) {
return Scaffold(
body: ScanMonitor(
onScanEnd: (value) {
print('扫描结果:$value');
},
child: Stack(
children: [],
),
),
);
}