Don't use the GWT mouse button constants with Elemental2

54 views
Skip to first unread message

Craig Mitchell

unread,
Aug 3, 2025, 9:36:12 PMAug 3
to GWT Users
PSA:  GWT uses old mouse button constants in com.google.gwt.dom.client.NativeEvent:

public static final int BUTTON_LEFT = 1;
public static final int BUTTON_MIDDLE = 4;
public static final int BUTTON_RIGHT = 2;

These are not the correct values to use with Elemental2 elemental2.dom.MouseEvent.button.

JavaScript now uses https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button so these are the correct values:

public static final int BUTTON_LEFT = 0;
public static final int BUTTON_MIDDLE = 1;
public static final int BUTTON_RIGHT = 2;

The keyboard key code constants are also old in GWT, but that one was obvious, as GWT has key codes as integers, while Elemental2 has key codes as strings.

I couldn't find any definitions for these new mouse buttons or key codes.  I still need to have a look at projects like Akasha and Elemento, as hopefully they fix these Elemental2 gotchas.

Jens

unread,
Aug 4, 2025, 4:47:47 AMAug 4
to GWT Users
These constants are expected to be used with NativeEvent.getButton(). GWT's implementation translates 0, 1, 2 to 1, 2, 4. The values 1, 2, 4 had been used by IE.

See DOMImplStandard.eventGetButton(). 

-- J.

Jens

unread,
Aug 4, 2025, 5:17:50 AMAug 4
to GWT Users
The keyboard key code constants are also old in GWT, but that one was obvious, as GWT has key codes as integers, while Elemental2 has key codes as strings.

JS KeyboardEvent has 

- key (number, the physical key on the keyboard)
- code (string, the final character on the keyboard after evaluating locale, keyboard layout and SHIFT / ALT / CTRL)
- keyCode (number, deprecated, the number does not represent the final character)
- charCode (number, deprecated, only available during keyPress event)

GWT uses keyCode for legacy reasons. You should always use code/key. But even if you use code/key you still have to take care if your application is international. A classic one is ALT + "/" as a keyboard shortcut which I cannot trigger at all for example.


-- J.

Craig Mitchell

unread,
Aug 5, 2025, 4:00:36 AMAug 5
to GWT Users
Thanks Jens.  Interesting that elemental2.dom.KeyboardEvent doesn't include keyCode or charCode.  I guess that's a good thing, so you can't use deprecated attributes.
Reply all
Reply to author
Forward
0 new messages