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?
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 ...
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 );
}
} );
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
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
)
}
}