15.10. Scroll Bars

 
[Page 479 ( continued )]

14.5. Key Events

Key events enable the use of the keys to control and perform actions or get input from the keyboard. A key event is fired whenever a key is pressed, released, or typed on a component. The KeyEvent object describes the nature of the event (namely, that a key has been pressed, released, or typed) and the value of the key, as shown in Figure 14.13. Java provides the KeyListener to handle key events, as shown in Figure 14.14.

Figure 14.13. The KeyEvent class encapsulates information about key events.
(This item is displayed on page 480 in the print version)

Figure 14.14. The KeyListener interface handles key pressed, released, and typed events.
(This item is displayed on page 480 in the print version)

The keyPressed handler is invoked when a key is pressed, the keyReleased handler is invoked when a key is released, and the keyTyped handler is invoked when a Unicode character is entered. If a key does not have a Unicode (e.g., function keys, modifier keys, action keys, and control keys), the keyTyped handler will be not be invoked.


[Page 480]

Every key event has an associated key character or key code that is returned by the getKeyChar() or getKeyCode() method in KeyEvent . The key codes are constants defined in Table 14.4. For a key of the Unicode character, the key code is the same as the Unicode value. For the key pressed and key released events, getKeyCode() returns the value as defined in the table. For the key typed event, getKeyCode() returns VK_UNDEFINED .

Table 14.4. Key Constants
Constant Description
VK_HOME The Home key
VK_END The End key
VK_PGUP The Page Up key
VK_PGDN The Page Down key
VK_UP The up-arrow key
VK_DOWN The down-arrow key
VK_LEFT The left-arrow key
VK_RIGHT The right-arrow key
VK_ESCAPE The Esc key
VK_TAB The Tab key
VK_CONTROL The Control key
VK_SHIFT The Shift key
VK_BACK_SPACE The Backspace key
VK_CAPS_LOCK The Caps Lock key
VK_NUM_LOCK The Num Lock key
VK_ENTER The Enter key
VK_UNDEFINED The keyCode unknown
VK_F1 to VK_F12 The function keys from F1 to F12
VK_0 to VK_9 The number keys from 0 to 9
VK_A to VK_Z The letter keys from A to Z

The program in Listing 14.8 displays a user-input character. The user can move the character up, down, left, and right, using the arrow keys VK_UP , VK_DOWN , VK_LEFT , and VK_RIGHT . Figure 14.15 contains a sample run of the program.

Figure 14.15. The program responds to key events by displaying a character and moving it up, down, left, or right.
(This item is displayed on page 482 in the print version)


Listing 14.8. KeyEventDemo.java
(This item is displayed on pages 480 - 481 in the print version)
 1   import   java.awt.*; 2   import   java.awt.event.*; 3   import   javax.swing.*; 

[Page 481]
 4 5   public class   KeyEventDemo   extends   JFrame { 6    private   KeyboardPanel keyboardPanel =   new   KeyboardPanel();  7 8  /** Initialize UI */  9   public   KeyEventDemo() { 10  // Add the keyboard panel to accept and display user input  11 add(keyboardPanel); 12 13  // Set focus  14  keyboardPanel.setFocusable(   true   );  15 } 16 17  /** Main method */  18   public static void   main(String[] args) { 19 KeyEventDemo frame =   new   KeyEventDemo(); 20 frame.setTitle(   "KeyEventDemo"   ); 21 frame.setLocationRelativeTo(   null   );  // Center the frame  22 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 23 frame.setSize(   300   ,   300   ); 24 frame.setVisible(   true   ); 25 } 26 27  // Inner class: KeyboardPanel for receiving key input  28    static class   KeyboardPanel   extends   JPanel {  29   private int   x =   100   ; 30   private int   y =   100   ; 31   private char   keyChar =   'A'   ;  // Default key  32 33   public   KeyboardPanel() { 34 addKeyListener(    new   KeyAdapter() {  35    public void   keyPressed(KeyEvent e) {  36   switch   (e.getKeyCode()) { 37   case   KeyEvent.VK_DOWN: y +=   10   ;   break   ; 38   case   KeyEvent.VK_UP: y -=   10   ;   break   ; 39   case   KeyEvent.VK_LEFT: x -=   10   ;   break   ; 40   case   KeyEvent.VK_RIGHT: x +=   10   ;   break   ; 41 default: keyChar = e.getKeyChar(); 42 } 43 44 repaint(); 45 } 46  }  ); 47 } 48 49  /** Draw the character */  50   protected void   paintComponent(Graphics g) { 51   super   .paintComponent(g); 52 53 g.setFont(   new   Font(   "TimesRoman"   , Font.PLAIN,   24   )); 54 g.drawString(String.valueOf(keyChar), x, y); 55 } 56 } 57 } 

The KeyboardPanel class extends JPanel to display a character (line 28). This class is declared as an inner class inside the main class because it is only used in this class. Furthermore, the inner class is declared static because it does not reference any instance members of the main class.


[Page 482]

Because the program gets input from the keyboard, it listens for KeyEvent and extends KeyAdapter to handle key input (line 34).

When a key is pressed, the keyPressed handler is invoked. The program uses e.getKeyCode() to obtain the key code and e.getKeyChar() to get the character for the key. When a non-arrow key is pressed, the key is displayed (line 41). When an arrow key is pressed, the character moves in the direction indicated by the arrow key (lines 37 “40).

Only a focused component can receive KeyEvent . To make a component focusable , set its isFocusable property to true (line 14).

Every time the component is repainted, a new font is created for the Graphics object in line 53. This is not efficient. It is better to create the font one time as a data field.

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net