*
The moose likes Swing / AWT / SWT and the fly likes KeyListener working randomly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "KeyListener working randomly" Watch "KeyListener working randomly" New topic
Author

KeyListener working randomly

Nikos Stavros
Ranch Hand

Joined: Feb 24, 2006
Posts: 243

I have a program that the key listeners only work at random. When I start the program they work sometimes, and sometimes they dont work.

my code is as follows


Jesus lives
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
if it is working randomly, something might be stealing the focus

try adding a focusListener, and in focusLost print a message to indicate that
is the problem.

or, if you are holding down the key, per your last thread, do you have it coded
so the keyEvent is not continuously firing?
Nikos Stavros
Ranch Hand

Joined: Feb 24, 2006
Posts: 243

try adding a focusListener, and in focusLost print a message to indicate that
is the problem.


When the paddles are working I get correct messages with this code

however when I have the problem when I toggle between the game windows and other windows I get not print out statements

so the keyEvent is not continuously firing

I'm not sure how to do this because I would have thought that as long as the key is down KeyEvents would be fired continously
[ November 06, 2006: Message edited by: Nikos Katsikanis ]
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> I'm not sure how to do this because I would have thought that as long as the
> key is down KeyEvents would be fired continously

keyEvents are forwarded to the current focus owner, otherwise you have a simple keyLogger.

if you want something to keep working whilst unfocused, use a timer
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: KeyListener working randomly
 
Similar Threads
Multiple Key Press
Multiplayer Pong
Graphics on Applets
Issue with graphics2d
Problem with multiple keys pressed