react-native-keyevent
react-native-keyevent copied to clipboard
Weird issue with KeyDown/KeyUp/Multiple
Hello, I'm trying to detect multiple key press. Like alt+E, ctrl+d and such. Even c+d would help.
I've been trying, but I can't get it to work. I'm using RN 0.59 and the latest react-native-keyevent. I have tested this with an Android 6 and an Android 9 emulator, and an Android 6 device.
I'm currently reacting to keyUp, and if I press and hold, it is fired multiple times, wich I think is weird. Like, REALLY weird. I have tried to use the event.getRepeatCount() in order to just forward it once, but it doesn't work.
If I react to keyDown, it also gets called multiple times. The event.getRepeatCount() doesn't work here either. I have tried each and every combination of return super.method; and super.method; return true; and it doesn't work.
KeyMultiple never fires.
This is my mainactivity:
`package com.sampleapp;
import android.os.Bundle; import com.facebook.react.ReactFragmentActivity; import com.facebook.react.ReactActivityDelegate; import com.facebook.react.ReactRootView; import com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView; import android.view.KeyEvent; import com.github.kevinejohn.keyevent.KeyEventModule;
public class MainActivity extends ReactFragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(null);
}
/**
* Returns the name of the main component registered from JavaScript.
* This is used to schedule rendering of the component.
*/
@Override
protected String getMainComponentName() {
return "SampleApp";
}
@Override
protected ReactActivityDelegate createReactActivityDelegate() {
return new ReactActivityDelegate(this, getMainComponentName()) {
@Override
protected ReactRootView createRootView() {
return new RNGestureHandlerEnabledRootView(MainActivity.this);
}
};
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
// A. Prevent multiple events on long button press
// In the default behavior multiple events are fired if a button
// is pressed for a while. You can prevent this behavior if you
// forward only the first event:
if (event.getRepeatCount() == 0) {
KeyEventModule.getInstance().onKeyDownEvent(keyCode, event);
}
//
// B. If multiple Events shall be fired when the button is pressed
// for a while use this code:
// KeyEventModule.getInstance().onKeyDownEvent(keyCode, event);
//
// Using B.
//KeyEventModule.getInstance().onKeyDownEvent(keyCode, event);
// There are 2 ways this can be done:
// 1. Override the default keyboard event behavior
// super.onKeyDown(keyCode, event);
// return true;
// 2. Keep default keyboard event behavior
// return super.onKeyDown(keyCode, event);
// Using method #1 without blocking multiple
super.onKeyDown(keyCode, event);
return true;
}
@Override // <--- Add this method if you want to react to keyUp
public boolean onKeyUp(int keyCode, KeyEvent event) {
KeyEventModule.getInstance().onKeyUpEvent(keyCode, event);
// There are 2 ways this can be done:
// 1. Override the default keyboard event behavior
// super.onKeyUp(keyCode, event);
// return true;
// 2. Keep default keyboard event behavior
// return super.onKeyUp(keyCode, event);
// Using method #1
super.onKeyUp(keyCode, event);
return true;
}
@Override
public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
KeyEventModule.getInstance().onKeyMultipleEvent(keyCode, repeatCount, event);
return super.onKeyMultiple(keyCode, repeatCount, event);
}
}`
And this is on my reactions component componentDidMount:
`KeyEvent.onKeyUpListener((keyEvent) => {
console.log("keyboardListener", keyEvent, keyEvent.keyCode, this.props.scannerKeyCode);
keyHandlerFn(keyEvent, this.props.scannerKeyCode);
});
KeyEvent.onKeyMultipleListener((keyEvent) => {
console.log("multiple!",keyEvent.keyCode);
});`
Has someone experienced this? I'm a little lost, to be honest. I've been trying to setup a java debug session but right now it's impossible to me.
Thanks for your time.
I suppose that public boolean onKeyDown(int keyCode, KeyEvent event)
(and others) should be in onConfigurationChanged method, but it is not clear explained in docs and it easy miss.