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

24 篇文章 0 订阅

以前只知道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-

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

10km

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值