1

I am developing Android application with lock functionality. please suggest me how to disable all the hard keys programmatically. here I am using below code to disable back button. I want like this functionality for all hard keys like home,search,camera, shortcut keys here is my code:

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_SEARCH) {
        Log.d("KeyPress", "search");
        return true;
    }
    return false;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Raghu Mudem
  • 6,793
  • 13
  • 48
  • 69

3 Answers3

0

Modify your onKey method to this :

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
    return true;
}

When you return true from onKey method, it means that you have handled the key press yourself and prevents the OS from taking the default action. In you code, you are only handling the search button, but when you return true for all cases, it will block all buttons.

P.S this might not work for soft buttons. Refer this

Kiran Kumar
  • 1,192
  • 8
  • 10
0

Override onKeyDown and onKeyUp func, return them true, it means you handle press action yourself. In onKeyUp, you can implement more logic for each key press action if you want.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
baonq-2356
  • 61
  • 4
-1

Try with this, it may solve your problem:

@Override

    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if ((keyCode == KeyEvent.KEYCODE_HOME)) {
            System.out.println("KEYCODE_HOME");
            return true;
        }
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            System.out.println("KEYCODE_BACK");
            return true;
        }
        if ((keyCode == KeyEvent.KEYCODE_MENU)) {
            System.out.println("KEYCODE_MENU");
            return true;
        }
        return false;
    }
Ramesh Reddy
  • 59
  • 12