Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect hardware key taps?

Tags:

flutter

I am at a point where I need to detect if any of the hardware buttons was presses when the flutter app is in foreground or when it is open.*

For example, when someone presses a volume or another button (even if it is the power off one) I want to perform some action in my app.

I know that when a flutter app is open and I am looking at the app logs and I tap any hardware button is tapped the lops related to that tap is printed in the logs.

Like when I press Vol down Key Down Tap Detected related logs are printed in logs.

How do can I perform a function when any of the above specified action is performed?

like image 273
Aman Malhotra Avatar asked Oct 29 '18 09:10

Aman Malhotra


2 Answers

New plugin called Hardware Buttons was released just a few days ago.

It supports volume button events and power button events, which seems like fits your need.

Maybe have a go with this?

like image 133
Hansol Avatar answered Oct 01 '22 20:10

Hansol


You will need find the proper flutter packages to do so.

Any system level call needs to use the platform channel api

https://flutter.io/platform-channels/

Here is the repository

https://pub.dartlang.org/flutter/

Here is an example of a method channel.

https://github.com/flutter/plugins/blob/master/packages/device_info/lib/device_info.dart

Here is an example of a stream listener.

https://github.com/flutter/plugins/tree/master/packages/sensors

The stream listener wasn't really well documented when I tried to grab mic fragments.

like image 44
user1462442 Avatar answered Oct 01 '22 21:10

user1462442