Cdacians

Cdacians
Cdacians

Thursday, 5 January 2012

Example code for OnKeyDown listener in Android

In this example Toast is shown for the key presses.



The approach is simple, just we need to put this code, thats all


Java
@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    super.onKeyDown(keyCode, event);
        switch(keyCode)
        {
        case KeyEvent.KEYCODE_CAMERA:
         Toast.makeText(KeyActions.this, "Pressed Camera Button", Toast.LENGTH_SHORT).show();
            return true;
        case KeyEvent.KEYCODE_1:
         Toast.makeText(KeyActions.this, "Pressed 1", Toast.LENGTH_SHORT).show();
            return true;
        case KeyEvent.KEYCODE_HOME:
         Toast.makeText(KeyActions.this, "Pressed Home Button", Toast.LENGTH_SHORT).show();
            return true;
            
        case KeyEvent.KEYCODE_BACK:
         Toast.makeText(KeyActions.this, "Pressed Back Button", Toast.LENGTH_SHORT).show();
         Intent result = new Intent("Complete");
            setResult(Activity.RESULT_OK, result);
            finish();
            return true;
        }

        return false;
    }


For every key there is the code, after entering the KeyEvent. the eclipse will show the list of codes available.

If you have doubts means 



Thanks 
akm from cdacians(www.cdacians.com)

No comments:

Post a Comment