views:

130

answers:

4

Im am making an Java SWT program that is required to run on both Linux and Windows.

I use the following Code to listen for KeyUp events:

Control.addListener(SWT.KeyUp, new Listener() {

public void handleEvent(Event arg0) {
 System.out.println("Event");

}

});

But this does not trigger when no control has focus.

Do anyone know of a place i can add a listener that acts as a Catch-all?

A: 

There is a GetAsyncKeyState in User32 that you can access through PInvoke or use the Managed Windows API wrapper for it - KeyboardKey retrieves keyboard data.

That gives you a way of accessing keyboard activity regardless of what currently has focus. It's low level and requires a bit of extra work to tie it into a regular event system, but it's great for stuff like hotkeys where you want to pick up information from another window and use it in your application.

glenatron
Unfortunately my program is required to run on both Linux and Windows.I am searching for an SWT solution to this problem.
JesperGJensen
Sorry, not paying enough attention, clearly. Tell you what, though, that answer would be spot on if you were working with C# :)I'll be interested to see how easy it is to do cross platform, given how tricky it can be using tools that should be windows native...
glenatron
A: 

try following method in Display class:

public void addListener ( int eventType, Listener listener ) 
Santhosh Kumar T
Doesn't work. I tried adding to Shell as well.
JesperGJensen
A: 

I have not been able to find a solution to this. I suspect none exist

JesperGJensen
A: 

The only way of doing this that I'm aware of is by placing a filter on the Display. Take note that multiple Shells may operate on one Display, so you should be careful!

shell.getDisplay().addFilter(SWT.KeyDown, new Listener() {
    public void handleEvent(final Event event) {
        System.out.println(event);
    }
});
Paul Lammertsma