Strange issue with Key Listener and Applets
I've been very puzzled about this for several hours trying to find out what is going on and I'm at a loss on this one.
For some reason any time I use a Key Listener in a Java Applet the key listener won't function but it works fine in a JFrame.
To top this off an asteroids game I made a while back has stopped working because of this and all the code in it is fine and unchanged. This strange issue just popped up and I'm not sure how long this has been going on since I just noticed it when trying to use an applet with a key listener in it.
I'm not getting any errors and I have no leads on what is causing this either. Can someone help?
Re: Strange issue with Key Listener and Applets
Be sure the component that has the KeyListener registered has focus. An alternative is to use KeyBindings
How to Use Key Bindings (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)
If this doesn't help, post an SSCCE that demonstrates the problem
Re: Strange issue with Key Listener and Applets
Thanks I'll look.
Any ideas on what could cause Key Listeners to suddenly stop working like this?
Re: Strange issue with Key Listener and Applets
Quote:
what could cause Key Listeners to suddenly stop working
You changed the code.
Normally programs continue working the same way if they are not changed.
Look at the changes you made.
Re: Strange issue with Key Listener and Applets
Found out what it was. For some reason I have to click on the applet before it'll let me do anything with the listeners. :o
Re: Strange issue with Key Listener and Applets
Look at the component class. It has methods for getting the focus.
Re: Strange issue with Key Listener and Applets
Quote:
Any ideas on what could cause Key Listeners to suddenly stop working like this?
Applets run within a web browser, and how that web browser interprets focus can change depending upon the browser (both brand and version - in other words, update your browser and focus can change). This could potentially happen regardless of how many times you call request focus from the applet. If you need to change your code, then I'd again recommend trying key bindings. If not, there are ways to use javascript to try and give the applet focus.