1 page.title=Handling Keyboard Actions 2 3 trainingnavtop=true 4 5 @jd:body 6 7 <div id="tb-wrapper"> 8 <div id="tb"> 9 10 <h2>This lesson teaches you to</h2> 11 <ol> 12 <li><a href="#SingleKey">Handle Single Key Events</a></li> 13 <li><a href="#ModifierKey">Handle Modifier Keys</a></li> 14 </ol> 15 16 </div> 17 </div> 18 19 20 <p>When the user gives focus to an editable text view such as an {@link android.widget.EditText} 21 element and the user has a hardware keyboard attached, all 22 input is handled by the system. If, however, you'd like to intercept 23 or directly handle the keyboard input yourself, you can do so by implementing callback methods 24 from the {@link android.view.KeyEvent.Callback} interface, such as {@link 25 android.view.KeyEvent.Callback#onKeyDown onKeyDown()} and {@link 26 android.view.KeyEvent.Callback#onKeyMultiple onKeyMultiple()}.</p> 27 28 <p>Both the {@link 29 android.app.Activity} and {@link android.view.View} class implement the 30 {@link android.view.KeyEvent.Callback} interface, so you 31 should generally override the callback methods in your extension of these classes as 32 appropriate.</p> 33 34 <p class="note"><strong>Note:</strong> When handling keyboard events with the {@link 35 android.view.KeyEvent} class and related APIs, you should expect that such keyboard 36 events come only from a hardware keyboard. You should never rely on receiving key events 37 for any key on a soft input method (an on-screen keyboard).</p> 38 39 40 <h2 id="SingleKey">Handle Single Key Events</h2> 41 42 <p>To handle an individual key press, implement {@link 43 android.app.Activity#onKeyDown onKeyDown()} or {@link 44 android.app.Activity#onKeyUp onKeyUp()} as appropriate. Usually, you should 45 use {@link android.app.Activity#onKeyUp onKeyUp()} if you want to be sure that you receive 46 only one event. If the user presses and holds the button, then {@link 47 android.app.Activity#onKeyDown onKeyDown()} is called multiple times.</p> 48 49 <p>For example, this implementation responds to some keyboard keys to control a game:</p> 50 51 <pre> 52 @Override 53 public boolean onKeyUp(int keyCode, KeyEvent event) { 54 switch (keyCode) { 55 case KeyEvent.KEYCODE_D: 56 moveShip(MOVE_LEFT); 57 return true; 58 case KeyEvent.KEYCODE_F: 59 moveShip(MOVE_RIGHT); 60 return true; 61 case KeyEvent.KEYCODE_J: 62 fireMachineGun(); 63 return true; 64 case KeyEvent.KEYCODE_K: 65 fireMissile(); 66 return true; 67 default: 68 return super.onKeyUp(keyCode, event); 69 } 70 } 71 </pre> 72 73 74 <h2 id="ModifierKey">Handle Modifier Keys</h2> 75 76 <p>To respond to modifier key events such as when a key is combined with Shift or Control, you can 77 query the {@link android.view.KeyEvent} that's passed to the callback method. Several methods 78 provide information about modifier keys such as {@link android.view.KeyEvent#getModifiers()} 79 and {@link android.view.KeyEvent#getMetaState()}. However, the simplest solution is to check whether 80 the exact modifier key you care about is being pressed with methods such as 81 {@link android.view.KeyEvent#isShiftPressed()} and {@link android.view.KeyEvent#isCtrlPressed()}. 82 </p> 83 84 <p>For example, here's the {@link android.app.Activity#onKeyDown onKeyDown()} implementation 85 again, with some extra handling for when the Shift key is held down with one of the keys:</p> 86 87 <pre> 88 @Override 89 public boolean onKeyUp(int keyCode, KeyEvent event) { 90 switch (keyCode) { 91 ... 92 case KeyEvent.KEYCODE_J: 93 if (event.isShiftPressed()) { 94 fireLaser(); 95 } else { 96 fireMachineGun(); 97 } 98 return true; 99 case KeyEvent.KEYCODE_K: 100 if (event.isShiftPressed()) { 101 fireSeekingMissle(); 102 } else { 103 fireMissile(); 104 } 105 return true; 106 default: 107 return super.onKeyUp(keyCode, event); 108 } 109 } 110 </pre> 111 112 113 114