How to detect ctrl-f in my SWT application

后端 未结 1 1300
旧巷少年郎
旧巷少年郎 2020-12-08 23:40

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

相关标签:
1条回答
  • 2020-12-09 00:03

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

    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.


    For your second question:

    Why don't I get the pressed character when I'm holding down ctrl? When I hold down alt or shift I get the expected mask and the pressed character.

    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:

    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).

    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.

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

    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 ();
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题