【问题标题】:Eclipse 4: disable native alt-f4 behaviorEclipse 4:禁用本机 alt-f4 行为
【发布时间】:2013-11-16 11:50:24
【问题描述】:

有谁知道是否可以在 e4 应用程序中禁用或覆盖“alt+f4”(在 Windows 上关闭应用程序)的本机行为? 实现这一目标的建议解决方案是什么?

最好的问候

【问题讨论】:

    标签: swt eclipse-rcp e4


    【解决方案1】:

    我的解决方案是不是纯 SWT 解决方案。 它仅适用于 Windows。但是您提到了 Windows,如果您只针对一个平台,这已经足够了。它使用来自 SWT 的内部代码,但它映射到由 Microsoft 记录的 Windows API,因此不会更改。

    public static void main(String[] args) {
        final Display display = new Display();
        final Shell shell = new Shell(display);
    
        shell.addListener(SWT.Close, new Listener() {
            @Override
            public void handleEvent(Event event) {
                if (OS.GetKeyState(OS.VK_MENU) < 0 && OS.GetKeyState(OS.VK_F4) < 0) {
                    event.doit = false;
                }
            }
        });
    
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }
    

    【讨论】:

      【解决方案2】:

      找到了解决方案,但我对此并不满意。 创建了一个插件: 即在UIEvents.UILifeCycle.APP_STARTUP_COMPLETE 主题上注册事件处理程序。 然后以某种方式从主题元数据中检索 shell 并在显示器上注册一个过滤器。

      @PostConstruct
      void hookListeners() {
          eventHandler = new EventHandler() {
      
      
      
              @Override
              public void handleEvent(Event arg0) {
                  MElementContainer property = (MElementContainer) arg0.getProperty("org.eclipse.e4.data");
                  final Shell shell = (Shell) property.getSelectedElement().getWidget();
                  final Display display = shell.getDisplay();
                  display.addFilter(SWT.Close, new Listener() {
      
                      @Override
                      public void handleEvent(org.eclipse.swt.widgets.Event event) {
      
                          if (!MessageDialog.openQuestion(shell, "Exit",
                                  "Do you really want to close the Application?")) {
                              //see api documentation display.addFilter( 
                               event.type = SWT.NONE;
                               event.doit = false;
      
                          }
      
                      }
                  });             
              }
          };
      eventBroker.subscribe(UIEvents.UILifeCycle.APP_STARTUP_COMPLETE, eventHandler);
      }
      

      这个解决方案对我来说似乎不正确,所以如果有人有更好的解决方案,请分享:-)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-06
        • 1970-01-01
        相关资源
        最近更新 更多