Key events can use the keyboard to control and perform some actions, or to get input from the keyboard, as long as the press, releasing a key or tapping on a component, triggers a key event. The KeyEvent object describes the characteristics of the event (press, release, or tap a key) and the corresponding value. Java provides keylistener interface for handling key events.
When a key is pressed, the keypressed processor is called, and when a key is released, the keyreleased processor is called, and the keytyped processor is called when a uniform encoding is entered. If this key is not a unified code (such as function keys, modifier keys, action keys and Control keys) Each keystroke event has a related key character and key code, which is returned by the Getkeychar () and getKeyCode () methods in KeyEvent respectively getkeychar (): char Returns the key-related characters in this event  G Etkeycode (): int Returns the integer key associated with the key in this event keypressed (e:keyevent) &nbs P called after pressing a key on the source component keyreleased (e:keyevent) Releasing a key on the source component after it is called keytyped (e:keyevent) & nbsp Press a key on the source component and then release the key after being called Key constants vk_home HOME key &N Bsp vk_control Control key vk_end end &NBS P Vk_shift SHIFT key Vk_pgup &NB Sp;page up vk_back_space &NBSP ; BACKSPACE vk_pgdn page down key &NB Sp Vk_caps_lock case lock key vk_up UP ARROW vk_num_l Ock keypad LOCK key
Vk_down down ARROW &NBSP ; vk_enter Enter Vk_left LEFT arrow &NBSP ; vk_undefined unknown key vk_right RIGHT ARROW &nbs P VK_F1--VK_F12 &NB Sp  F1-f12vk_escape ESC & nbsp  VK_0--vk_9 0---9vk_tab TAB &NBSP ; VK_A--vk_z  A---- Z getkeycode () returns the value defined in the table Getkeychar () returns the input character
1 Importjava.awt.*;2 Importjava.awt.event.*;3 Importjavax.swing.*;4 5 Public classKeyeventdemoextendsjframe{6 PrivateKeyboardpanel Keyboardpanel =NewKeyboardpanel ();7 8 PublicKeyeventdemo () {9 Add (keyboardpanel);Ten OneKeyboardpanel.setfocusable (true); A - } - the Public Static voidMain (string[] args) { -Keyeventdemo frame =NewKeyeventdemo (); -Frame.settitle ("Keyeventdemo"); -Frame.setsize (300,300); +Frame.setlocationrelativeto (NULL); - frame.setdefaultcloseoperation (JFrame. Exit_on_close); +Frame.setvisible (true); A at } - - Static classKeyboardpanelextendsjpanel{ - Private intx = 100; - Private inty = 100; - Private CharKeyChar = ' A ' ; in - PublicKeyboardpanel () { toAddkeylistener (NewKeyadapter () { + Public voidkeypressed (KeyEvent e) { - Switch(E.getkeycode ()) { the CaseKeyevent.vk_down:y + = 10; Break; * CaseKeyevent.vk_up:y-= 10; Break; $ CaseKeyevent.vk_left:x-= 10; Break;Panax Notoginseng CaseKeyevent.vk_right:x + = 10; Break; - default: KeyChar =E.getkeychar (); the } + A repaint (); the } + }); - } $ $ protected voidpaintcomponent (Graphics g) { - Super. paintcomponent (g); - theG.setfont (NewFont ("Timesroman", font.plain,24)); - g.drawstring (String. ValueOf (KeyChar), x, y);Wuyi } the } -}
Java key Event KeyEvent