You could try a lower level, grabbing the key down and key up events.  
Something like this:

RootPanel.get().addDomHandler(event -> {
handleKeyEvent(event.getNativeKeyCode(), true);
event.preventDefault();
event.stopPropagation();
}, KeyDownEvent.getType());

RootPanel.get().addDomHandler(event -> {
handleKeyEvent(event.getNativeKeyCode(), false);
event.preventDefault();
event.stopPropagation();
}, KeyUpEvent.getType());

private void handleKeyEvent(int keyCode, boolean isDown) {
if (keyCode == KeyCodes.KEY_ALT) {
altKeyDown = isDown;
}
}

On Thursday, 12 December 2024 at 10:18:49 am UTC+11 Jens wrote:

> Usually you want to use the "key" property of the event as it is 
> independent of the physical location of the key. It contains the printable 
> character if the keyboard key produces one. If it doesn't it contains a 
> description like "shift".
>
> Depending on how international you have to be, you might also want to 
> think about keyboards that have two layouts: one with latin characters and 
> one with non-latin characters (arabic, ..). These keyboards usually have a 
> key to switch between both layouts and the layout with latin characters 
> usually matches the standard English keyboard layout, but it doesn't have 
> to. If you don't want to force your users to switch to latin mode before 
> using a keyboard shortcut you would need to detect if the "key" property 
> contains a non-latin character and if it does you should check "code" 
> instead of "key" property. You would use the "code" of the English layout, 
> assuming that the chance is high that the latin layout is indeed the 
> standard English layout.
>
> If you only support Chromium based browsers you can use the Keyboard API: 
> https://developer.mozilla.org/en-US/docs/Web/API/Keyboard_API
>
> -- J.
>
> Venky schrieb am Mittwoch, 11. Dezember 2024 um 22:38:11 UTC+1:
>
>> Hey community,
>>
>> I am working on a project where I want to introduce a keyboard shortcut 
>> for a particular action.
>>
>> private void shortcutKeyHandler() {
>>     Event.addNativePreviewHandler(new Event.NativePreviewHandler() {
>>         @Override
>>         public void onPreviewNativeEvent(Event.NativePreviewEvent event) {
>>           NativeEvent nativeEvent = event.getNativeEvent();
>>           if (event.getTypeInt() == Event.ONKEYDOWN && 
>> nativeEvent.getAltKey() && nativeEvent.getKeyCode() == 191 && 
>> !db.isShowing()) {
>>             nativeEvent.preventDefault();
>>             execute();
>>           } 
>>         } 
>>       });
>>     }
>>
>> In the code, I implemented a shortcut (Alt + /) to display a dialog box. 
>> While it works well with English keyboard layouts, the problem arises with 
>> other layouts where the Slash key (keycode 191) generates a different 
>> keycode.
>>
>> Is there some workaround with the help of JSNI?
>>
>> I would greatly appreciate any help in determining how to check the 
>> character code and verify if the Alt key is pressed.
>>
>> Thanks in advance 
>>
>

-- 
You received this message because you are subscribed to the Google Groups "GWT 
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/google-web-toolkit/73f4c6f9-64a4-4c13-9157-e58c79902b21n%40googlegroups.com.

Reply via email to