I am having a problem with a java game I have made, where some users are reporting that they can't play because the game won't recognize keypresses. It seems that the users who are reporting this are exclusively using either Mac or, once, Ubuntu. The game runs on both desktop and as an applet but it's only available right now as an applet.
I have tested the game with four different browsers (Chrome, Firefox, Safari, IE) from Windows 7 and the first two from Windows XP. I cannot replicate the problem because I do not have a Mac to test from (although it just occurred to me that with some effort, I may be able to secure a Linux machine). At any rate, I cannot provide all of my code (it's pretty large anyway, I doubt anyone would want to comb through it) and it's rather difficult to "dumb down" the problem because as I said, I cannot replicate it myself. I will try, but in the meantime I'd really just like to put out feelers for whether anybody has encountered this sort of thing before? (So please, no insisting that you can't help me without my posting code. Right now, I'm just hoping there's somebody who doesn't need the code; who reads this and says, "That happened to me!" I will provide code as soon as I am able.)
To reiterate, the problem appears to be that the KeyListener, which works fine on desktop & every browser I can test it on from Windows, fails when run as an applet from a Mac, on at least Firefox and Chrome, presumably Safari too. It has not been tested as an application on a Mac. At least one person has reported it failing from Ubuntu.
(If it helps, I have MouseListeners all over the place that *are* working, including in the class whose KeyListener is failing).
> I find some problems with my applet, and Mac OSX. The applet is trying
> to use the java.awt.FocusTraversalPolicy.java, but faults.
FocusTraversalPolicy exists since Java 1.4.x. Most browsers on Mac OS X
still use Java 1.3.x.
So if that's what's happening, how might I fix it?
This works in Windows, but not Ubuntu, and probably not OSX although I haven't confirmed that. I am beginning to think that I need something in my HTML, not the Java - especially since if the applet is larger than the window, arrow keys in Windows are caught by the KeyListener but in Ubuntu, they cause the page to scroll. The keys are therefore probably not being passed by the browser to the applet at all. Here's what I've got for that:
The onload doesn't seem to be doing much (it's certainly not necessary in Windows) but it seems a step in the right direction. Note however that if you leave that there without any text (like where I have "Applet.") or other object, it causes the script to not stop, in Ubuntu. Also note that the onload code works just fine in both OSes for giving the focus to a text input box.
I also tried using the new Object tag but I must be using it wrong or something, because it doesn't do anything for me in Windows. But unless that's actually my problem, I'll stick with Applet.
Thanks for any help you can give.
I'm not sure where in the code that should be added, I would guess at the end of the init() method. I know that when using a frame that statement needs to be invoked on a visible GUI.
However, the better approach is to use Key Bindings then you don't have to worry about these problems.
Still works great in Windows (testing in Chrome) but not Ubuntu (testing in Firefox).
PS. I know that I am using the most recent version of Java with the Ubuntu machine because I installed it myself yesterday.
Add a FocusListener to the panel to verify if it ever gets/loses focus. If the panel doesn't have focus when the applet starts then try using the tab key, the panel should gain focus and you can confirm if this is the problem.
Also, the Key Binding can be done on the root pane if for some reason you can't get the panel to have focus.
(listener in applet)
(listener in panel)
Using the RootPane for the Input/Action Maps actually causes it to stop working in Windows.
Sorry, it looks like I gave you the wrong link. I usually send people to my blog entry on Key Bindings. From the example there you will see that you need to use a different InputMap when using the root pane.
I don't user Ubuntu, so I don't know why the panel doesn't get focus.