You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native Windows中onKeyPress无法获取按键值问题求助

React Native 0.54.0 on Windows: TextInput onKeyPress Issues & Solutions

Hey there! Let's break down your problem and find a solution that fits your needs.

First: Is onKeyPress Compatible with Windows in RN 0.54.0?

Short answer: Not really. React Native 0.54.0 just introduced onKeyPress support for Android, but the Windows platform hadn't caught up yet at that version. The empty SyntheticEvent you're seeing (all properties null) is a dead giveaway— the native Windows bridge wasn't properly passing key event data up to the JavaScript layer back then. This isn't a mistake on your end; it's a framework limitation for that specific RN version on Windows.

Your Core Goal: Listening for the Enter Key

Since your main need is to detect when the enter key is pressed, here are two reliable workarounds that work in RN 0.54.0 on Windows:

1. Use onChange to Detect Newline Characters

In multiline mode, pressing enter adds a newline (\n) to the input text. You can leverage this in your onChange handler to spot when enter is pressed:

<TextInput
  ref={this.setTextInputRef}
  onChange={(e) => {
    const newText = e.nativeEvent.text;
    this.setState({ currentString: newText });
    
    // Check if the last character is a newline (enter key was pressed)
    if (newText.endsWith('\n')) {
      this.doAThing(); // Trigger your desired action
      
      // Optional: Remove the newline if you don't want it in the input
      this.setState({ currentString: newText.slice(0, -1) });
    }
  }}
  onSubmitEditing={() => this.doAThing()}
  multiline={true}
/>

This approach is cross-platform, simple to implement, and doesn't rely on platform-specific key events—perfect for your RN 0.54.0 setup.

2. Build a Custom Native Module (Advanced)

If you need to detect more keys beyond just enter, you can create a custom Windows native module to handle key events directly. Here's a quick outline:

  • Wrap the standard Windows TextBox control in a custom native component.
  • Add a key-down event listener in the C# native code.
  • When the enter key is detected, send an event back to your React Native JavaScript code.
  • Replace the default TextInput with this custom component.

This takes more work, but gives you full control over key detection. Just make sure the module is compatible with RN 0.54.0's Windows bridge.

Optional: Upgrade for Proper onKeyPress Support

If you can afford to upgrade your React Native version, later releases (around RN 0.60 and above) added full Windows support for onKeyPress. With an upgraded version, you could use this clean approach:

onKeyPress={(e) => {
  if (e.nativeEvent.key === 'Enter') {
    this.doAThing();
  }
}}

But if upgrading isn't feasible right now, the onChange workaround is your most reliable option.

内容的提问来源于stack exchange,提问作者Grey Haven

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:30:37