java SWT:Display.addFilter设置快捷键的例子-切换全屏

以前只知道org.eclipse.swt.widgets.Widget以及子类可以添加Listener。
今天才搞清楚:
Display也有addListener方法,而且还有优先级更高的addFilter方法:

addFilter方法的原文说明:
Adds the listener to the collection of listeners who will be notified when an event of the given type occurs anywhere in a widget. The event type is one of the event constants defined in class SWT. When the event does occur, the listener is notified by sending it the handleEvent() message.

Setting the type of an event to SWT.None from within the handleEvent() method can be used to change the event type and stop subsequent Java listeners from running. Because event filters run before other listeners, event filters can both block other listeners and set arbitrary fields within an event. For this reason, event filters are both powerful and dangerous. They should generally be avoided for performance, debugging and code maintenance reasons.

从上面的说明可以知道使用addFilter方法添加Listener,可以拦截所有Widget的事件.
so,利用addFilter方法的这个特性可以实现全局的快捷键。
如下代码实现了F11切换全屏状态

        // F11全局快捷键,在切换全屏
        Display.getDefault().addFilter(SWT.KeyDown, new Listener(){
            @Override
            public void handleEvent(Event e) {
                if(SWTKeySupport.convertEventToUnmodifiedAccelerator(e)==SWT.F11){
                    Control focusControl = Display.getDefault().getFocusControl();              
                    boolean fullScreen = !getShell().getFullScreen();
                    {
                        // 应用代码。。。。
                    }                   
                    getShell().setFullScreen(fullScreen);
                    //shell.setMaximized(fullScreen);
                    // 恢复焦点
                    if(null!=focusControl)
                        focusControl.forceFocus();
                    e.doit=false;
                }
            }});

如果在普通的Widget中加入全局热键,则要记得在dispose中删除Listener,否则如果widget被销毁后,产生热键事件时会抛出异常
TestAddFilter.java

package testwb;

import org.eclipse.jface.bindings.keys.SWTKeySupport;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;

public class TestAddFilter extends Composite {
    // 侦听Ctrl-M键,实现全屏切换
    private final Listener ctrlmListener = new Listener() {
        @Override
        public void handleEvent(Event e) {
            if (SWTKeySupport.convertEventToUnmodifiedAccelerator(e) == (SWT.CTRL + 'M')) {
                System.out.println("Ctrl-M");
            }
        }
    };

    public TestAddFilter(Composite parent, int style) {
        super(parent, style);
        // 加入Ctrl-M全局快捷键监听
        getShell().getDisplay().addFilter(SWT.KeyDown, ctrlmListener);
        // 添加DisposeListener,对象被销毁时,删除filter
        addDisposeListener(new DisposeListener() {
            public void widgetDisposed(DisposeEvent e) {
                getShell().getDisplay().removeFilter(SWT.KeyDown, ctrlmListener);
            }
        });
    }
}

参考:
http://help.eclipse.org/neon/nftopic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/swt/widgets/Display.html#addFilter-int-org.eclipse.swt.widgets.Listener-

你可能感兴趣的:(java,ui)