As of now i am using below code to detect two key presses. I.e, Ctrl+F(or) Shift+1...Etc,
CCombo comboBox = this.cellEditor.getViewer().getCCombo();
KeyAdapter keyAdapter = new KeyAdapter()
{
@Override
public void keyPressed(final KeyEvent evt)
{
// KeyPreferenceUtils is used to get the eclipse workbench key preference values.
int keyCode = KeyPreferenceUtils.getKeyValue();
if ((evt.stateMask == SWT.CTRL) || (evt.stateMask == SWT.ALT) || (evt.stateMask == SWT.SHIFT) || (evt.stateMask == SWT.COMMAND))
{
String pressedKey = Action.convertAccelerator(evt.stateMask + evt.keyCode);
int pressedKeyValue = Action.convertAccelerator(pressedKey);
if (pressedKeyValue == keyCode)
{
comboBox.setListVisible(true);
}
}
else if (evt.keyCode == keyCode)
{
comboBox.setListVisible(true);
}
}
};
comboBox.addKeyListener(keyAdapter);
Now my problem is i want detect 3 key presses. I.e, Ctrl+Shift+2 or some keys combination.
I don't know SWT that well but I'd assume evt.stateMask
is an int
used to represent a bit field and the SWT.XXX
values represent masks for the corresponding bits. Thus you could try if (evt.stateMask & SWT.CTRL > 0 && evt.stateMask & SWT.SHIFT > 0)
.
To clarify evt.stateMask & SWT.CTRL > 0
would mean that if the bit defined by SWT.CTRL
is set in stateMask
you'll get a value greater than 0 otherwise you get 0. However, if other bits are set as well you'll not detect that with that approach.
If you only want to allow those bits to be set you could try if (evt.stateMask == (SWT.CTRL | SWT.SHIFT) )
(i.e. you combine the two masks ans then compare).
Example:
We'll use 8-bit values for simplicity and assume SWT.CTRL = 00000001
and SWT.SHIFT = 00000100
.
With the first approach you'd get true for masks like 10000101
, 00010101
, 00000101
and false for masks like 00100100
etc.
With the second approach you'd only get true for stateMask = 00000101
.