Wednesday, May 22, 2013

Handling Keyboard Events In Android

When a user gives focus on an View like  an EditText element and the user has a hardware keyboard attached, all input is handled by the system. If, however, you'd like to intercept or directly handle the keyboard input yourself, you can do so by implementing callback methods from the KeyEvent.Callback interface, such as onKeyDown() and onKeyMultiple().

Both the Activity and View class implement the KeyEvent.Callback interface, so you should generally override the callback methods in your extension of these classes as appropriate.

Handling Single Key Events

To handle an individual key press, implement onKeyDown() or onKeyUp() as appropriate. Usually, we should use onKeyUp() if you want to be sure that you receive only one event. If the user presses and holds the button, then onKeyDown() is called multiple times.

Below is just s glimpse of some keys you see the full list of keycode  List of Key Code


@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    switch (keyCode) {
 
        case KeyEvent.KEYCODE_D:
            // Your code to handle event when 'D' is pressed
            return true;
 
        case KeyEvent.KEYCODE_F:
            // Your code to handle event when 'F' is pressed
             return true;
 
        case KeyEvent.KEYCODE_J:
            // Your code to handle event when 'J' is pressed 
            return true;
 
        case KeyEvent.KEYCODE_K:
            // Your code to handle event when 'K' is pressed 
            return true;
 
        default:
            return super.onKeyUp(keyCode, event);
    }
}
 
 

Handling Modifier keys like Shift, Control



To respond to modifier key events such as when a key is combined with Shift or Control, you can
query the KeyEvent that's passed to the callback method. Several methods
provide information about modifier keys such as getModifiers()
and getMetaState(). However, the simplest solution is to check whether
the exact modifier key you care about is being pressed with methods such as
isShiftPressed() and isCtrlPressed().

Example:


 here's the onKeyDown() implementation again, with some extra handling for when the Shift key is held down with one of the keys:

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    switch (keyCode) {
        ...
        case KeyEvent.KEYCODE_J:
            if (event.isShiftPressed()) {
                // Your Code
            } else {
                 // Your Code
            }
            return true;
        case KeyEvent.KEYCODE_K:
            if (event.isShiftPressed()) {
                // Your Code
            } else {
                 // Your Code
            }
            return true;
     }
      default:
            return super.onKeyUp(keyCode, event);

 }
 

4 comments:

  1. Attractive, post. I just stumbled upon your weblog and wanted to say that I have liked browsing your blog posts. After all, I will surely subscribe to your feed, and I hope you will write again soon! Extratorrent

    ReplyDelete
  2. Sub-$100 Price - I don't think the majority of users need a keyboard over $100. Sure some of them are very nice but in the end, programming keyboard couldn't you get the same usable features from a keyboard that costs less than $100?

    ReplyDelete
  3. This time, do something truly thoughtful and add personalized birthday wishes and birthday poems to your card or gift. Your friends and family will know that you really spent some time and effort, and they will appreciate how much they were in your thoughts, as you have taken the time to add a personal touch to your birthday offering for them. love shayari images

    ReplyDelete