Java 复合密钥侦听器?

Java 复合密钥侦听器?,java,swt,listener,keylistener,composite,Java,Swt,Listener,Keylistener,Composite,我想为组合添加一个键侦听器。 我的代码如下: @Override protected Control createDialogArea(Composite parent) { //add swt text box , combo etc to parent } 该组合是一个:org.eclipse.swt.widgets.composite 现在我想向复合父级添加一个键侦听器。 就像用户按下ctrl键或escape键一样,用户应该得到通知。 即使焦点在文本或组合字段上,也应该通知父侦听

我想为组合添加一个键侦听器。 我的代码如下:

@Override
protected Control createDialogArea(Composite parent) {
    //add swt text box , combo etc to parent
}
该组合是一个:org.eclipse.swt.widgets.composite
现在我想向复合父级添加一个键侦听器。
就像用户按下ctrl键或escape键一样,用户应该得到通知。
即使焦点在文本或组合字段上,也应该通知父侦听器。
谢谢你的帮助。

好的,给你:在
显示器上添加一个
过滤器。在
侦听器中
检查当前焦点控件的父控件是否是
组合的
外壳
。如果是,请检查密钥代码

总之,如果您的注意力“在”您的
组合中,您将处理关键事件,如果它“在”您的
组合中,您将忽略它

public static void main(String[] args)
{
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(1, false));

    final Composite content = new Composite(shell, SWT.NONE);
    content.setLayout(new GridLayout(2, false));

    Text text = new Text(content, SWT.BORDER);
    Button button = new Button(content, SWT.PUSH);
    button.setText("Button");

    display.addFilter(SWT.KeyUp, new Listener()
    {
        @Override
        public void handleEvent(Event e)
        {
            if (e.widget instanceof Control && isChild(content, (Control) e.widget))
            {
                if ((e.stateMask & SWT.CTRL) == SWT.CTRL)
                {
                    System.out.println("Ctrl pressed");
                }
                else if(e.keyCode == SWT.ESC)
                {
                    System.out.println("Esc pressed");
                }
            }
        }
    });

    Text outsideText = new Text(shell, SWT.BORDER);

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

private static boolean isChild(Control parent, Control child)
{
    if (child.equals(parent))
        return true;

    Composite p = child.getParent();

    if (p != null)
        return isChild(parent, p);
    else
        return false;
}