Key Listener

i read the tutorial on the website about writing the key listener program, and some what understand it. buy how can i apply it in my program? I know it may be a stupid question but I'm still a student.
[209 byte] By [bav_man13a] at [2007-11-26 13:41:45]
# 1
What do you need a KeyListener for? It's a low level event catcher andmost of the time you don't need it.kind regards,Jos
JosAHa at 2007-7-7 23:58:39 > top of Java-index,Java Essentials,Java Programming...
# 2
i need something that detects arrow key movement, i dont know if theres something for that.
bav_man13a at 2007-7-7 23:58:39 > top of Java-index,Java Essentials,Java Programming...
# 3

If this is a Swing question it would be much better off in the Swing forum. People who answer there have a good grasp of what they are talking about. But I will give it a try.

The arrow keys are already handled by most components, so you don't get to see them. I am guessing that when you say "arrow key movement" you mean you want to know where in a text component the cursor is, and not just that the keys are being pressed. There are better ways to find out where the cursor is than trying to duplicate what the text component does with the arrow keys.

And as I said, the people in the Swing forum give better advice. Only when you post your question there, a little more background would be helpful. Like for example what you are actually trying to do, rather than asking about a proposed solution without explaining the problem.

DrClapa at 2007-7-7 23:58:39 > top of Java-index,Java Essentials,Java Programming...
# 4

> i need something that detects arrow key movement, i

> dont know if theres something for that.

Yes, a KeyListener can do that for you. Craft a class that implements

the KeyListener interface. If the 'getKeyCode()' method of the KeyEvent

(the parameter passed to one of your KeyListener methods) returns

VK_UP or VK_DOWN etc. your method should do whatever it is supposed

to do. Register your KeyListener implementation to whatever Component

you like and you're in business.

kind regards,

Jos

JosAHa at 2007-7-7 23:58:39 > top of Java-index,Java Essentials,Java Programming...
# 5
what does it mean when i get this.... is not abstract and does not override abstract method keyReleased(java.awt.event.KeyEvent) in java.awt.event.KeyListenerpublic class .... implements KeyListener
bav_man13a at 2007-7-7 23:58:39 > top of Java-index,Java Essentials,Java Programming...
# 6
http://java.sun.com/docs/books/tutorial/java/IandI/usinginterface.html
es5f2000a at 2007-7-7 23:58:39 > top of Java-index,Java Essentials,Java Programming...
# 7

KeyListener is an interface, and you need to implement all three methods that it provides, one of them being keyReleased as indicated by the compile error.

> what does it mean when i get this

>

>

> .... is not abstract and does not override abstract

> method keyReleased(java.awt.event.KeyEvent) in

> java.awt.event.KeyListener

> public class .... implements KeyListener

youaresofakingwetoddeda at 2007-7-7 23:58:39 > top of Java-index,Java Essentials,Java Programming...
# 8

Im still very confused about this...

I've been referring to :

http://java.sun.com/docs/books/tutorial/uiswing/events/examples/KeyEventDemo.java

for help, but i still cant get it.

What i basically want to do is:

