0

I'm trying to catch the back key event using boolean onKeyDown(int keyCode, KeyEvent event) while showing the soft keyboard. But it is not working...

Himanshu
  • 31,810
  • 31
  • 111
  • 133
Shamitha
  • 1,407
  • 1
  • 10
  • 9

2 Answers2

1

override this method of Edittext:

@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK) {
        //put your logic here
        }
    return super.onKeyPreIme(keyCode, event);
}

you will get event on backpress inside if.

Chirag Shah
  • 2,058
  • 2
  • 20
  • 30
-1

Try the following:

if(event.getKeyCode() == KeyEvent.KEYCODE_BACK){
//your code
return true;
}

Warning: You are informing Android system that you already handled the back key press and it will not perform as intended inside the Application (will not exit).

Mohamed_AbdAllah
  • 5,311
  • 3
  • 28
  • 47
  • onKeyDown() isn't called when the SoftKeyboard isn't hidden. – Graeme Jul 30 '12 at 09:31
  • From the Document, I think it is possible :Key presses in software keyboards will generally NOT trigger this listener, *although some may elect to do so in some situations* – Mohamed_AbdAllah Jul 30 '12 at 10:40