Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

responding to keypress and keyrelease in an applet

843807Dec 10 2006 — edited Dec 10 2006
so, i have an applet, some objects i made etc, and some html to display it.

i would like my applet to respond to keypress and keyrelease events.
i want the responses to be normal code, "variable= true;" etc, not some swingaction stuff.

now, i have been looking around,

this is the sort of thing i am looking for
public boolean keyDown(Event e, int key) {

	// Check if any cursor keys have been pressed and set flags.

	if (key == Event.LEFT)
	  left = true;
	if (key == Event.RIGHT)
	  right = true;
	if (key == Event.UP)
	  up = true;
	if (key == Event.DOWN)
	  down = true;
	return false;
}
  public boolean keyUp(Event e, int key) {

	// Check if any cursor keys where released and set flags.

	if (key == Event.LEFT)
	  left = false;
	if (key == Event.RIGHT)
	  right = false;
	if (key == Event.UP)
	  up = false;
	if (key == Event.DOWN)
	  down = false;
	return false;
}
the problem is that's all depreciated
http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Event.html
http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Event.html#KEY_PRESS

so, i look around the java site, and its all about key listners and other stuff, mainly about navigating through menus with keypresses.

so does anyone know any nice simple pieces of code that can let me change a variable on keypress or keyrelease or call an objects method, or just normal java code

note, i'm not just looking at named keys like in the above code "LEFT" "RIGHT" etc,
w,a,s,d," " etc are all good to respond to too :)

this seems to be close to what i'm looking for
http://java.sun.com/j2se/1.5.0/docs/api/java/awt/event/KeyEvent.html
http://java.sun.com/j2se/1.5.0/docs/api/java/awt/event/KeyEvent.html#KEY_PRESSED
but i'm not quite sure how to use it

this did have something that looked ok
http://java.sun.com/docs/books/tutorial/uiswing/events/keylistener.html
until it said

//You should only rely on the key char if the event
//is a key typed event.

and i wanted to use keyrelease and keydown, not keytyped , so it wasn't good :(

help will be appreciated :)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 7 2007
Added on Dec 10 2006
1 comment
294 views