如何在React Native中获取用户按键?

发布于 2025-02-08 20:27:51 字数 808 浏览 1 评论 0原文

如何在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

潇烟暮雨 2025-02-15 20:27:51

如果您正在寻找物理键盘处理,那么到目前为止,没有物理键盘键盘处理功能。

您可以尝试第三方库,例如我的: https:https:///www.npmjs .com/package/react-native-external-keyboard

 import {ExternalKeyboardView} from 'react-native-external-keyboard';
  <ExternalKeyboardView
    onKeyDownPress={onKeyDownHandler}
    onKeyUpPress={onKeyUpHandler}
    canBeFocused
  >

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

 import {ExternalKeyboardView} from 'react-native-external-keyboard';
  <ExternalKeyboardView
    onKeyDownPress={onKeyDownHandler}
    onKeyUpPress={onKeyUpHandler}
    canBeFocused
  >
赏烟花じ飞满天 2025-02-15 20:27:51

好吧,这不是最优雅的方式,但它完全按照您的意愿进行:

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

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

键盘钥匙事件侦听器现在可以在不显示键盘的情况下获得按键。
希望这会有所帮助。

Well, it's not the most elegant way, but it does exactly as you want:

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

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

The keyboard key event listener will get key presses now without showing the keyboard.
Hope this helps.

笙痞 2025-02-15 20:27:51

您需要使用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.

楠木可依 2025-02-15 20:27:51

我不确定视图组件,但是对于TextInput,这可能会起作用!

<TextInput
    onKeyPress={handleKeyDown}
    placeholder="Sample Text"
/>

对于功能部分:

const handleKeyDown = (e) => {
    if(e.nativeEvent.key == "Enter"){
        dismissKeyboard();
    }
}

I am not sure about the View component, but for TextInput this might work!

<TextInput
    onKeyPress={handleKeyDown}
    placeholder="Sample Text"
/>

and for function part:

const handleKeyDown = (e) => {
    if(e.nativeEvent.key == "Enter"){
        dismissKeyboard();
    }
}
执手闯天涯 2025-02-15 20:27:51

首先,您将onkeydown = {keydownhandler}添加到一个react元素。每次按键时,这都会触发您的keydownhandler函数。例如:

<div className='example-class' onKeyDown={keyDownHandler}>

您可以这样设置您的功能:

const keyDownHandler = (
    event: React.KeyboardEvent<HTMLDivElement>
) => {
        if (event.code === 'ArrowDown') {
            // do something
        } else if (event.code === 'A') {
            // do something
        }
}

First, you add onKeyDown={keyDownHandler} to a React element. This will trigger your keyDownHandler function each time a key is pressed. Ex:

<div className='example-class' onKeyDown={keyDownHandler}>

You can set up your function like this:

const keyDownHandler = (
    event: React.KeyboardEvent<HTMLDivElement>
) => {
        if (event.code === 'ArrowDown') {
            // do something
        } else if (event.code === 'A') {
            // do something
        }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文