• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

KeyListener failing for Macs

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 .
 
Rendelle Fox
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!
 
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
PPS. I just tried a version that catches the A button instead, in case my virtual machine was consuming the F2. Still no luck.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try calling content.requestFocusInWindow(); from inside SwingUtilities#invokeLater()
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
PS. When I remember to call "addFocusListener(content);", I get focus events for Windows, but still nothing for Ubuntu.
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
You guys haven't done this much, have ya? I suggest you study this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic