• Advertisement

Archived

This topic is now archived and is closed to further replies.

[java] Keyboard event

This topic is 5561 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

My question it this. What it the best way to handle the keyboard in an applet? In the book I was reading (Sam’s Teach Yourself Java 2 in 21 days) they add the functions public boolean keyDown(Event e, int key); and public boolean keyUp(Event e, int key) ; to the applet. This works, but it gives me a deprecated API warning. Thanks for any help

Share this post


Link to post
Share on other sites
Advertisement
You could implement a keylistener instead.
I use something that looks like:

public class KeyBoard implements KeyListener
{
private boolean[] keys = new boolean[256];
public KeyBoard() {}
public void keyPressed(KeyEvent e)
{
int keyCode = e.getKeyCode();
keys[keyCode] = true;
}

public void keyReleased(KeyEvent e)
{
int keyCode = e.getKeyCode();
keys[keyCode] = false;
}

public void keyTyped(KeyEvent e) {} // Do nothing

public boolean isPressed(int keyCode)
{
return keys[keyCode];
}

}

Keyboard kb = new KeyBoard();
addKeyListener(kb);

Then I can se if a key is pressed by using
kb.isPressed(KeyEvent.VK_ESCAPE);

Maybe nog the best way... but there are no errors

Share this post


Link to post
Share on other sites
thanks tordyvel that works good.

However, I have one more question.

In what code set is the key pressed reported?

It appears to hold the ASCII value of the key, but it only reports upper case for characters.

What constant can I use for the arrow keys?

Event.UP / Event.DOWN are in Unicode.
I could just hard code the ASCII values but I don’t know if the would be portable.

Thanks for any help


[edited by - Abob on December 2, 2002 1:37:24 PM]

Share this post


Link to post
Share on other sites
Hi,

look for anything in the class KeyEvent , that is a "final static int" and has a name like VK_*.

Yep, those are virtual key constants. And they are platform independent.

Should work for you, but I''m not sure with pre 1.2 event model.

have fun ...

Petr Stedry

Share this post


Link to post
Share on other sites
Also, if you don't feel like implementing all of the KeyListener methods or your key event handling is complicated enough that you want to put it in its own file, you can extend KeyAdapter and pass that to the addKeyListener method of a component.

EDIT: Didn't think of it, but a class that implements KeyListener can be kept seperate from the rest of a program too... =)

This isn''t life in the fast lane, it''s life in the oncoming traffic.
-- Terry Pratchett

[edited by - Kentaro on December 3, 2002 5:27:49 PM]

Share this post


Link to post
Share on other sites
don''t worry about ''deprecated'' events... unless you are trying to
write specifically for jdk 1.2.2., etc, it''s not a big deal. It''s just sun
saying that they *may* not be supporting that feature in future
versions of jdks.

Share this post


Link to post
Share on other sites
quote:
Original post by johnnyboy
don''t worry about ''deprecated'' events... unless you are trying to
write specifically for jdk 1.2.2., etc, it''s not a big deal. It''s just sun
saying that they *may* not be supporting that feature in future
versions of jdks.


Yeah, except if you are just learning, you might as well learn to do it the right way the first time.



Make it work, then
make it fast.

"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book

Share this post


Link to post
Share on other sites

  • Advertisement