private int getKeyCode(final KeySequence inSequence) { int out = 0; for (final KeyStroke lStroke : inSequence.getKeyStrokes()) { out |= lStroke.getModifierKeys() | lStroke.getNaturalKey(); } return out; }
@Test public void testKeyStrokes() { final KeyStroke lKeyStroke = KeyStroke.getInstance(SWT.MOD1, 'I'); assertEquals(SWT.MOD1 | 'I', lKeyStroke.getModifierKeys() | lKeyStroke.getNaturalKey()); assertEquals("CTRL+I", lKeyStroke.format()); final KeySequence lSequence = KeySequence.getInstance(lKeyStroke); assertEquals("CTRL+I", lSequence.format()); assertEquals(SWT.MOD1 | 'I', getKeyCode(lSequence)); }
/** * @param event the key event to be checked * @param commandId the command to be checked * @return true if the given key event can trigger the passed command (and false otherwise). */ public static boolean matchesKeybinding(KeyEvent event, String commandId) { final IBindingService bindingSvc = (IBindingService) PlatformUI.getWorkbench().getAdapter(IBindingService.class); TriggerSequence[] activeBindingsFor = bindingSvc.getActiveBindingsFor(commandId); for (TriggerSequence seq : activeBindingsFor) { if (seq instanceof KeySequence) { KeySequence keySequence = (KeySequence) seq; KeyStroke[] keyStrokes = keySequence.getKeyStrokes(); for (KeyStroke keyStroke : keyStrokes) { if (keyStroke.getNaturalKey() == event.keyCode && (keyStroke.getModifierKeys() & event.stateMask) != 0) { return true; } } } } return false; }
/** * Determine if the key event represents a "submit" action (<modifier>+Enter). * * @param event * @return true, if it means submit, false otherwise */ public static boolean isSubmitKeyEvent(KeyEvent event) { return (event.stateMask & SWT.MODIFIER_MASK) != 0 && event.keyCode == SUBMIT_KEY_STROKE.getNaturalKey(); }