I want to call a SOS function when user long press the power button. If there is no library to do this, is there any native method to do so?
Asked
Active
Viewed 1,552 times
1
-
Does this answer your question? [Detect power button long press](https://stackoverflow.com/questions/39064523/detect-power-button-long-press) – M. Prokhorov Jan 20 '20 at 13:29
1 Answers
2
You can use this library to run a code when the home button is pressed
You can change the keycode creating a fork of the repository
and, check the long press with a timer.
UPDATE:
You can use this library to get a keycode event of key up and key down event.
Example:
componentDidMount() {
KeyEvent.onKeyDownListener((keyEvent) => {
if (keyEvent.keyCode === 'Keycode of the power button'){
this.timeout = setTimeout(() => {
//Your SOS Function here
}, 1000)
}
});
KeyEvent.onKeyUpListener((keyEvent) => {
if (keyEvent.keyCode === 'Keycode of the power button'){
clearTimeout(this.timeout)
}
})
}
componentWillUnmount() {
KeyEvent.removeKeyDownListener();
KeyEvent.removeKeyUpListener();
}

William Brochensque junior
- 2,385
- 18
- 35
- 46