Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 24282

Summary: Global keybindings without menu presence
Product: [Eclipse Project] Platform Reporter: Christopher Dodd <cmdodd>
Component: SWTAssignee: Platform-SWT-Inbox <platform-swt-inbox>
Status: RESOLVED DUPLICATE QA Contact:
Severity: enhancement    
Priority: P3 CC: stori
Version: 2.0.1   
Target Milestone: ---   
Hardware: All   
OS: All   
Whiteboard:

Description Christopher Dodd CLA 2002-10-01 15:11:30 EDT
I would like to be able to listen for all keyevents in an application (not just 
the control with focus) or register Actions with accelerators that do not 
require a menu presence.
Comment 1 Steve Northover CLA 2002-10-02 08:33:06 EDT
So would I.

It turns out that this is a very tricky thing to do portably and SWT does not 
have a good solution because there really is no good portable solution.

One thing that will kind of work is to listen for all key and traversal events 
by hooking a key and traversal listener to every widget in the shell.  If the 
shell has a menu, do not use MenuItem.setAccelerator().  The problem with this 
approach is that you'll get every key all right but so will the controls in the 
shell.  So, if you look for 'A' in your key handler and focus is in a list 
widget, the 'A' will go to the list widget, causing it to scroll to an item 
that begins with the letter 'A' and your key handler will run.  A further 
problem is that looking for 'Ctrl+<key>' in a key handler is danagerous on 
international platforms (on a German keyboard, a '@' is entered by holding down 
an special key that generates Ctrl+Alt so that an application that only tests 
the stateMask could do the wrong thing.

So, there isn't a good solution to this problem.  Fortunately, it's possible to 
build many applications that don't need this kind of fine grain control over 
key strokes.

I'm linking your PR to the work that Eclipse is doing in this area so you'll be 
able to track what they are doing.

*** This bug has been marked as a duplicate of 5682 ***
Comment 2 Christopher Dodd CLA 2002-10-02 10:26:52 EDT
Thanks for the info.
I already tried the trick of adding a KeyListener to all controls, but this did 
not work for me in the case where I had push Buttons with mnemonics.  If the 
button had a mnemonic of Alt-A it would activate and take the focus when Alt-A 
is pressed, all good. But then if the button has focus and 'A' is pressed the 
button activates again and I get no key event. Worse still if there are other 
buttons in the same Composite also with mnemonics they no longer require the 
Alt modifier either.  I could see how this would be nice is most cases, but it 
is not something that works for my application.