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.
> 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
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
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.
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)
{
}
}
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--;
}
}
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() ?
> > 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
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.
> 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.).