我如何在React Native中收听键盘输入 [英] How do I listen to keyboard input in React Native

查看:403
本文介绍了我如何在React Native中收听键盘输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个霍尼韦尔扫描仪,可以在扫描条形码时输出文本. 我可以将其用作文本输入的键盘",这非常方便,因为我无需进行任何接口. 但这有一个缺点,那就是必须集中输入并显示移动设备的虚拟键盘,这对于我正在从事的项目是不可接受的.

I have a Honeywell Scanner that outputs text when scanning bar codes. I can use it as a "keyboard" for text inputs, which comes in very handy because I don't have to interface anything. But it has the downside of having to focus an input and thus displaying the virtual keyboard of the mobile device, which is unacceptable for the project I'm working on.

有什么方法可以在不集中输入的情况下扫描值?我相信,侦听keyPress事件或类似事件将是解决问题的方法,将输入的值存储在textInput之外的其他变量中.

Is there any way to get the value scanned without having to focus an input? I believe that listening for the keyPress events or the likes of it would be the way to go, storing the value inputted in a variable elsewhere than the textInput.

如果有更好的方法可以做到这一点,我全都听着!

If there's a better way of achieving this, I'm all ears!

推荐答案

大家好,找到了一种做到这一点而又不会太疯狂的方法. 这不是最优雅的解决方案,但是它完全可以满足我的需要,并且不会在代码中添加过多的内容.

Okay everyone, found a way to do this without going way too crazy. It's not the most elegant solution, but it does exactly what I need and doesn't add too much to the code.

import { Keyboard, TextInput } from 'react-native';

...

<TextInput
  autoFocus
  onFocus={() => Keyboard.dismiss()} />

这里的游戏名称是autoFocusonFocus={() => Keyboard.dismiss()} />.

现在,我将使用 https://www.npmjs.com/package/@MattAft建议使用react-native-keyevent 来监听按键(不幸的是,此程序包不会提供实际的扫描仪输入.但是,它将触发keyDown事件),以便在扫描仪读取某些内容时使TextInput聚焦

Now I'll be using https://www.npmjs.com/package/react-native-keyevent as suggested by @MattAft to listen for key presses (unfortunately this package wont give me the actual scanner input. It will, however, trigger the keyDown event) to focus the TextInput when the scanner reads something.

更新 几天前,当我们有一段时间重构此组件时,一位同事为我提供了一些帮助,我们发现在react-native-keyevent包上有一个名为onKeyMultipleListener的监听器,它不仅监听多个同时的keyPresses而且还捕获了整个输入,这正是我们在这里需要的.

UPDATE A coworker helped me a bit with this a few days ago when we had some time to refactor this component, and we found out there is a listener called onKeyMultipleListener on the react-native-keyevent package which not only listens for multiple simultaneous keyPresses but also captures the entire input, which is exactly what we needed here.

这篇关于我如何在React Native中收听键盘输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