Modifier Key state

Go To StackoverFlow.com

1

I have a tiny (rikiki) problem in SWT ...

I am making a small class extending org.eclipse.swt.widgets.Composite and which is supposed to be nested in an RCP app ...

In this small class I have widgets which are supposed to react to mouse or keyboard event

BUT

I need to use modifier keys (Shift/Ctrl/Alt/...) to alter my coponents behaviours when I click them or send them keyboard event ...

The probleme is that I cannot just listen for mod-key striking because my user can strike it out from my component and then click it...

I cannot use a display filter to avoid disturbing the shell that nests my component.(but may be it will be my last solution in case there is no other solution)

I cannot make a transparent component that reads and dispatch events to all of my components because it would, at the most, be as large as my component and wont get mod-key strikes from the shell out my component (or even out from the shell) ...

Do anyone have any idea?

2009-06-16 14:52
by Ar3s
Oh found something about State Masks ... i'll search more about that ^ - Ar3s 2009-06-16 15:33


4

More or less it is like

myComponent.add<Any>Listener(new <Appropriate>Listener(){
    @Override
    public void <AppropriateMethod>(like KeyPress)>(<Appropriate>Event e) {
        int stateMask=e.stateMask;
        if((stateMask & SWT.ALT)==SWT.ALT){
            <Do_appropriate_actions>;
        }
        if((stateMask & SWT.CTRL)==SWT.CTRL){
            <Do_another_appropriate_actions>;
        }
        if((stateMask & SWT.SHIFT)==SWT.SHIFT){
            <Do_an_even_more_appropriate_actions_cause_you_are_kind_of_a_code_roxxor_!>;
        }
    };
};

Hope it helps ...

2009-06-26 10:17
by Ar3s


1

Try something along these lines to capture all keys and save them for later:

    Display.getDefault().addFilter( SWT.KeyDown, new Listener() {

        public void handleEvent( Event passedEvent ) {
            //Listen for and store as static var last pressed keycode
            System.out.println( "Key Event: " + passedEvent );
        }
    } );
2009-06-23 18:04
by fisherja
Yes but it captures all the key press, even the un-necessary ones ...

My State-Mask method (getting de modifier key pressed when you trigger another event) is much simpler ^^ (in my case at least - Ar3s 2009-06-24 08:44

For completeness could you post your method/solution to this question as well? Thank - fisherja 2009-06-24 16:01


0

You can test for modifier keys using following method:

/**
 * Key code of pressed modifier key.
 *
 * @param keyEvent the received key event
 * @return the key code or 0 if no modifier key is pressed
 */
private static int getModifierKeyCode(KeyEvent keyEvent) {
    return (keyEvent.stateMask & SWT.MODIFIER_MASK);
}

Example call:

item.addKeyListener(new KeyAdapter() {
    @Override
    public void keyPressed(KeyEvent keyEvent) {
        if(getModifierKeyCode(keyEvent) == SWT.CTRL && keyEvent.keyCode == 'f') { // CTRL + F
            // do something
        )
    }
}
2018-08-28 08:59
by Terran
Thanks. (That was 9 years ago though but hope this'll help someone one day maybe - Ar3s 2018-08-28 12:21
Ads