java - 如何在我的 SWT 应用程序中检测 ctrl-f

标签 java event-handling keyboard-shortcuts swt

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

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 时,我会看到以下输出:

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

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

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(...) 但这根本没有收到任何事件。

是的,通常 Display.addFilter(...) 是添加全局快捷方式的最佳方式,因为它们比事件监听器具有更高的优先级。请参阅 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.


第二个问题:

为什么我在按住 ctrl 时没有得到按下的字符?当我按住 alt 或 shift 时,我得到预期的掩码和按下的字符。

问题是你看错地方了。您应该使用 e.keyCode 而不是查询 e.character。根据 e.character 的 javadoc,您不会只得到字符 f:

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 时情况并非如此.

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

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.

查看以下代码了解更多详情。对于演示,我尝试将监听器放在 DisplayTest 上。

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 ();

    }
}

关于java - 如何在我的 SWT 应用程序中检测 ctrl-f,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5842190/

相关文章:

intellij-idea - intelliJ 的任何键盘快捷键可以在所选文本中用下划线替换空格?

Javascript ,事件处理程序总是被调用,即使事件没有引发

java - Java 中 String 的内存高效、低开销替换

java - 如何将属性文件中的类路径值赋予变量

java - Spring MVC ResourceBundleMessageSource XML 配置

jquery - 从另一个对象的单击事件中手动触发 jQuery 单击事件

javascript - jQuery live ('click' ) 右键点击触发

VIM:删除+粘贴在一个 "command"中?

eclipse - Eclipse的重做键盘快捷键

Java 7 构造函数