KeyAdapter Example getKeyChar() getKeyCode()

Note 1: Before going into this notes, if not aware of about adapter classes, It is advised to go through Java AWT Adapters.

Note 2: For notes on integration of classes (it is done here also), refer Applet: MouseMotionAdapter getX() and getY() Example .

Two methods getKeyChar() and getKeyCode() of KeyEvent class are used in this program. Let us see what Java Designer says about these methods.

  1. char getKeyChar(): Returns the character associated with the key in this event. For example, the KEY_TYPED event for shift + "a" returns the value for "A".
  2. int getKeyCode(): Returns the integer keyCode associated with the key in this event.

Note: getKeyCode() returns an integer value of the keyboard key as defined in the underlying OS, but not ASCII value.

KeyAdapter Example

Following are the two programs (HTML and Applet) involved.

HTML file to run applet


 

Applet File Name: AppletKeyAdapterExample.java

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class AppletKeyAdapterExample extends Applet
{
  String keyMessage = "";
  int keyCode;

  public void init()
  {
    addKeyListener(new HelperClass(this));
  }
  public void paint(Graphics g)
  {
    g.drawString("You typed " + keyMessage + " and its code is " + keyCode, 50, 100);
  }
}

class HelperClass extends KeyAdapter
{
  AppletKeyAdapterExample akae2;

  public HelperClass(AppletKeyAdapterExample akae1)
  {
    akae2 = akae1;
  }
  public void keyPressed(KeyEvent e)
  {
    akae2.showStatus("A Key is Pressed");
    int keyNumber = e.getKeyCode();
    
    switch(keyNumber)
    {
      case KeyEvent.VK_F1:
        akae2.keyMessage = "Funcitonal key F1";
        akae2.keyCode = e.getKeyCode();
        break;
      case KeyEvent.VK_F2:
        akae2.keyMessage = "Funcitonal key F2";
        akae2.keyCode = e.getKeyCode();            
      case KeyEvent.VK_F3:
        akae2.keyMessage = "Funcitonal key F3";
        akae2.keyCode = e.getKeyCode();            
        break;
      case KeyEvent.VK_F4:
        akae2.keyMessage = "Funcitonal key F4";
        akae2.keyCode = e.getKeyCode();            
        break;
      case KeyEvent.VK_F5:		  // like this you can go up to F24
        akae2.keyMessage = "Funcitonal key F5";
        akae2.keyCode = e.getKeyCode();            
        break;
      case KeyEvent.VK_PAGE_DOWN:         // like this you can go for VK_PAGE_UP
        akae2.keyMessage = "Page Down";
        akae2.keyCode = e.getKeyCode();            
        break;
      case KeyEvent.VK_LEFT:	          // like this you can go for VK_RIGHT
        akae2.keyMessage = "Left arrow";
        akae2.keyCode = e.getKeyCode();            
        break;
      case KeyEvent.VK_UP:
        akae2.keyMessage = "Key up";
        akae2.keyCode = e.getKeyCode();            
        break;
      case KeyEvent.VK_DOWN:
        akae2.keyMessage = "Down arrow";
        akae2.keyCode = e.getKeyCode();            
        break;
    }
    akae2.repaint();
  }
  public void keyReleased(KeyEvent e)
  {
    akae2.keyMessage = ""+ e.getKeyChar();
    //  akae2.keyCode = e.getKeyCode();    does not work, see notes
    akae2.showStatus("A key is released");
  }
  public void keyTyped(KeyEvent e)
  {
    akae2.keyMessage = "" + e.getKeyChar();
    //  akae2.keyCode = e.getKeyCode();    does not work, see notes
    akae2.repaint();
  }
}

case KeyEvent.VK_F1:

VK_F1 represents an integer number defined in java.awt.event.KeyEvent class that represents F1 functional key. KeyEvent class provides key codes upto F24.

addKeyListener(new HelperClass(this));

The above statement is explained (comes under integration of classes) in Applet: MouseMotionAdapter getX() and getY() Example.

In keyTyped() and keyReleased() methods getKeyCode() does not work and use only getKeyChar(). In these two methods, the getKeyCode() is VK_UNDEFINED.

For numbers it is VK_1 or VK_2 etc. The other virtual keys are VK_INSERT, VK_LESS, VK_RIGHT_PARENTHESIS, VK_WINDOWS etc. For a list of virtual and other keys refer Java API, KeyEvent class.


KeyAdapter Example
Output screen when F3 is pressed KeyAdapter Example

1 thought on “KeyAdapter Example getKeyChar() getKeyCode()”

Leave a Comment

Your email address will not be published.