PDA

Click to See Complete Forum and Search --> : input in an applet


SteveCRM
Jan 5th, 2004, 05:54 PM
hey hey its final project time in my java class :)

im making a simple game (kind of like a 1 player slimeball), but Im not sure how to get keyboard input in an applet. I know how to get it in normal console, but not too sure about applets. Thats all I need, thanks all! :cool:

Dillinger4
Jan 6th, 2004, 01:09 AM
import java.awt.event.*;

class B extends KeyAdapter{

public void keyPressed(KeyEvent ke){
System.out.println(ke.getKeyChar());
}
}


Here is a short description of the KeyEvent class.

"This event is generated when the user presses or releases a key, or does bolth (ie types a character) These situations are characterized by the following constants in the KeyEvent class"

public static int KEY_PRESSED
public static int KEY_RELEASED
public static int KEY_TYPED

The last one is what you want.

"This event is delivered when a character has been typed on the keyboard ie.... a key has been pressed (KEY_PRESSED) and then released (KEY_RELEASED) to signify typing a character.

There are two methods that are defined in the KeyEvent class.....

int getkeyCode()

For KEY_PRESSED or KEY_RELEASED events, this method can be used to get the integer key-code associated with the key. The key codes are defined as constants in the KeyEvent.

char getKeyChar()

For KEY_TYPED events, the method can be used to get the Unicode character theat results from hitting a key.

If you want you can also (which i think is cool) log the time the user pressed the key. By using the getWhen() method inherited from the parent class InputEvent