I’m developing a game within a Java application (using a Canvas class to draw).
The problem is, my arrow input from the keyboard is ignored until I click within the drawing
area. Is there a workaround to this? Thanks.
-Phoenix412
I’m developing a game within a Java application (using a Canvas class to draw).
The problem is, my arrow input from the keyboard is ignored until I click within the drawing
area. Is there a workaround to this? Thanks.
-Phoenix412
i had this problem too, i think the trick was:
addListener
validate()
with possibly a requestFocus for good measure if necessary…
I’m not quite sure on what the best practice is, but validate seemed to be the magic word.
Playing around with both right now but it doesn’t appear to work. I think I
may be using it in the wrong place? Here’s my Canvas constructor code, showing my implementation:
validate() and requestFocus() calls are made at the end of the canvas constructor
CvWorld(OverworldFrame master)
{
masterFrame = master;
//Mouse movement listener
addKeyListener(new KeyAdapter()
{ public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode();
if(key == KeyEvent.VK_UP)
up = true;
else if(key == KeyEvent.VK_DOWN)
down = true;
else if(key == KeyEvent.VK_RIGHT)
right = true;
else if(key == KeyEvent.VK_LEFT)
left = true;
try{
}catch (Exception ex){}// why!??
}
public void keyReleased(KeyEvent e)
{
int key = e.getKeyCode();
if(key == KeyEvent.VK_UP)
up = false;
else if(key == KeyEvent.VK_DOWN)
down = false;
else if(key == KeyEvent.VK_RIGHT)
right = false;
else if(key == KeyEvent.VK_LEFT)
left = false;
}
});
up = down = left = right = false;
run_switch = false;
logicalX = logicalY = 0;
...
//initializes and starts Thread
thr = new Thread(this);
thr.start();
validate();
requestFocus();
} // end constructor CvWorld
At first I used just validate, then I added requestFocus along with it.
Neither one appears to work.
This looks like what I need (according to the API), but I think
my usage isn’t right.
-Phoenix412
I had a similar problem fixed by adding addFocusable(true), hope that helps.
What class is that in? I couldn’t find it for canvas and googling “addFocusable” only
gives a few results not related to Java…
zoto probably meant setFocusable(true). However, I think components are focusable by default.
I’d be surprised if the validate() would do the trick for a canvas, they’re primarily intended for containers.
Also the requestFocus() probably won’t work, as it’s mostly intended for actionlistener components (text input, buttons, etc…).
Did you try to add your listeners to the owner frame (OverworldFrame master) instead of the canvas (CvWorld)?
requestFocus() really should do the trick. Just call it every single timestep for good measure.
Well, he said that it didn’t work, so…
No, he did it once at the end of his constructor. Something he does later can easily take the focus away again. That’s what I said all it every single timestep for good measure. That way, no matter what the user clicks on or what happens, you immediately pull focus back to the correct Component.
RTFM ;D
http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html
and specifically :-
http://java.sun.com/javase/7/docs/api/java/awt/Component.html#requestFocusInWindow()
(For those that don’t read the manuals)
Use requestFocusInWindow() instead of requestFocus().
“The focus behavior of this method can be implemented uniformly across platforms…” while the other’s “…focus behavior is platform-dependent”.
If it won’t work in the constructor,
[quote=""]
Also requestFocusInWindow() is meant to be called in the constructor, whereas requestFocus() isn’t.
The combination of:
setFocusable(true);
validate();
requestFocusInWindow();
called at the end of the constructor does the trick.
Thanks for the help!