Practical Exercise 12 – Keyboard events

The purpose of this practical is to learn how to use the keyboard to interact with the program.

Methods used by java.awt.event.KeyEvent include:

Method / Use
getKeyCode() / gives the key number (virtual key code) for keyPressed and keyReleased events.
VK_name / constant with the key number for the key name eg VK_space, VK_M, VK_F3, VK_8 correspond to a space, M, function key F3 and 8 key.
getKeyText(n) / translates the key number n to text
getKeyChar() / gives a generated character of type char for keyTyped events eg if shift and a were pressed you would get the resulting character A
isAltDown() / shows if the user held down the alt key - returns a Boolean value
isControlDown() / shows if the user held down the control key - returns a Boolean value
isShiftDown() / shows if the user held down the shift key - returns a Boolean value

Note

keyPressed is fired whenever any key press occurs. keyTyped is fired when a key is pressed that can be converted into a unicode character. If the shift key is down, for example, pressing a will tell keyTyped that you typed a capital A, and keyPressed will just get the a key, without capital or lowercase designations. You cannot call event.getKeyChar() from keyPressed, because there is no key char associated with the events. Characters only come from keyTyped.The basic idea is that keyTyped is used to find characters that are typed, and keyPressed is used for obtain raw key presses.

From http://stackoverflow.com/questions/17797231/keypressed-and-keytyped-confusion

C Standard

Copy the following program, get it working, then modify it in some interesting way.

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

public class KeyboardEvents extends Applet implements KeyListener {

int number=0; //count number of keys pressed

char keyChar=' '; //character of key pressed

String stringKP="",stringAK="",stringKR="",stringX="";

public void init() {

this.requestFocus();

addKeyListener(this);

setSize(300,200);

}

public void paint(Graphics g) {

g.drawString("Number of keys pressed is: "+number, 20, 20);

g.drawString("Character pressed: "+keyChar, 20, 40);

g.drawString("Key pressed is: "+stringKP, 20, 60);

g.drawString("Key released is: "+stringKR, 20, 80);

g.drawString("Action key pressed is: "+stringAK, 20, 100);

g.drawString("stringX: "+stringX, 20, 120);

}

public void keyPressed(KeyEvent e) {

number++;;

stringKP=e.getKeyText(e.getKeyCode());

if (e.getKeyCode()==e.VK_UP)

{

stringAK="Up Key";

}

repaint();

}

public void keyReleased(KeyEvent e) {

stringKR=e.getKeyText(e.getKeyCode());

repaint();

}

public void keyTyped(KeyEvent e) {

keyChar=e.getKeyChar();

if(keyChar=='x')

{

stringX="The lower case x was pressed!";

}

repaint();

}

}

B Standard

Use the arrow keys to move an image or simple drawing around the screen.

Hint: Use VK_UP and VK_DOWN to update the y-coordinate and VK_LEFT and VK_RIGHT to update the x-coordinate.

A Standard

Create a simple keyboard-operated media player (or media viewer).

For example, when you press “P”, start playing a sound file, and change the colour of the button to show it has been chosen. (have another picture of a different colour and/or use setVisible?)

You could base this on the “Images and Sounds” prac or even “House with Methods” if you’re not sick of it yet.

Hobart College 2015, based on Rosny College 2009 Page 1 of 2