android - react native android 键盘事件

标签 android keyboard-events react-native

我一直在尝试创建自定义 react native 自定义键盘事件。 具体来说,我一直在尝试检测退格键。

在我的MainApplication.java

public boolean onKeyUp(int keyCode, KeyEvent event) {

  // Filter for delete key being pressed
  if (event.getAction() == KeyEvent.DEL) {
    getReactNativeHost().getReactInstanceManager().getCurrentRea‌​ctContext()
    .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
    .emit("onKeyPressed", keyCode);
  }
  return true;
}

在我的 react 组件中

componentDidMount() {
  if (Platform.OS === 'android') {
    DeviceEventEmitter.addListener('onKeyPressed', this.handleKeyPress);
  }
}

componentWillUnmount() {
    if (Platform.OS === 'android') {
      DeviceEventEmitter.removeListener('onKeyPressed');
    }
}

有人可以给我一些关于为什么没有触发事件的想法吗?

最佳答案

在对 android 开发进行更多研究之后。

使用 Keyevents 检测按下的键是不可靠的。

https://developer.android.com/reference/android/view/KeyEvent.html

As soft input methods can use multiple and inventive ways of inputting text, there is no guarantee that any key press on a soft keyboard will generate a key event: this is left to the IME's discretion, and in fact sending such events is discouraged.

关于android - react native android 键盘事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44563545/

相关文章:

java - Android Activity销毁时如何销毁Native进程

java - 在 Android 中,如何查看 try/catch block 中收到的错误?

Android webview 下拉菜单不适用于 Android 4.x 设备

javascript - 使用javascript更改键盘布局

android - React Native (Android) - 应用程序在 "Open with"列表中出现两次

java - Android:放置多个 fragment 共享的功能的位置

linux - Windows 中的键盘缓冲区

linux - 在 Linux 上使用 Python 拦截和发送击键

android - 无法将任务 'wrapper' 添加为具有该名称的任务已存在

javascript - FlatList 不会在 React-Native 中渲染任何文本