1 11 12 package org.eclipse.jface.bindings.keys; 13 14 import org.eclipse.jface.bindings.keys.formatting.IKeyFormatter; 15 import org.eclipse.jface.bindings.keys.formatting.NativeKeyFormatter; 16 import org.eclipse.swt.SWT; 17 import org.eclipse.swt.events.KeyEvent; 18 import org.eclipse.swt.widgets.Event; 19 20 27 public final class SWTKeySupport { 28 29 33 private static final IKeyFormatter NATIVE_FORMATTER = new NativeKeyFormatter(); 34 35 43 public static final KeyStroke convertAcceleratorToKeyStroke(int accelerator) { 44 final int modifierKeys = accelerator & SWT.MODIFIER_MASK; 45 final int naturalKey; 46 if (accelerator == modifierKeys) { 47 naturalKey = KeyStroke.NO_KEY; 48 } else { 49 naturalKey = accelerator - modifierKeys; 50 } 51 52 return KeyStroke.getInstance(modifierKeys, naturalKey); 53 } 54 55 70 public static final int convertEventToModifiedAccelerator(final Event event) { 71 int modifiers = event.stateMask & SWT.MODIFIER_MASK; 72 char character = topKey(event); 73 return modifiers + toUpperCase(character); 74 } 75 76 93 public static final int convertEventToUnmodifiedAccelerator( 94 final Event event) { 95 return convertEventToUnmodifiedAccelerator(event.stateMask, 96 event.keyCode); 97 } 98 99 117 private static final int convertEventToUnmodifiedAccelerator( 118 final int stateMask, final int keyCode) { 119 int modifiers = stateMask & SWT.MODIFIER_MASK; 120 int character = keyCode; 121 return modifiers + toUpperCase(character); 122 } 123 124 140 public static final int convertEventToUnmodifiedAccelerator( 141 final KeyEvent event) { 142 return convertEventToUnmodifiedAccelerator(event.stateMask, 143 event.keyCode); 144 } 145 146 158 public static final int convertEventToUnshiftedModifiedAccelerator( 159 final Event event) { 160 if (Character.isLetter((char) event.keyCode)) { 162 return convertEventToUnmodifiedAccelerator(event); 163 } 164 165 int modifiers = event.stateMask & (SWT.MODIFIER_MASK ^ SWT.SHIFT); 166 char character = topKey(event); 167 return modifiers + toUpperCase(character); 168 } 169 170 179 public static final int convertKeyStrokeToAccelerator( 180 final KeyStroke keyStroke) { 181 return keyStroke.getModifierKeys() + keyStroke.getNaturalKey(); 182 } 183 184 191 public static IKeyFormatter getKeyFormatterForPlatform() { 192 return NATIVE_FORMATTER; 193 } 194 195 206 private static final char topKey(final Event event) { 207 char character = event.character; 208 boolean ctrlDown = (event.stateMask & SWT.CTRL) != 0; 209 210 if (ctrlDown && event.character != event.keyCode 211 && event.character < 0x20 212 && (event.keyCode & SWT.KEYCODE_BIT) == 0) { 213 character += 0x40; 214 } 215 216 return character; 217 } 218 219 227 private static final int toUpperCase(int keyCode) { 228 if (keyCode > 0xFFFF) { 230 return keyCode; 231 } 232 233 final char character = (char) keyCode; 235 return Character.isLetter(character) ? Character.toUpperCase(character) 236 : keyCode; 237 } 238 239 242 protected SWTKeySupport() { 243 } 245 } 246 | Popular Tags |