如何在React Native中获取用户按键?
如何在React Native中按下键(除了让用户单击文本框之外)?添加到< view>
组件中时,OnkeyPress和Onkeydown会产生类型错误。
我还看到了此答案,但是它需要第三方套餐。我想象本机应用程序支持键盘输入用于可访问性。
以下代码不起作用,
import { View } from 'react-native'
<View onKeyDown={handleKeyDown}>my app</View>
这会产生以下错误:
Property 'onKeyDown' does not exist on type 'IntrinsicAttributes & InterfaceViewProps & RefAttributes<unknown>'
window.addeventListener('keydown')
也对Android或iOS不起作用,鉴于没有浏览器,这很有意义。
编辑 我正在尝试在使用应用程序时随时收听用户的任何键盘输入。例如:当使用该应用程序触发盲友好功能时,用户随机按“ F”,而无需单击或看到任何内容。
How do I get the key pressed in React Native (other than having the user click a text box)? onKeyPress and onKeyDown gives a type error when being added to the <View>
component.
I have also seen this answer, but it requires a 3rd party package. I imagine React Native apps support keyboard input for accessibility purposes.
The following code does not work
import { View } from 'react-native'
<View onKeyDown={handleKeyDown}>my app</View>
This gives the following error:
Property 'onKeyDown' does not exist on type 'IntrinsicAttributes & InterfaceViewProps & RefAttributes<unknown>'
window.addEventListener('keydown')
also does not work for android or IOS which makes sense given there is no browser.
EDIT
I am trying to listen for ANY keyboard input from the user at any time when using the app. Ex: User presses 'f' randomly when using the app to trigger a blind-friendly feature, without clicking or seeing anything.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您正在寻找物理键盘处理,那么到目前为止,没有物理键盘键盘处理功能。
您可以尝试第三方库,例如我的: https:https:///www.npmjs .com/package/react-native-external-keyboard
If you are looking for physical keyboard handling, there are no physical Keyboard key press handling functionality so far.
You can try 3rd party libraries, for example mine: https://www.npmjs.com/package/react-native-external-keyboard
好吧,这不是最优雅的方式,但它完全按照您的意愿进行:
键盘钥匙事件侦听器现在可以在不显示键盘的情况下获得按键。
希望这会有所帮助。
Well, it's not the most elegant way, but it does exactly as you want:
The keyboard key event listener will get key presses now without showing the keyboard.
Hope this helps.
您需要使用
textInput
组件来处理此类事件。您可以在此处找到API: https://reaectnative.dev/docs/dextinput 。请记住,React库是作为API构建的,因此它可以利用不同的渲染。但是问题是,当使用移动机构渲染器时,您将不会在Web视图上拥有域属性,这是不同的。
you need to use a
TextInput
component to handle this kind of event. You can find the API here: https://reactnative.dev/docs/textinput.Bear in mind that react library is built as an API so it can take advantage of different renders. But the issue is that when using the mobile native renderer you will not have the DOM properties you would have on a web view, it is way different.
我不确定视图组件,但是对于TextInput,这可能会起作用!
对于功能部分:
I am not sure about the View component, but for TextInput this might work!
and for function part:
首先,您将
onkeydown = {keydownhandler}
添加到一个react元素。每次按键时,这都会触发您的keydownhandler
函数。例如:您可以这样设置您的功能:
First, you add
onKeyDown={keyDownHandler}
to a React element. This will trigger yourkeyDownHandler
function each time a key is pressed. Ex:You can set up your function like this: