如何检测ctrl-f在我的SWT应用程序 [英] How to detect ctrl-f in my SWT application

查看:134
本文介绍了如何检测ctrl-f在我的SWT应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个SWT UI,它具有在StyledText控件中显示文本的主要功能。我想为Ctrl-F添加一个处理程序,以便按下该快捷方式时,焦点设置为一个搜索框。我尝试使用以下代码来检测按键。

I have written an SWT UI which has a primary function of displaying text in a StyledText control. I want to add a handler for "Ctrl-F" so that when that shortcut is pressed the focus is set to a search box. I have tried using the following code to detect the keypress.

sWindow = new Shell();
...
sWindow.getDisplay().addFilter(SWT.KeyDown, new Listener()
{
  @Override
  public void handleEvent(Event e)
  {
    System.out.println("Filter-ctrl: " + SWT.CTRL);
    System.out.println("Filter-mask: " + e.stateMask);
    System.out.println("Filter-char: " + e.character);
  }
});

我期望当我按下Ctrl-f时,我会看到以下输出: p>

I was expecting that when I pressed "Ctrl-f" I would see the following output:

Filter-ctrl: 262144
Filter-mask: 262144
Filter-char: f

然而,实际上我实际上看到以下内容。

However, in practice I actually see the following.

Filter-ctrl: 262144
Filter-mask: 262144
Filter-char: <unprintable char - displayed as a box in eclipse console>

我有两个问题:


  • Display.addFilter(...)是添加全局快捷方式的最佳方式吗?我尝试过Display.addListener(...),但是根本没有收到任何事件。

  • 为什么当我按住ctrl时我不会得到被压缩的字符?

推荐答案

当我按住Alt或Shift Display.addFilter(...)是添加glbal快捷方式的最佳方式吗?我尝试过Display.addListener(...),但是根本没有收到任何事件。

Is Display.addFilter(...) the best way to add a glbal shortcut? I tried Display.addListener(...) but this didn't receive any events at all.

是的,通常 Display.addFilter(...)是添加glbal快捷方式的最佳方式,因为它们对事件侦听器的偏好偏高。请参阅 Display.addFilter(...) javadoc中的以下注释。

Yes, normally Display.addFilter(...) is the best way to add a glbal shortcut because they have higher preference over the event listeners. See the below comment from Display.addFilter(...) javadoc.


由于事件过滤器在其他
监听器之前运行,事件过滤器可以同时
阻止其他监听器并设置
事件中的任意字段。对于
这个原因,事件过滤器都是
强大而危险的。他们应该
一般避免表现,
调试和代码维护
的原因。

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.

< hr>
对于您的第二个问题:

为什么不当我按下ctrl时,得到压制的角色?当我按住Alt或Shift时,我会得到预期的面具和按下的字符。

问题是你在看错了地方。不要查询 e.character ,您应该使用 e.keyCode 。根据 e.character 的javadoc,您将不会仅获得字符 f

The problem is that you are looking at the wrong place. Instead of querying e.character you should be using e.keyCode. As per javadoc of e.character you won't get just character f:


根据事件,字符
由键入的键表示。
这是所有修饰符都被应用于
之后,
结果的最后一个字符。例如,当用户
键入Ctrl + A时,字符值为
0x01(ASCII SOH)。

Depending on the event, the character represented by the key that was typed. This is the final character that results after all modifiers have been applied. For example, when the user types Ctrl+A, the character value is 0x01 (ASCII SOH).

所以当你按 CTRL + f 时,它转换为 0x06 (ASCII ACK)。当您执行 ALT + f SHIFT + f

So when you press CTRL+f then it converts in 0x06 (ASCII ACK). Which is not the case when you do ALT+f or SHIFT+f.

另一方面, e.keyCode 的javadoc说:

On the other hand the javadoc of e.keyCode says:

根据事件,键入代码
,键

SWT中的键代码常量定义为
。当
事件的字符字段不明确时,此字段
包含
原始字符的未受影响的值。例如,
键入Ctrl + M或Enter都会导致
的字符'\r',但是$ c
字段也将包含'\r',当
Enter为当键入Ctrl + M
时,键入和m。

depending on the event, the key code of the key that was typed, as defined by the key code constants in class SWT. When the character field of the event is ambiguous, this field contains the unaffected value of the original character. For example, typing Ctrl+M or Enter both result in the character '\r' but the keyCode field will also contain '\r' when Enter was typed and 'm' when Ctrl+M was typed.

查看下面的代码以获取更多详细信息。对于演示,我试图将听众放在显示测试

Check the below code for more details. For demo I have tried to put listener on Display and Test.

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

public class ControlF 
{
    public static void main(String[] args) 
    {

        Display display = new Display ();

        final Shell shell = new Shell (display);
        final Color green = display.getSystemColor (SWT.COLOR_GREEN);
        final Color orig = shell.getBackground();

        display.addFilter(SWT.KeyDown, new Listener() {

            public void handleEvent(Event e) {
                if(((e.stateMask & SWT.CTRL) == SWT.CTRL) && (e.keyCode == 'f'))
                {
                    System.out.println("From Display I am the Key down !!" + e.keyCode);
                }
            }
        });

        shell.addKeyListener(new KeyListener() {
            public void keyReleased(KeyEvent e) {
                if(((e.stateMask & SWT.CTRL) == SWT.CTRL) && (e.keyCode == 'f'))
                {
                    shell.setBackground(orig);
                    System.out.println("Key up !!");
                }
            }
            public void keyPressed(KeyEvent e) {
                if(((e.stateMask & SWT.CTRL) == SWT.CTRL) && (e.keyCode == 'f'))
                {
                    shell.setBackground(green);
                    System.out.println("Key down !!");
                }
            }
        });

        shell.setSize (200, 200);
        shell.open ();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch ()) display.sleep ();
        }
        display.dispose ();

    }
}

这篇关于如何检测ctrl-f在我的SWT应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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