SWT Global KeyListener 按钮焦点问题 [英] SWT Global KeyListener Button Focus Problem

查看:35
本文介绍了SWT Global KeyListener 按钮焦点问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的应用程序,我需要空格键来调用一个独立于焦点小部件的函数,在应用程序中的任何地方,但前提是相应的选项卡被打开.我发现可以在显示中添加过滤器,如下所示:

for my app I need the space key to call a function independent from the focused widget, everywhere in the app but only if the according tab is opend. I found that one can add a filter to the display, like this:

 getShell().getDisplay().addFilter(SWT.KeyDown, new Listener() {

            public void handleEvent(Event arg0) {
                if( arg0.character == 32 ) { /**SPACE*/
                    if( mainTabs.getSelection().equals(analyseSoundFilesTab)) {
                        soundController.playButtonClickHandler();
                    }
                }
            }

        });

大多数情况下都可以正常工作,但是如果我通过选项卡"或移位选项卡"将焦点放在按钮上,这有点奇怪 - 空格键会激活按下的按钮",就像单击一样用鼠标的按钮.我现在有点卡住了,我不知道如何避免这种情况...对于按钮,我实现了一个 SelectionListener.

That works fine most of the time, but if I give a button the focus via the "tab" or "shift tab", its kinda strange - the space bar will than activate a "button pressed", as if one clicks the button with the mouse. Im a bit stuck now, I don't know how to avoid this... For the buttons, I have implemented a SelectionListener.

问候.

推荐答案

您可以使用 TraverseListener 并使用 doin 字段禁用按下事件检测.这是一个示例代码:

You can use TraverseListener and disabled press event detection using doin field. Here is a sample code:

display.addFilter(SWT.KeyDown, new Listener() {
    public void handleEvent(Event e) {
        if (e.character == 32) {
            System.out.printf("Space detected %s\n", e);
        }
    }
});

Button b1 = new Button(shell, SWT.PUSH);
b1.addSelectionListener(new SelectionAdapter() {
    @Override
    public void widgetSelected(SelectionEvent se) {
        System.out.printf("Button pressed %s\n", se);
    }
});

b1.addTraverseListener(new TraverseListener() {
    @Override
    public void keyTraversed(TraverseEvent te) {
        System.out.printf("Traverse detected %s\n", te);
        te.doit = true;
    }
});

如果 addTraverseListener() 不存在,则在过滤后检测到您的空格按钮,因此您会看到检测到空格...",然后是按下按钮...".现在您设置了 te.doit = true,您告诉 SWT 执行空格键遍历(实际上什么都不做),而不是触发键侦听器.您可以选择检查 te.detail 以仅防止助记符遍历.

If addTraverseListener() didn't exist, your space button was detected after filter, so you would see "Space detected..." and after that "Button pressed...". Now that you set te.doit = true, you say to SWT to do space bar traversal (which does nothing actually) instead of firing key listener. You may optionally check te.detail to only prevent mnemonic traversals.

这篇关于SWT Global KeyListener 按钮焦点问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