flutter_hardware_buttons icon indicating copy to clipboard operation
flutter_hardware_buttons copied to clipboard

Intercept hardware button events completely?

Open hicnar opened this issue 6 years ago • 14 comments

Hi,

Is it possible to intercept events generated by hardware buttons completely? 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?

Thanks in advance for your reply! K.

hicnar avatar Nov 30 '19 00:11 hicnar

Hello @hicnar ! Yeah actually, this implementation was a to-do but was delaying on it cause we thought there was no demand for this.

Since we now know you want it, we can start working on it.

However, this would only be possible on Android and not on iOS. I'll let you know when we're done with this.

Thanks!

giantsol avatar Nov 30 '19 10:11 giantsol

Thanks for replying! Why is it not possible on iOS?

hicnar avatar Nov 30 '19 13:11 hicnar

As far as we saw, there was no way to block default behaviors on iOS. If you know any, please let us know.

giantsol avatar Nov 30 '19 13:11 giantsol

Hello @hicnar , We'll start working on it this weekend. Just to let you know :)

giantsol avatar Dec 09 '19 04:12 giantsol

same issue

bahador-r avatar Jul 01 '20 05:07 bahador-r

Same issue here does anyone have a solution for this?

7Eltantawy avatar Jan 28 '21 23:01 7Eltantawy

@HasanEltantawy & @bahador-r

For Android, the solution is implementing onKeyUp/Down in the way shown below. There's no viable solution for iOS as Apple explicitly bans applications that attempt to intercept or amend the default hardware buttons functionality.

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
  }
}

hicnar avatar Jan 29 '21 11:01 hicnar

@hicnar I tried to implement that code in MainActivity.java but the compiler throws these errors below

The code

package com.hassaneltantawy.hisnelmoslem;

import io.flutter.embedding.android.FlutterActivity;

public class MainActivity extends 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
    }
    
}

Errors

\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:7: error: ';' expected
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
                ^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:7: error: invalid method declaration; return type required
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
                 ^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:7: error: <identifier> expected
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
                                  ^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:7: error: ';' expected
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
                                   ^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:7: error: illegal start of type
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
                                       ^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:7: error: ';' expected
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
                                              ^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:7: error: <identifier> expected
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
                                                        ^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:7: error: illegal start of type
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
                                                         ^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:7: error: <identifier> expected
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
                                                          ^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:7: error: ';' expected
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
                                                           ^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:8: error: ';' expected
        super.onKeyDown(keyCode, event)
                                       ^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:9: error: ';' expected
        return true
                   ^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:12: error: ';' expected
    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
                ^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:12: error: invalid method declaration; return type required
    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
                 ^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:12: error: <identifier> expected
    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
                                ^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:12: error: ';' expected
    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
                                 ^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:12: error: illegal start of type
    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
                                     ^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:12: error: ';' expected
    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
                                            ^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:12: error: <identifier> expected
    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
                                                      ^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:12: error: illegal start of type
    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
                                                       ^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:12: error: <identifier> expected
    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
                                                        ^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:12: error: ';' expected
    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
                                                         ^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:13: error: ';' expected
        super.onKeyUp(keyCode, event)
                                     ^
\android\app\src\main\java\com\hassaneltantawy\hisnelmoslem\MainActivity.java:14: error: ';' expected
        return true
                   ^
24 errors

7Eltantawy avatar Jan 29 '21 13:01 7Eltantawy

I make it like this and it run but the problem is still there the volume go up and down wen i press it

package com.hassaneltantawy.hisnelmoslem;
import android.view.KeyEvent;
import io.flutter.embedding.android.FlutterActivity;

public class MainActivity extends FlutterActivity {

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
         super.onKeyDown(keyCode, event);
        return true;
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
         super.onKeyUp(keyCode, event);
        return true;
    }

}

7Eltantawy avatar Jan 29 '21 14:01 7Eltantawy

@HasanEltantawy My code sample was in Kotlin and you've go Java there, sorry should have mentioned it. Anyway that's pretty bizarre there should be no difference if it's Java or Kotlin. Did you try to put that code in a new project, just to explore if it works or you placed it in the project where you actually need it to work? If I were you I would create a new project with minimal amount of code and experiment on it a bit. If possible generate a new project with Android backed by Kotlin.

hicnar avatar Jan 29 '21 15:01 hicnar

Ok I'll try to do that with anew project. I hope it'll work thanks @hicnar

7Eltantawy avatar Jan 29 '21 15:01 7Eltantawy

Even with new app and also implementing kotlin code the problem is still existing. @hicnar

7Eltantawy avatar Jan 29 '21 17:01 7Eltantawy

@HasanEltantawy I have just modified the MainActivity.kt in my little template project that I use for re-creating Flutter issues I occasionally find and report - the project is called fluttery_stuff. Go ahead, clone it and run on your Android device. I can confirm that once I tested that just a few minutes ago, the hardware buttons (volume up/down) were fully intercepted and pressing them did not result in displaying the volume bar on my Samsung S9. The class you can run is https://github.com/hicnar/fluttery_stuff/blob/main/lib/dialogs/main.dart

hicnar avatar Feb 02 '21 15:02 hicnar

@hicnar On my Samsung Galaxy S7 the implementation is working, too.
Here you can see the source code of @hicnar's Android implementation (Kotlin).

kinafu avatar Sep 07 '21 12:09 kinafu