Capture all events in JFrame without deactivating the window

event-handling, events, java, swing

Solution

1.) JNA comes with an example that does almost what you want:

http://java.net/projects/jna/sources/svn/content/trunk/jnalib/contrib/w32keyhook/KeyHook.java

In order to block a key, just return 1 instead of calling `CallNextHookEx` - cf. the MSDN documenttaion.

2.) JNativeHook allows you to hook into global event processing, but right now there is no way of stopping events from being deliverd - e.g. the Windows key will still activate the start menu. However it is still worth looking at since it comes with much less overhead, and you can modify it (starting with `CallNextHookEx` here) to behave the way you want. It is licensend under the GPL though.

3.) Another clean way of doing this, would be to switch to SWT and use the SWT Win32 Extensions to intercept keyboard events.

Problem

I'm trying to develop something like a remote desktop / VNC client. It's necessary for me to capture all events in the client window. The method I'm using is to override the `processEvent` method of the `JFrame`: ``` @Override protected void processEvent(AWTEvent e) { ... } ``` However on events like the `Windows` key or `Alt+Tab` the window is getting deactivated: ``` ... 00000191 KEY_PRESSED,keyCode=524,keyText=Windows,keyChar=Undefined keyChar,keyLocation=KEY_LOCATION_LEFT,rawCode=91,primaryLevelUnicode=0,scancode=91,extendedKeyCode=0x20c 00000192 KEY_RELEASED,keyCode=524,keyText=Windows,keyChar=Undefined keyChar,keyLocation=KEY_LOCATION_LEFT,rawCode=91,primaryLevelUnicode=0,scancode=91,extendedKeyCode=0x20c 000000ce WINDOW_DEACTIVATED,opposite=null,oldState=0,newState=0 ... ``` How do I keep the window active on such events? I would prefer a pure Java solution to this. If there is no pure java solution, can someone point me towards a JNA solution (or any other solution for that fact)? EDIT1: * Resolved ambiguous term 'focus' to window deactivation * Emphasized that non pure Java solutions are acceptible

Original source

Related problems