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

Bug 311775

Summary: [widgets] setAccelerator() fails to distinguish shifted and non-shifted for certain characters
Product: [Eclipse Project] Platform Reporter: Doug M <eclipse>
Component: SWTAssignee: Scott Kovatch <skovatch>
Status: RESOLVED NOT_ECLIPSE QA Contact: Silenio Quarti <Silenio_Quarti>
Severity: normal    
Priority: P3 CC: skovatch
Version: 4.0   
Target Milestone: ---   
Hardware: Macintosh   
OS: Mac OS X   
Whiteboard:
Attachments:
Description Flags
Fix none

Description Doug M CLA 2010-05-05 14:25:56 EDT
Build Identifier: swt-I20100426-0852-cocoa-macosx-x86_64

Attempting to specify cmd-minus as the key for a "zoom out" menu item. Using
item.setAccelerator(SWT.MOD1 | '-')

shows the correct command glyphs in the menu item. However, it responds to BOTH cmd-minus and cmd-shift-minus (that's cmd-underline).

It should respond only to cmd-minus.

If I try different base keys, the shifted and non-shifted versions are correctly distinguished. For example,

item.setAccelerator(SWT.MOD1 | 'd');
responds only to cmd-d, and

item.setAccelerator(SWT.SHIFT | SWT.MOD1 | 'd');
responds only to cmd-shift-d, both showing correct glyphs in the menu item.

I tried SWT.KEYCODE_BIT with the actual keycode, but that also responds to both cmd-minus and cmd-underline.

Reproducible: Always

Steps to Reproduce:
1.Load Snippet29
2.Substitute the accelerators cited above.
3.Observe results in Output window.
Comment 1 Scott Kovatch CLA 2010-05-06 19:33:37 EDT
Taking this, but may not make it for 3.6.
Comment 2 Scott Kovatch CLA 2010-05-10 13:18:54 EDT
Curiously, if you have two separate menu items, with one that specifies cmd-'-' and the other cmd-shift-'-' they work correctly. And, cmd-shift-'-' by itself works as expected. But cmd-'-' alone responds to cmd-'-' and cmd-shift-'-'.

AWT does not have this problem, which I find odd. The accelerator handling is pretty much identical.
Comment 3 Scott Kovatch CLA 2010-05-10 15:37:56 EDT
Created attachment 167795 [details]
Fix

There are situations where command-<char> and command-shift-<char> map to the same menu accelerator (mainly when there's no clear lowercase/uppercase mapping). Guard against that by seeing what accelerators were pressed to invoke the action and if they don't match the accelerator mask, ignore it.
Comment 4 Scott Kovatch CLA 2010-05-10 15:39:52 EDT
(In reply to comment #2)
> AWT does not have this problem, which I find odd. The accelerator handling is
> pretty much identical.

Ignore this comment -- AWT looks at the key event and determines it is a menu key equivalent before the OS gets a chance. Cocoa never gets involved.
Comment 5 Scott Kovatch CLA 2010-05-10 15:40:11 EDT
Ready for review.
Comment 6 Doug M CLA 2010-05-10 18:37:59 EDT
(In reply to comment #5)
> Ready for review.

(In reply to comment #3)
> Created an attachment (id=167795) [details]
> Fix
> 
> There are situations where command-<char> and command-shift-<char> map to the
> same menu accelerator (mainly when there's no clear lowercase/uppercase
> mapping). Guard against that by seeing what accelerators were pressed to invoke
> the action and if they don't match the accelerator mask, ignore it.

Thanks for the fix.

Are you saying that Cocoa itself does not distinguish the two accelerators unless the shifted version is defined?
Comment 7 Scott Kovatch CLA 2010-05-10 19:02:27 EDT
(In reply to comment #6)

> Are you saying that Cocoa itself does not distinguish the two accelerators
> unless the shifted version is defined?

Only in cases where the accelerator character doesn't have a lowercase equivalent.  For example, if you specify Cmd-'D' as the modifier mask and character, respectively, Cocoa considers that and Cmd-Shift-'d' as equivalent. (note capital vs. lowercase)

In your example, the lowercase of '-' is also '-', so cmd-'-' and cmd-shift-'-' are identical if you only have cmd-'-' in the menu. But if each one is in its own menu item Cocoa will map the key combo and accelerator to the right menu item.
Comment 8 Silenio Quarti CLA 2010-05-11 17:31:09 EDT
This problem only happens on Snow Leopard. Current code works fine in Leopard. It looks like a bug introduced by Apple. We should consider putting a version check if we decide to workaround it. It might be better to open a radar bug instead.

The fix has one problem. 

1) Run snippet
2) Pulldown menu using mouse
3) Use arrow key to select menu item
4) Press and hold Ctrl key
5) Select menu item by pressing Return key

You get no selection event. The problem will happen any time the current event is a key event, but the menu item is not selected by typing the accelerator key.
Comment 9 Scott Kovatch CLA 2010-05-11 19:21:35 EDT
(In reply to comment #8)
> This problem only happens on Snow Leopard. Current code works fine in Leopard.
> It looks like a bug introduced by Apple. We should consider putting a version
> check if we decide to workaround it. It might be better to open a radar bug
> instead.

Sigh. Indeed it is. Given that this is 10.6-only and that having both cmd-shift-'-' and cmd-'-' in the menu work properly I think we should mark this as NOT_ECLIPSE. It's not work working around it.

I'll file a bug with Apple.