2

In my self-signed applet (that may possibly become a CA-signed applet in near future) in which I have a fairly complex Swing GUI, I need to detect whenever the user has the SHIFT- or CONTROL-key pressed. I am using this in combination with component-related MouseEvents and MouseMotionEvents to do special mouse-event processing at any time. Shortly:

When I recieve a mouse-motion-event on a distinct component I need to detect whether CONTROL or SHIFT is being held down (pressed). The problem is, that a user will press down shift/control BEFORE he/she moves the mouse into the component of interest. This is my real "usability" problem!

Prior to Java 7 update 21 I was able to accomplish that using a JVM-global AWTKeyListener which I added to the default Toolkit. This is no longer allowed (due to security requirements of Java 7 update 21 - and this in despite of, that I am JNLP-launching my applet in a distinct and separate JVM, but that's another talk).

So what do I have:

I have tried to register a KeyListener with the "main" panel of my applet, but key-events seem to be "swallowed" (by whatever component in my GUI which is having the keyboad focus).

Then I have tried using key-bindings for my main-panel (the top-level container), adding bindings for "shift", "control" and "escape". The only binding working is for "escape", and it always hits my "released" action. "Shift" and "control" are never hit (notified). I am using the WHEN_IN_FOCUSED_WINDOW input-map of my main-panel.

I need to detect keyboard events application-wide (and NOT JVM-wide). How can I accomplish that? Is it possible to "intercept" keyboard-events at some level in my component-hierarchy by other means?

I don't mind overriding Swing/AWT methods in order to accomplish it - I am just desparete to find a solution.

I appreciate any suggestions, as I will otherwise discourage customers from updating Java to 7 upd 21. Thanks!

Aron

PS: Why is there no support for "application"-global resources in Java. It goes for Authenticators, Keylisteners, Default Locale etc..? It is always on a JVM-global basis, despite we (as applet developers) only need it at application level.

1 Answers1

0

You can use the methods on the event you're passed to determine the current state of the modifier keys. event.isControlDown(), for example, will tell you if the control key was still pressed when the mouse event in question occurred. Likewise, you can call event.isShiftDown() for the shift key.

Richard Wilkes
  • 309
  • 2
  • 10