我一直在尝试创建自定义 react native 自定义键盘事件。 具体来说,我一直在尝试检测退格键。
在我的MainApplication.java
public boolean onKeyUp(int keyCode, KeyEvent event) {
// Filter for delete key being pressed
if (event.getAction() == KeyEvent.DEL) {
getReactNativeHost().getReactInstanceManager().getCurrentReactContext()
.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/