Passing focus between Processing applet and HTML/JavaScript
in
Integration and Hardware
•
2 years ago
Hi,
I have some problems with passing focus between a Processing applet and the HTML/JavaScript page it is embedded in. My aim is that the different parts obtain focus without the user having to click them.
A demo may be visited at
(
http://www.infochembio.ethz.ch/test/test_navigation/rauminformation.html 2011/03/01 - this link is not valid anymore)
(Code will be published as soon as it is complete and well-documented.)
Code available at
http://www.infochembio.ethz.ch/en/librarynavigator_about.html (2011/07/19)
After initial loading of the page, focus is first on the HTML/JavaScript part. In the navigation frame at the left, floors can be hovered with the mouse and are highlighted accordingly. When one clicks a topic, a service, or simply a floor in the building, parameters are passed by calling an API method of the Processing applet and the applet obtains focus by using the Java requestFocus() method. That it has obtained focus can be seen by hovering over the space objects. Of course, now the navigation frame has lost focus (hover over building floors disabled), and one would have to click outside the applet frame to pass focus back to the navigation frame.
One idea of mine was to use a LiveConnect JSObject window.eval("window.focus();") to pass focus back to the HTML window as soon as the mouse pointer leaves the applet frame. However, there are some problems associated with that:
1. One could use the Java mouse methods as discussed in this thread (
http://processing.org/discourse/yabb2/YaBB.pl?num=1263445642 ), but this is not possible in an applet context because of the standard Java Runtime Environment policies that do not allow to track the mouse position outside the applet. One would obtain a java.security.AccessControlException: access denied (java.awt.AWTPermission watchMousePointer). Of course, I can't control the JRE settings of each individual user who accesses the applet.
2. One could use a small border region to detect mouseout:
- if (mouseX < 1 || mouseY < 1 || mouseX > (applet_width - 1) || mouseY > (applet_height - 1)) {
- try {
- window.eval("window.focus();");
- } catch (JSException e) {
- }
At present, this code is implemented in draw(). However, this is not very reliable if the mouse is moved too fast (since the last draw() frame-cycle had recorded a mouseX/mouseY still in the inner region, so window.eval does not fire). Also it does not distinguish between mouse move-in and mouse move-out.
So here my question: Is there a simple Java method that tells me if the mouse pointer is within the applet frame (I don't need the exact position, which would violate the AWT permission, just in/out or true/false).
1