You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SWT Shell嵌入Edge浏览器后键盘事件被Shell捕获的问题

问题:Edge子窗口无法捕获键盘事件,SWT Shell拦截事件

我有一个版本为1.6的Java SWT应用,使用swt.Browser组件展示嵌入式浏览器,但该组件基于IE内核,且无法升级SWT以使用Edge内核的Browser组件。

为解决此问题,我们开发了并行的Selenium WebDriver应用来启动Edge浏览器。在SWT应用中,我获取Edge进程的PID,通过org.eclipse.swt.internal.win32.OS库将Edge窗口设置为SWT Shell的子窗口。

目前Edge窗口可响应鼠标点击操作,但无法捕获键盘事件,键盘事件会被SWT Shell捕获。我已添加KeyListener代码验证:当聚焦Edge窗口并按键时,事件被Shell捕获,我希望这些键盘事件能传递至Edge浏览器。

用于定位Edge窗口的Shell.open()方法代码

import org.eclipse.swt.internal.win32.OS;

//...
                OS.ShowWindow(iWndMyWebDriver, OS.SW_HIDE);     
        
        OS.SetWindowLong(iWndMyWebDriver, OS.GWL_STYLE, OS.WS_CHILD & ~OS.WS_POPUP);
        
        
        OS.SetParent(iWndMyWebDriver, handle);
        
        int style = OS.GetWindowLongA(iWndMyWebDriver, OS.GWL_STYLE);

        style = style & ~OS.WS_SYSMENU;
        style = style & ~OS.WS_MAXIMIZEBOX;
        style = style & ~OS.WS_MINIMIZEBOX;
        style = style & ~OS.WS_CAPTION;

        OS.SetWindowLongA(iWndMyWebDriver, OS.GWL_STYLE, style);

        if (!isDisposed()) {
            OS.SetWindowPos(iWndMyWebDriver, 0, -1, -2, getBounds().width, 495, 4 + 32);
        }

        OS.BringWindowToTop(iWndMyWebDriver);

        RECT r = new RECT();
        r.left = 0;
        r.top = 0;

        OS.RedrawWindow(iWndMyWebDriver, r, 0, 256 + 512 + 1 + 2 + 128);

        OS.ShowWindow(iWndMyWebDriver, OS.SW_SHOW);

        OS.SetForegroundWindow(iWndMyWebDriver);
        
        if (!isVisible()) {
            setVisible(true);
            
            Pointer pHwnd = new Pointer(this.handle);

            User32.INSTANCE.ShowWindow(pHwnd, 9);

            User32.INSTANCE.SetForegroundWindow(pHwnd);

            UtilsOS.SetWindowAlwaysOnTop(this.handle, true);
            
            User32.INSTANCE.SetActiveWindow(pHwnd);
            
            setFocus();
    
            UtilsOS.SetWindowAlwaysOnTop(this.handle, false);

        }

新增的KeyListener代码

addKeyListener( new KeyListener() {
            
            @Override
            public void keyReleased(KeyEvent e) {
                // TODO Auto-generated method stub
                System.out.println( "key Released");
            }
            
            @Override
            public void keyPressed(KeyEvent e) {
                // TODO Auto-generated method stub
                System.out.println( "Key pressed");
            }
        });

内容的提问来源于stack exchange,提问作者Clonw

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 09:07:41