I want to be able to make a help/search popup in my application when the shift key is clicked twice. My initial ide was to put this addListener(KeyDownEvent.class,e->{}) in my MainLayout constructor and that this would work on any view that uses the MainLayout, but this seems kinda iffy.
If my last focus was anywhere on the MainLayout (Header, Drawer etc) then the event is captured and if I have focus on a views component (TextField, Button etc) this also works, but not if the last focus was on a VerticalLayout or any white-space in the views.
Any tips to where I could put this listener to make it also work on whitespace etc? Or any explanation to why it wont work?
I cannot explain, why it’s not working with main layout (maybe some browser thingy regarding focus handling), but as a workaround you can add your key listener to the UI. Unfortunately the UI does not implement the KeyNotifier interface, so you have to manually add the listener using the ComponentUtil.
UI ui = ...
ComponentUtil.addListener(ui, KeyUpEvent.class, new KeyEventListener<>(event -> {
System.out.println("shift at " + LocalTime.now());
}, Key.SHIFT));
I would recommend to add that in the ui init event of the service initializer to ensure, that the listener is only added once (or to check otherwise, if it already has been registered).
However, I cannot guarantee, that this will work forever or in every use case, but at least on a simple app it worked.