6

Is it possible to intercept events generated by hardware buttons completely in Flutter? For example if the app is active and volume up/down button is pressed, is it possible to intercept that event in the app, handle it in the app specific way and most importantly prevent it from being acted upon further by the system so that the default behaviour that actually turns the volume up/down and displays the system dialog/popup showing the volume going up/down would not happen?

I tried to use the hardware_buttons flutter library/plugin. It allows me to react when buttons are pressed, but the events still get propagated on so the system also handles them in the regular way.

Thanks in advance for your suggestions!

hicnar
  • 163
  • 3
  • 9
  • Does this answer your question? [Access hardware buttons (like the volume buttons) from Flutter](https://stackoverflow.com/questions/55678814/access-hardware-buttons-like-the-volume-buttons-from-flutter) – Akhilesh Pothuri Nov 30 '19 at 06:26
  • 1
    Nope it does not. I mention this plugin in my question, I am aware of it. It does not allow me to handle the events exclusively by mu app without propagating them further – hicnar Nov 30 '19 at 10:35
  • did you get any way to do this @hicnar? The only lib I found is now deprecated. :( https://github.com/flutter-moum/flutter_hardware_buttons – Eradicatore Dec 19 '20 at 12:40
  • On Android you can do that with ease and all you have to do is override onKeyDown and onKeyUp functions in your main activity so it should look somewhat like this: `class MainActivity: FlutterActivity() { override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean { super.onKeyDown(keyCode, event) return true } override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean { super.onKeyUp(keyCode, event) return true } }` Regarding Apple, AFAIK your app will be rejected if it modifies the default behaviour of the hardware buttons. – hicnar Dec 25 '20 at 15:34

3 Answers3

1

Brother, you can easily do so with the built-in Widget RawKeyboardListener, and the most amazing thing about this widget is it also listens to the button presses of any USB device connected through OTG or any bluetooth device, like wireless earbuds, you can even incorporate the tap gestures of wireless buds with this Class. Hope you find this helpful, have a great life ahead!

home: RawKeyboardListener(
    focusNode: focusnode,
    autofocus: true,
    onKey: (event) {
      setState(() {
        eventt = event.logicalKey.keyLabel;
        print(eventt);
        print('outside if');
        print(event.runtimeType);
        if (event.runtimeType == RawKeyDownEvent) {
          print('inside if');
        if (eventt == 'Audio Volume Up'){
          print('Volume up');
          sendMessage('j');
        }
        if (eventt =='Audio Volume Down'){
          print('Volume Down');
          sendMessage('h');
        }
          if (eventt == 'Media Track Previous'){
            print('Volume up');
            sendMessage('h');
          }
          if (eventt =='Media Track Next'){
            print('Volume Down');
            sendMessage('j');
          }
        }
      });
    },
    child: Scaffold(
0

volume_watcher worked for me. You can either:

  • Use widget for listening volume events:
import 'package:volume_watcher/volume_watcher.dart';

VolumeWatcher(
  onVolumeChangeListener: (double volume) {
    // do stuff
  },
)
  • Or listen to events directly:
import 'package:volume_watcher/volume_watcher.dart';

int _volumeListenerId;

@override
void initState() {
  super.initState();
  _volumeListenerId = VolumeWatcher.addListener((volume) {
    // do stuff
  });
}

@override
void dispose() {
  VolumeWatcher.removeListener(_volumeListenerId);
  super.dispose();
}
Denis Gordin
  • 892
  • 10
  • 22
0

The answer is NO, while you can listen to them there is no way to consume them and stop the OS from acting on them. The other example question is just listening to the volume.

Alan Meier
  • 41
  • 4