简体   繁体   English

如何在 SWT 中监听粘贴键盘快捷键

[英]How to listen for paste keyboard shortcut in SWT

I want to globally listen for CTRL + v in SWT.我想在 SWT 中全局监听CTRL + v I can successfully listen for CTRL + c but paste seems to be handled differently in SWT.我可以成功地监听CTRL + c但粘贴在 SWT 中的处理方式似乎有所不同。 The following Listener shows how it works for copy and how it does not work for paste:以下Listener显示了它如何用于复制以及它如何不适用于粘贴:

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

        private final int CTRL = SWT.MOD1;
        private boolean checkNextEventForPaste = false;

        @Override
        public void handleEvent(Event event) {
            if(event.stateMask == CTRL && event.keyCode == 'c'){
                System.out.println("copy: this works!");
            }
            else if(event.stateMask == CTRL && (event.keyCode == 'v'
                    || event.keyCode == 'V'
                    || event.keyCode == 0x16
                    || event.keyCode == 118)){
                System.out.println("paste: does not work!");
            }

            else if (event.keyCode == CTRL){
                //control for paste is fired first
                checkNextEventForPaste  = true;
            }
            else if(checkNextEventForPaste){
                if(event.keyCode == 65536){
                    System.out.println("custom solution: seems to not only apply for paste");
                }
                checkNextEventForPaste = false;
            }
        }

    });

I debugged the paste case and created a custom solution.我调试了粘贴案例并创建了一个自定义解决方案。 The paste keyboard short cut creates the following event sequence:粘贴键盘快捷键创建以下事件序列:

  • first event with stateMask = 0 and keyCode = CTRL stateMask = 0keyCode = CTRL第一个event
  • second event with stateMask = 0 and keyCode = 65536 stateMask = 0keyCode = 65536第二个event

The problem is that the custom paste solution seems to apply for other shortcuts too.问题是自定义粘贴解决方案似乎也适用于其他快捷方式。 For example the copy shortcut creates the following event sequence:例如,复制快捷方式创建以下事件序列:

  • first event with stateMask = 0 and keyCode = CTRL stateMask = 0keyCode = CTRL第一个event
  • second event with stateMask = CTRL and keyCode = 'c'带有stateMask = CTRLkeyCode = 'c'第二个event
  • third event with stateMask = 0 and keyCode = 65536 stateMask = 0keyCode = 65536第三个event

Why does SWT handle the paste shortcut in a different way?为什么 SWT 以不同的方式处理粘贴快捷方式? Is it possible that the paste shortcut is already consumed by an other control?粘贴快捷方式是否可能已被其他控件使用? Or does anybody know how I can identify the paste shortcut?或者有人知道我如何识别粘贴快捷方式? A VerifyListener is not applicable in my use case. VerifyListener在我的用例中不适用。 I have implemented a more or less complex UI with custom selection, as you can see here .我已经实现自定义选择或多或少复杂的用户界面,你可以看到在这里

The code below works just fine for both Ctrl + c and Ctrl + v下面的代码适用于Ctrl + cCtrl + v

public static void main(String[] args)
{
    final Display d = new Display();
    Shell s = new Shell(d);

    d.addFilter(SWT.KeyDown, e ->
    {
        if (((e.stateMask & SWT.CTRL) == SWT.CTRL) && (e.keyCode == 'c'))
        {
            System.out.println("copy");
        }
        else if (((e.stateMask & SWT.CTRL) == SWT.CTRL) && (e.keyCode == 'v'))
        {
            System.out.println("paste");
        }
    });

    s.pack();
    s.open();

    while (!s.isDisposed())
    {
        if (!d.readAndDispatch())
            d.sleep();
    }
    d.dispose();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM