Capture external usb or bluetooth hardware mouse events like scroll and hover. Useful for RN applications that run on Chromebooks.
Supports Android / Chromebooks only
Learn about Android MotionEvent here.
Run npm install react-native-keyevent --save
Run yarn add react-native-keyevent
No manual linking required on RN >=0.60 and module is not supporting React Native <= 0.59
Implement onConfigurationChanged method in MainActivity.java
import android.view.MotionEvent; // <--- import
import com.github.andrewkirkovski.usbmouse.UsbMouseModule; // <--- import
public class MainActivity extends ReactActivity {
......
@Override
public boolean onGenericMotionEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_HOVER_MOVE) {
UsbMouseModule.getInstance().onMouseHover(event);
// return true; // <-- Uncomment to prevent default handler
} else if (event.getAction() == MotionEvent.ACTION_SCROLL) {
UsbMouseModule.getInstance().onMouseScroll(event);
// return true; // <-- Uncomment to prevent default handler
}
UsbMouseModule.getInstance().onGenericMotionEvent(event); // Comment out if you dont need all events
// return true; // <-- Uncomment to prevent default handler
return super.onGenericMotionEvent(event);
}
}
Whenever you want to use it within React Native code now you can:
import KeyEvent from 'react-native-keyevent';
import * as UsbMouse from 'react-native-usb-mouse-android';
useEffect(()=>{
const listener = UsbMouse.onMouseScroll((event: GenericMotionEvent)=>{
console.log('Mouse Scroll', event);
});
return ()=>listener.remove();
}, []);
- iOS Support
- Automatic MainActivity configuration