Skip to content

Commit 248ab17

Browse files
committed
Edge: Implement ICoreWebView2Controller#add_AcceleratorKeyPressed()
The ICoreWebView2AcceleratorKeyPressedEvent does not give us all key events and is therefore much less complete than the IE.handleDOMEvent() equivalent. But at least this allows us to react on VK_ESCAPE (allowing e.g. element info popups to be closed via ESC) and other typical key combos like Ctrl-N, etc.
1 parent 8aec5fc commit 248ab17

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

bundles/org.eclipse.swt/Eclipse SWT Browser/win32/org/eclipse/swt/browser/Edge.java

+60
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,9 @@ public void create(Composite parent, int style) {
429429
handler = newCallback(this::handleGotFocus);
430430
controller.add_GotFocus(handler, token);
431431
handler.Release();
432+
handler = newCallback(this::handleAcceleratorKeyPressed);
433+
controller.add_AcceleratorKeyPressed(handler, token);
434+
handler.Release();
432435
if (webView_2 != null) {
433436
handler = newCallback(this::handleDOMContentLoaded);
434437
webView_2.add_DOMContentLoaded(handler, token);
@@ -794,6 +797,63 @@ int handleGotFocus(long pView, long pArg) {
794797
return COM.S_OK;
795798
}
796799

800+
/**
801+
* Events are not fired for all keyboard shortcuts.
802+
* <ul>
803+
* <li>Events for ctrl, alt modifiers are sent, but not for shift. So we use
804+
* GetKeyState() to read modifier keys consistently and don't send out any
805+
* events for the modifier-only events themselves.
806+
* <li>We are missing some other keys (e.g. VK_TAB or VK_RETURN, unless modified
807+
* by ctrl or alt).
808+
* </ul>
809+
* This is a best-effort implementation oriented towards
810+
* {@link IE#handleDOMEvent(org.eclipse.swt.ole.win32.OleEvent)}.
811+
*
812+
* @see <a href=
813+
* "https://learn.microsoft.com/en-us/microsoft-edge/webview2/reference/win32/icorewebview2controller">https://learn.microsoft.com/en-us/microsoft-edge/webview2/reference/win32/icorewebview2controller</a>
814+
*/
815+
int handleAcceleratorKeyPressed(long pView, long pArgs) {
816+
ICoreWebView2AcceleratorKeyPressedEventArgs args = new ICoreWebView2AcceleratorKeyPressedEventArgs(pArgs);
817+
int[] virtualKey = new int[1];
818+
args.get_VirtualKey(virtualKey);
819+
int[] lparam = new int[1];
820+
args.get_KeyEventLParam(lparam);
821+
long flags = Integer.toUnsignedLong(lparam[0]) >> 16;
822+
boolean isDown = (flags & 0x8000) == 0;
823+
824+
if (virtualKey[0] == OS.VK_SHIFT || virtualKey[0] == OS.VK_CONTROL || virtualKey[0] == OS.VK_MENU) {
825+
return COM.S_OK;
826+
}
827+
828+
Event keyEvent = new Event ();
829+
keyEvent.widget = browser;
830+
keyEvent.keyCode = translateKey(virtualKey[0]);
831+
if (OS.GetKeyState (OS.VK_MENU) < 0) keyEvent.stateMask |= SWT.ALT;
832+
if (OS.GetKeyState (OS.VK_SHIFT) < 0) keyEvent.stateMask |= SWT.SHIFT;
833+
if (OS.GetKeyState (OS.VK_CONTROL) < 0) keyEvent.stateMask |= SWT.CONTROL;
834+
835+
if (isDown) {
836+
keyEvent.type = SWT.KeyDown;
837+
// Ignore repeated events while key is pressed
838+
boolean isRepeat = (flags & 0x4000) != 0;
839+
if (isRepeat) {
840+
return COM.S_OK;
841+
}
842+
843+
if (!sendKeyEvent(keyEvent)) {
844+
args.put_Handled(true);
845+
}
846+
} else {
847+
keyEvent.type = SWT.KeyUp;
848+
browser.notifyListeners (keyEvent.type, keyEvent);
849+
if (!keyEvent.doit) {
850+
args.put_Handled(true);
851+
}
852+
}
853+
854+
return COM.S_OK;
855+
}
856+
797857
int handleMoveFocusRequested(long pView, long pArgs) {
798858
ICoreWebView2MoveFocusRequestedEventArgs args = new ICoreWebView2MoveFocusRequestedEventArgs(pArgs);
799859
int[] pReason = new int[1];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2024 SAP SE and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* SAP SE - initial implementation
13+
*******************************************************************************/
14+
package org.eclipse.swt.internal.ole.win32;
15+
16+
public class ICoreWebView2AcceleratorKeyPressedEventArgs extends IUnknown {
17+
18+
public ICoreWebView2AcceleratorKeyPressedEventArgs(long address) {
19+
super(address);
20+
}
21+
22+
public int get_KeyEventKind(long[] value) {
23+
return COM.VtblCall(3, address, value);
24+
}
25+
26+
public int get_VirtualKey(int[] value) {
27+
return COM.VtblCall(4, address, value);
28+
}
29+
30+
public int get_KeyEventLParam(int[] value) {
31+
return COM.VtblCall(5, address, value);
32+
}
33+
34+
public int get_PhysicalKeyStatus(long[] value) {
35+
return COM.VtblCall(6, address, value);
36+
}
37+
38+
public int get_Handled(int[] value) {
39+
return COM.VtblCall(7, address, value);
40+
}
41+
42+
public int put_Handled(boolean value) {
43+
return COM.VtblCall(8, address, value ? 1 : 0);
44+
}
45+
46+
}

0 commit comments

Comments
 (0)