Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

KeyListener not working on some PCs  RSS feed

 
Andreas Beck
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone!
I'm currently writing a JApplet that has to respond to Key Events and Mouse Events. So I implemented a KeyListener and a MouseListener and wrote the method

When I run the program and press a key on the keyboard, the message pops up. When a friend of mine runs the program, nothing happens.
Mouse Events work on my and my friend's pc...

Any suggestions? Maybe it's because I have Java 6 and he has Java 7?
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Insufficient information, What component is the KeyListener added to? Swing is designed to use key bindings, why use a KeyListener? Does your code respect Swing's single threaded rule?

To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem.
 
Andreas Beck
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry. Here's a compilable code:
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also have java 6, and it doesn't work unless you click the applet to give it focus
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My recommendation to use key bindings stands, but if for some silly reason you have to use a key listener, setFocusable(true) and maybe requestFocusInWindow() of the JApplet's contentPane., Should work.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Dunn wrote:I also have java 6, and it doesn't work unless you click the applet to give it focus


This was because Microsoft lost a lawsuit and they had to make Internet Explorer work that way, I think. Maybe the problem is browser-dependent?
 
Andreas Beck
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I finally got the time to test it. setFocusable(true) and requestFocusInWindow() did the trick. For now it works, but sooner or later I'll try using the key bindings.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!