aspose file tools*
The moose likes Applets and the fly likes KeyListener failing for Macs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "KeyListener failing for Macs" Watch "KeyListener failing for Macs" New topic
Author

KeyListener failing for Macs

Rendelle Fox
Greenhorn

Joined: Nov 07, 2010
Posts: 20
Hi all,

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).
Rendelle Fox
Greenhorn

Joined: Nov 07, 2010
Posts: 20
Right, and I was about to post a reply saying that I am indeed using setFocusable, when it occurred to me to try searching by that term + Mac. I found this interesting tidbit from http://lists.apple.com/archives/java-dev/2005/Feb/msg00518.html:

> 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?
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

As long as you have about 1 GByte of memory and about 20 GByte of spare disk space it is very easy to set up a Linux system using Oracle's VirtualBox. See http://www.virtualbox.org/ and http://www.ubuntu.com/desktop/get-ubuntu/download .


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Rendelle Fox
Greenhorn

Joined: Nov 07, 2010
Posts: 20
Wow, that looks fantastic. About the most helpful answer I could have gotten, probably, given how little information I could give. Tonight, I will try installing that on my home machine, and between the Mac OS X Server and Linux distros, I will hopefully be able to isolate the problem. (Right, that's the phrase I was looking for last night. Dumb down, indeed.)

Thanks!!
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Rendelle Fox wrote:FocusTraversalPolicy exists since Java 1.4.x. Most browsers on Mac OS X still use Java 1.3.x.

That was in 2005. These days, Java 1.3 is extinct on Macs, as is 1.4 for all practical purposes. So, whatever is causing the problem, that's not it.
Rendelle Fox
Greenhorn

Joined: Nov 07, 2010
Posts: 20
All right, I have it simplified down to 30 lines of code:

GameApplet.java


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.
Rendelle Fox
Greenhorn

Joined: Nov 07, 2010
Posts: 20
Oh, and compile.bat which I use to compile:



Manifest.txt


These two files sit in one directory with a src directory that contains GameApplet.java. On my server, journey.jar and test.htm sit in the same directory.

Thanks again!
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
Just because the component is focusable doesn't mean it has focus. KeyEvents are only dispatched to the component with focus. Try adding:



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.
Rendelle Fox
Greenhorn

Joined: Nov 07, 2010
Posts: 20
Hm. Both of those suggestions sounded great to me, but neither of them seems to have worked. Here is my code now:



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.
Rendelle Fox
Greenhorn

Joined: Nov 07, 2010
Posts: 20
PPS. I just tried a version that catches the A button instead, in case my virtual machine was consuming the F2. Still no luck.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10535
    
    9

Try calling content.requestFocusInWindow(); from inside SwingUtilities#invokeLater()


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
The problem appears to be that your component doesn't have focus. I don't know why. The solutions won't work unless the component has focus.

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.
Rendelle Fox
Greenhorn

Joined: Nov 07, 2010
Posts: 20
All excellent ideas - but nothing is working. Using the RootPane for the Input/Action Maps actually causes it to stop working in Windows. The FocusListener is reporting no focus being achieved or lost, however I don't trust that since it doesn't report it for Windows either, when I deliberately tab away and, say, type in the address bar. I tried the FocusListener both in the JApplet and in the JPanel.

More code:

(listener in applet)


(listener in panel)
Rendelle Fox
Greenhorn

Joined: Nov 07, 2010
Posts: 20
PS. When I remember to call "addFocusListener(content);", I get focus events for Windows, but still nothing for Ubuntu.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
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.



should be:



I don't user Ubuntu, so I don't know why the panel doesn't get focus.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: KeyListener failing for Macs