反应本机 android 键盘事件
React native android keyboard events
我一直在尝试创建自定义 React 本机自定义键盘事件。
具体来说,我一直在尝试检测退格键。
在我的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.
我一直在尝试创建自定义 React 本机自定义键盘事件。 具体来说,我一直在尝试检测退格键。
在我的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.