近期使用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;@overrideWidget 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 qelse if (PhysicalKeyboardKey.keyQ == event.physicalKey) {str = event.isShiftPressed ? 'Q' : 'q';}// W welse if (PhysicalKeyboardKey.keyW == event.physicalKey) {str = event.isShiftPressed ? 'W' : 'w';}// E eelse if (PhysicalKeyboardKey.keyE == event.physicalKey) {str = event.isShiftPressed ? 'E' : 'e';}// R relse if (PhysicalKeyboardKey.keyR == event.physicalKey) {str = event.isShiftPressed ? 'R' : 'r';}// T telse if (PhysicalKeyboardKey.keyT == event.physicalKey) {str = event.isShiftPressed ? 'T' : 't';}// Y yelse if (PhysicalKeyboardKey.keyY == event.physicalKey) {str = event.isShiftPressed ? 'Y' : 'y';}// U uelse if (PhysicalKeyboardKey.keyU == event.physicalKey) {str = event.isShiftPressed ? 'U' : 'u';}// I ielse if (PhysicalKeyboardKey.keyI == event.physicalKey) {str = event.isShiftPressed ? 'I' : 'i';}// O oelse if (PhysicalKeyboardKey.keyO == event.physicalKey) {str = event.isShiftPressed ? 'O' : 'o';}// P pelse 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 aelse if (PhysicalKeyboardKey.keyA == event.physicalKey) {str = event.isShiftPressed ? 'A' : 'a';}// S selse if (PhysicalKeyboardKey.keyS == event.physicalKey) {str = event.isShiftPressed ? 'S' : 's';}// D delse if (PhysicalKeyboardKey.keyD == event.physicalKey) {str = event.isShiftPressed ? 'D' : 'd';}// F felse if (PhysicalKeyboardKey.keyF == event.physicalKey) {str = event.isShiftPressed ? 'F' : 'f';}// G gelse if (PhysicalKeyboardKey.keyG == event.physicalKey) {str = event.isShiftPressed ? 'G' : 'g';}// H helse if (PhysicalKeyboardKey.keyH == event.physicalKey) {str = event.isShiftPressed ? 'H' : 'h';}// J jelse if (PhysicalKeyboardKey.keyJ == event.physicalKey) {str = event.isShiftPressed ? 'J' : 'j';}// K kelse if (PhysicalKeyboardKey.keyK == event.physicalKey) {str = event.isShiftPressed ? 'K' : 'k';}// L lelse 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 zelse if (PhysicalKeyboardKey.keyZ == event.physicalKey) {str = event.isShiftPressed ? 'Z' : 'z';}// X xelse if (PhysicalKeyboardKey.keyX == event.physicalKey) {str = event.isShiftPressed ? 'X' : 'x';}// C celse if (PhysicalKeyboardKey.keyC == event.physicalKey) {str = event.isShiftPressed ? 'C' : 'c';}// V velse if (PhysicalKeyboardKey.keyV == event.physicalKey) {str = event.isShiftPressed ? 'V' : 'v';}// B belse if (PhysicalKeyboardKey.keyB == event.physicalKey) {str = event.isShiftPressed ? 'B' : 'b';}// N nelse if (PhysicalKeyboardKey.keyN == event.physicalKey) {str = event.isShiftPressed ? 'N' : 'n';}// M melse 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 = ' ';}/* 小键盘 */// 0else if (PhysicalKeyboardKey.numpad0 == event.physicalKey) {str = '0';}// 1else if (PhysicalKeyboardKey.numpad1 == event.physicalKey) {str = '1';}// 2else if (PhysicalKeyboardKey.numpad2 == event.physicalKey) {str = '2';}// 3else if (PhysicalKeyboardKey.numpad3 == event.physicalKey) {str = '3';}// 4else if (PhysicalKeyboardKey.numpad4 == event.physicalKey) {str = '4';}// 5else if (PhysicalKeyboardKey.numpad5 == event.physicalKey) {str = '5';}// 6else if (PhysicalKeyboardKey.numpad6 == event.physicalKey) {str = '6';}// 7else if (PhysicalKeyboardKey.numpad7 == event.physicalKey) {str = '7';}// 8else if (PhysicalKeyboardKey.numpad8 == event.physicalKey) {str = '8';}// 9else 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方法里处理收到的数据。

@overrideWidget build(BuildContext context) {return Scaffold(body: ScanMonitor(onScanEnd: (value) {print('扫描结果:$value');},child: Stack(children: [],),),);}

