1 11 12 package org.eclipse.ui.keys; 13 14 import java.util.Iterator ; 15 import java.util.SortedSet ; 16 import java.util.TreeSet ; 17 18 import org.eclipse.swt.SWT; 19 import org.eclipse.swt.events.KeyEvent; 20 import org.eclipse.swt.widgets.Event; 21 import org.eclipse.ui.internal.keys.NativeKeyFormatter; 22 23 29 public final class SWTKeySupport { 30 31 39 public static KeyStroke convertAcceleratorToKeyStroke(int accelerator) { 40 final SortedSet modifierKeys = new TreeSet (); 41 NaturalKey naturalKey = null; 42 43 if ((accelerator & SWT.ALT) != 0) { 44 modifierKeys.add(ModifierKey.ALT); 45 } 46 47 if ((accelerator & SWT.COMMAND) != 0) { 48 modifierKeys.add(ModifierKey.COMMAND); 49 } 50 51 if ((accelerator & SWT.CTRL) != 0) { 52 modifierKeys.add(ModifierKey.CTRL); 53 } 54 55 if ((accelerator & SWT.SHIFT) != 0) { 56 modifierKeys.add(ModifierKey.SHIFT); 57 } 58 59 if (((accelerator & SWT.KEY_MASK) == 0) && (accelerator != 0)) { 60 naturalKey = null; 62 } else { 63 accelerator &= SWT.KEY_MASK; 65 66 switch (accelerator) { 67 case SWT.ARROW_DOWN: 68 naturalKey = SpecialKey.ARROW_DOWN; 69 break; 70 case SWT.ARROW_LEFT: 71 naturalKey = SpecialKey.ARROW_LEFT; 72 break; 73 case SWT.ARROW_RIGHT: 74 naturalKey = SpecialKey.ARROW_RIGHT; 75 break; 76 case SWT.ARROW_UP: 77 naturalKey = SpecialKey.ARROW_UP; 78 break; 79 case SWT.BREAK: 80 naturalKey = SpecialKey.BREAK; 81 break; 82 case SWT.CAPS_LOCK: 83 naturalKey = SpecialKey.CAPS_LOCK; 84 break; 85 case SWT.END: 86 naturalKey = SpecialKey.END; 87 break; 88 case SWT.F1: 89 naturalKey = SpecialKey.F1; 90 break; 91 case SWT.F10: 92 naturalKey = SpecialKey.F10; 93 break; 94 case SWT.F11: 95 naturalKey = SpecialKey.F11; 96 break; 97 case SWT.F12: 98 naturalKey = SpecialKey.F12; 99 break; 100 case SWT.F2: 101 naturalKey = SpecialKey.F2; 102 break; 103 case SWT.F3: 104 naturalKey = SpecialKey.F3; 105 break; 106 case SWT.F4: 107 naturalKey = SpecialKey.F4; 108 break; 109 case SWT.F5: 110 naturalKey = SpecialKey.F5; 111 break; 112 case SWT.F6: 113 naturalKey = SpecialKey.F6; 114 break; 115 case SWT.F7: 116 naturalKey = SpecialKey.F7; 117 break; 118 case SWT.F8: 119 naturalKey = SpecialKey.F8; 120 break; 121 case SWT.F9: 122 naturalKey = SpecialKey.F9; 123 break; 124 case SWT.HOME: 125 naturalKey = SpecialKey.HOME; 126 break; 127 case SWT.INSERT: 128 naturalKey = SpecialKey.INSERT; 129 break; 130 case SWT.KEYPAD_0: 131 naturalKey = SpecialKey.NUMPAD_0; 132 break; 133 case SWT.KEYPAD_1: 134 naturalKey = SpecialKey.NUMPAD_1; 135 break; 136 case SWT.KEYPAD_2: 137 naturalKey = SpecialKey.NUMPAD_2; 138 break; 139 case SWT.KEYPAD_3: 140 naturalKey = SpecialKey.NUMPAD_3; 141 break; 142 case SWT.KEYPAD_4: 143 naturalKey = SpecialKey.NUMPAD_4; 144 break; 145 case SWT.KEYPAD_5: 146 naturalKey = SpecialKey.NUMPAD_5; 147 break; 148 case SWT.KEYPAD_6: 149 naturalKey = SpecialKey.NUMPAD_6; 150 break; 151 case SWT.KEYPAD_7: 152 naturalKey = SpecialKey.NUMPAD_7; 153 break; 154 case SWT.KEYPAD_8: 155 naturalKey = SpecialKey.NUMPAD_8; 156 break; 157 case SWT.KEYPAD_9: 158 naturalKey = SpecialKey.NUMPAD_9; 159 break; 160 case SWT.KEYPAD_ADD: 161 naturalKey = SpecialKey.NUMPAD_ADD; 162 break; 163 case SWT.KEYPAD_CR: 164 naturalKey = SpecialKey.NUMPAD_ENTER; 165 break; 166 case SWT.KEYPAD_DECIMAL: 167 naturalKey = SpecialKey.NUMPAD_DECIMAL; 168 break; 169 case SWT.KEYPAD_DIVIDE: 170 naturalKey = SpecialKey.NUMPAD_DIVIDE; 171 break; 172 case SWT.KEYPAD_EQUAL: 173 naturalKey = SpecialKey.NUMPAD_EQUAL; 174 break; 175 case SWT.KEYPAD_MULTIPLY: 176 naturalKey = SpecialKey.NUMPAD_MULTIPLY; 177 break; 178 case SWT.KEYPAD_SUBTRACT: 179 naturalKey = SpecialKey.NUMPAD_SUBTRACT; 180 break; 181 case SWT.NUM_LOCK: 182 naturalKey = SpecialKey.NUM_LOCK; 183 break; 184 case SWT.PAGE_DOWN: 185 naturalKey = SpecialKey.PAGE_DOWN; 186 break; 187 case SWT.PAGE_UP: 188 naturalKey = SpecialKey.PAGE_UP; 189 break; 190 case SWT.PAUSE: 191 naturalKey = SpecialKey.PAUSE; 192 break; 193 case SWT.PRINT_SCREEN: 194 naturalKey = SpecialKey.PRINT_SCREEN; 195 break; 196 case SWT.SCROLL_LOCK: 197 naturalKey = SpecialKey.SCROLL_LOCK; 198 break; 199 default: 200 naturalKey = CharacterKey 201 .getInstance((char) (accelerator & 0xFFFF)); 202 } 203 } 204 205 return KeyStroke.getInstance(modifierKeys, naturalKey); 206 } 207 208 223 public static int convertEventToModifiedAccelerator(Event event) { 224 int modifiers = event.stateMask & SWT.MODIFIER_MASK; 225 char character = topKey(event); 226 return modifiers + toUpperCase(character); 227 } 228 229 245 public static int convertEventToUnmodifiedAccelerator(Event event) { 246 return convertEventToUnmodifiedAccelerator(event.stateMask, 247 event.keyCode); 248 } 249 250 268 private static int convertEventToUnmodifiedAccelerator(int stateMask, 269 int keyCode) { 270 int modifiers = stateMask & SWT.MODIFIER_MASK; 271 int character = keyCode; 272 return modifiers + toUpperCase(character); 273 } 274 275 291 public static int convertEventToUnmodifiedAccelerator(KeyEvent event) { 292 return convertEventToUnmodifiedAccelerator(event.stateMask, 293 event.keyCode); 294 } 295 296 307 public static int convertEventToUnshiftedModifiedAccelerator(Event event) { 308 if (Character.isLetter((char) event.keyCode)) { 310 return convertEventToUnmodifiedAccelerator(event); 311 } 312 313 int modifiers = event.stateMask & (SWT.MODIFIER_MASK ^ SWT.SHIFT); 314 char character = topKey(event); 315 return modifiers + toUpperCase(character); 316 } 317 318 326 public static final int convertKeyStrokeToAccelerator( 327 final KeyStroke keyStroke) { 328 int accelerator = 0; 329 final Iterator iterator = keyStroke.getModifierKeys().iterator(); 330 331 while (iterator.hasNext()) { 332 final ModifierKey modifierKey = (ModifierKey) iterator.next(); 333 334 if (modifierKey == ModifierKey.ALT) { 335 accelerator |= SWT.ALT; 336 } else if (modifierKey == ModifierKey.COMMAND) { 337 accelerator |= SWT.COMMAND; 338 } else if (modifierKey == ModifierKey.CTRL) { 339 accelerator |= SWT.CTRL; 340 } else if (modifierKey == ModifierKey.SHIFT) { 341 accelerator |= SWT.SHIFT; 342 } 343 } 344 345 final NaturalKey naturalKey = keyStroke.getNaturalKey(); 346 347 if (naturalKey instanceof CharacterKey) { 348 accelerator |= ((CharacterKey) naturalKey).getCharacter(); 349 } else if (naturalKey instanceof SpecialKey) { 350 final SpecialKey specialKey = (SpecialKey) naturalKey; 351 352 if (specialKey == SpecialKey.ARROW_DOWN) { 353 accelerator |= SWT.ARROW_DOWN; 354 } else if (specialKey == SpecialKey.ARROW_LEFT) { 355 accelerator |= SWT.ARROW_LEFT; 356 } else if (specialKey == SpecialKey.ARROW_RIGHT) { 357 accelerator |= SWT.ARROW_RIGHT; 358 } else if (specialKey == SpecialKey.ARROW_UP) { 359 accelerator |= SWT.ARROW_UP; 360 } else if (specialKey == SpecialKey.END) { 361 accelerator |= SWT.END; 362 } else if (specialKey == SpecialKey.F1) { 363 accelerator |= SWT.F1; 364 } else if (specialKey == SpecialKey.F10) { 365 accelerator |= SWT.F10; 366 } else if (specialKey == SpecialKey.F11) { 367 accelerator |= SWT.F11; 368 } else if (specialKey == SpecialKey.F12) { 369 accelerator |= SWT.F12; 370 } else if (specialKey == SpecialKey.F2) { 371 accelerator |= SWT.F2; 372 } else if (specialKey == SpecialKey.F3) { 373 accelerator |= SWT.F3; 374 } else if (specialKey == SpecialKey.F4) { 375 accelerator |= SWT.F4; 376 } else if (specialKey == SpecialKey.F5) { 377 accelerator |= SWT.F5; 378 } else if (specialKey == SpecialKey.F6) { 379 accelerator |= SWT.F6; 380 } else if (specialKey == SpecialKey.F7) { 381 accelerator |= SWT.F7; 382 } else if (specialKey == SpecialKey.F8) { 383 accelerator |= SWT.F8; 384 } else if (specialKey == SpecialKey.F9) { 385 accelerator |= SWT.F9; 386 } else if (specialKey == SpecialKey.HOME) { 387 accelerator |= SWT.HOME; 388 } else if (specialKey == SpecialKey.INSERT) { 389 accelerator |= SWT.INSERT; 390 } else if (specialKey == SpecialKey.PAGE_DOWN) { 391 accelerator |= SWT.PAGE_DOWN; 392 } else if (specialKey == SpecialKey.PAGE_UP) { 393 accelerator |= SWT.PAGE_UP; 394 } 395 } 396 397 return accelerator; 398 } 399 400 private static final IKeyFormatter NATIVE_FORMATTER = new NativeKeyFormatter(); 401 402 409 public static IKeyFormatter getKeyFormatterForPlatform() { 410 return NATIVE_FORMATTER; 411 } 412 413 424 private static char topKey(Event event) { 425 char character = event.character; 426 boolean ctrlDown = (event.stateMask & SWT.CTRL) != 0; 427 428 if (ctrlDown && event.character != event.keyCode 429 && event.character < 0x20) { 430 character += 0x40; 431 } 432 433 return character; 434 } 435 436 444 private static int toUpperCase(int keyCode) { 445 if (keyCode > 0xFFFF) { 447 return keyCode; 448 } 449 450 char character = (char) keyCode; 452 return Character.isLetter(character) ? Character.toUpperCase(character) 453 : keyCode; 454 } 455 456 459 private SWTKeySupport() { 460 } 462 } 463 | Popular Tags |