if (//right arrow is pressed)

x++;

else if (//left arrow is pressed)

x--;

if some one could help me i would appreciate it.

thanks.

bav_man13a at 2007-7-7 23:58:39 > top of Java-index,Java Essentials,Java Programming...
# 9
how are you currently using KeyListener? Do you have a class that implements it? Show us what you have and please use the code tags so the code will be readable.
youaresofakingwetoddeda at 2007-7-7 23:58:39 > top of Java-index,Java Essentials,Java Programming...
# 10

public class KeyStroke implements KeyListener

{

public void keyTyped(KeyEvent e)

{

}

public void keyPressed(KeyEvent e)

{

//i got these 3 from the "How to

//write a key listener", but dont

//know what to put in them

}

public void keyReleased(KeyEvent e)

{

}

public static void main (String[] args)

{

}

}

bav_man13a at 2007-7-7 23:58:39 > top of Java-index,Java Essentials,Java Programming...
# 11

Well, you could try just leaving keyTyped and keyReleased empty and put the following code inside keyPressed method

public void keyPressed(KeyEvent e) {

if (e.getSource() == KeyEvent.VK_LEFT) {

x--;

} else if (e.getSource() == KeyEvent.VK_RIGHT) {

x++;

} else if (e.getSource() == KeyEvent.VK_UP) {

y++;

} else if (e.getSource() == KeyEvent.VK_DOWN) {

y--;

}

}

youaresofakingwetoddeda at 2007-7-7 23:58:39 > top of Java-index,Java Essentials,Java Programming...
# 12
it kept giving me "incomparable types: java.lang.Object and int" 4 times
bav_man13a at 2007-7-7 23:58:39 > top of Java-index,Java Essentials,Java Programming...
# 13
> it kept giving me "incomparable types:> java.lang.Object and int" 4 timesIt's becuase youaresofakingwetodded gave you the wrong methods to use. Substitute all the getSource() with getKeyCode().
CaptainMorgan08a at 2007-7-7 23:58:39 > top of Java-index,Java Essentials,Java Programming...
# 14

ok my last question..hopefully

this is what i have so far

public class KeyStroke implements KeyListener

{

public static int x , y;

public void keyTyped(KeyEvent e)

{

if (e.getKeyCode() == KeyEvent.VK_LEFT)

{

x--;

} else if (e.getKeyCode() == KeyEvent.VK_RIGHT)

{

x++;

} else if (e.getKeyCode() == KeyEvent.VK_UP)

{

y++;

} else if (e.getKeyCode() == KeyEvent.VK_DOWN)

{

y--;

}

}

public void keyPressed(KeyEvent e)

{

}

public void keyReleased(KeyEvent e)

{

}

public static void main (String[] args)

{

x = 0;

y = 0;

while (x < 10)

{

keyTyped();

}

}

}

what do i put in for keyTyped() ?

bav_man13a at 2007-7-7 23:58:39 > top of Java-index,Java Essentials,Java Programming...
# 15
> what do i put in for keyTyped() ? You said you read the tutorial on "How to Use Key Listeners".So start with that program and modify it to do what you want.
camickra at 2007-7-21 15:54:09 > top of Java-index,Java Essentials,Java Programming...
# 16

> > it kept giving me "incomparable types:

> > java.lang.Object and int" 4 times

>

> It's becuase youaresofakingwetodded gave you the

> wrong methods to use. Substitute all the getSource()

> with getKeyCode().

oops you're right, i meant getKeyCode

youaresofakingwetoddeda at 2007-7-21 15:54:09 > top of Java-index,Java Essentials,Java Programming...
# 17

KeyStroke implementing KeyListener needs to be added to a Container. So your main method should be in another class that extends perhaps...say JFrame

and it will listen to key strokes hence the name KeyListener. and keyTyped will be called when it a key is typed/pressed and the code in the method will check if it's the arrow keys that are pressed and increment/decrement x and y values.

you won't have to call keyTyped yourself.

youaresofakingwetoddeda at 2007-7-21 15:54:09 > top of Java-index,Java Essentials,Java Programming...
# 18

> and keyTyped will be called when it a

> key is typed/pressed...

From the API (for a better description):

"Key typed" events are higher-level and generally do not depend on the platform or keyboard layout. They are generated when a Unicode character is entered, and are the preferred way to find out about character input. In the simplest case, a key typed event is produced by a single key press (e.g., 'a'). Often, however, characters are produced by series of key presses (e.g., 'shift' + 'a'), and the mapping from key pressed events to key typed events may be many-to-one or many-to-many. Key releases are not usually necessary to generate a key typed event, but there are some cases where the key typed event is not generated until a key is released (e.g., entering ASCII sequences via the Alt-Numpad method in Windows). No key typed events are generated for keys that don't generate Unicode characters (e.g., action keys, modifier keys, etc.).

CaptainMorgan08a at 2007-7-21 15:54:09 > top of Java-index,Java Essentials,Java Programming...