flutter:实现扫码枪获取数据相关推荐

  1. JAVA通过tcp通信劳易测BCL 308i扫码枪获取数据

    最近项目里面要集成一个扫码枪,我来负责对接.不得不说,这个扫码枪属实是我见过最烦的.首先便是硬件,送过来需要自己组装,然后网线需要自己做一根来接入他们的引脚,也没有充电线需要24V电压,得需要电压转换 ...

  2. java通过扫码枪获取数据

    1.通过按钮获取输入框焦点 2.扫描二维码或条形码,并设置回车键(大部分扫描枪已自动设置) 3.在输入框设置回车 事件 4.获取输入框的值,将数据传入数据库 <script type=" ...

  3. 前端websocket获取数据后需要存本地吗_是什么让我放弃了restful api?了解清楚后我全面拥抱GraphQL...

    GraphQL初步认识 背景 REST作为一种现代网络应用非常流行的软件架构风格,自从Roy Fielding博士在2000年他的博士论文中提出来到现在已经有了20年的历史.它的简单易用性,可扩展性, ...

  4. 08-Flutter移动电商实战-dio基础_伪造请求头获取数据

    08-Flutter移动电商实战-dio基础_伪造请求头获取数据 在很多时候,后端为了安全都会有一些请求头的限制,只有请求头对了,才能正确返回数据.这虽然限制了一些人恶意请求数据,但是对于我们聪明的程 ...

  5. 前端websocket获取数据后需要存本地吗_是什么让我放弃了Restful API?了解清楚后我全面拥抱GraphQL!...

    背景 REST作为一种现代网络应用非常流行的软件架构风格,自从Roy Fielding博士在2000年他的博士论文中提出来到现在已经有了20年的历史.它的简单易用性,可扩展性,伸缩性受到广大Web开发 ...

  6. Flutter-防京东商城项目-创建商品数据模型 、请求Api接口渲染热门商品 推荐商品 获取数据然后模型赋值-06

    一直觉得自己写的不是技术,而是情怀,一个个的教程是自己这一路走来的痕迹.靠专业技能的成功是最具可复制性的,希望我的这条路能让你们少走弯路,希望我能帮你们抹去知识的蒙尘,希望我能帮你们理清知识的脉络,希 ...

  7. Android USB 扫码枪获取扫描内容

    最近做了关于在Android设备上外接扫码的项目,在此记录一下关于Android USB扫码枪获取内容的问题 首先我这边使用是USB HID的扫码枪,即插即用,只需要在界面上有一个带有焦点的EditT ...

  8. C#通过键盘钩子获取数据

    C#通过键盘钩子获取数据 前言 在做关于使用USB读卡器.扫码枪等项目的开发时,有可能需要通过键盘钩子去获取刷卡器.扫码枪扫描到的数据,当然,如果设备是串口.或者非系统独占设备的USB,可以采用串口通 ...

  9. USB扫码枪获取数据流的实现方式

    硬件条件: OTG接口转换器(或者自带usb接口的设备(大头)) USB扫码枪(支持USB虚拟串口) 安卓设备 实现方式: 串口方式 USB方式 使用场景: 在扫码枪连接机器的时候,当扫码枪扫描到内容 ...

  10. Android通过扫码枪获取到商品的编码 完成自动搜索

    最近要做一个大型的Android设备的软件,是超市前台收银的设备,机器上有外接扫码枪等设备,这里的扫码枪指的是外接USB扫码枪 其本质是模拟输入法的一段输入流 通过获取焦点的EditText 可以直接 ...

最新文章

  1. wordpress本地mysql_如何在本地搭建和运行wordpress
  2. Python:利用collections库实现统计单个字或单个字母的频率统计并进行降序输出、统计一个列表内重复元素并以字典形式输出
  3. Springboot文件上传提示:failed to convert java.lang.String to org.springframework.util.unit.DataSize
  4. 数学--数论--欧拉降幂--P5091 欧拉定理
  5. WEBSHELL权限提升 菜菜
  6. npm 私有库开源组件_苹果的ResearchKit,npm私有模块以及更多开源新闻
  7. mlag 堆叠_三分钟了解VRRP、堆叠、M-LAG
  8. 上岸 | 青椒博士毕业后未返校任教,被判返还高校41万余元
  9. C#基础18:内置委托类型Action和Func
  10. 用java实现新浪爬虫,代码完整剖析(仅针对当前SinaSignOn有效)
  11. css中iconfont图标旋转
  12. 解决华为手机Android系统10.0不能隐藏桌面图标问题,全网唯一
  13. elementUI Tree组件实现双击事件
  14. /Users/xxxx/.zshrc:export:101: not valid in this context: /Users/xxxx/xxxx
  15. python PIL增强或降低图像对比度
  16. 失业下的深圳中年:没有人活的容易,生活仍得继续...
  17. 支付宝支付接口开发———在线扫码支付
  18. 大数据征信的应用和启示:ZestFinance的基于大数据的信用评估技术
  19. 一起来看流星雨剧情简介/剧情介绍/剧情分集介绍第十一集
  20. CSR8675学习笔记:新建一个GATT server

热门文章

  1. php根据经纬度获取地理位置
  2. Python pyserial 串口工具
  3. xposed框架定位修改怎么用_硬核!教你三种方法,实现微信自定义修改地区!
  4. 2018最新最全百度网盘限速解决方法
  5. 深度学习入门-基于python的理论与实现-深度学习
  6. Win10下 vc++6.0打开文件闪退解决
  7. 无线ac管理服务器调试方法,AC功能管理无线AP设置步骤
  8. 从傅里叶级数到拉普拉斯变换
  9. 三丰三坐标编程基本步骤_贴片机编程教程,编程步骤,编程怎么编
  10. 惠普服务器DL380 Gen10/Gen9宕机重启