| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
package org.eclipse.swtbot.swt.finder.keyboard; |
| 12 | |
|
| 13 | |
import java.util.HashSet; |
| 14 | |
import java.util.Set; |
| 15 | |
|
| 16 | |
import org.eclipse.jface.bindings.keys.KeyStroke; |
| 17 | |
import org.eclipse.swt.SWT; |
| 18 | |
import org.eclipse.swt.widgets.Display; |
| 19 | |
import org.eclipse.swt.widgets.Event; |
| 20 | |
import org.eclipse.swtbot.swt.finder.utils.SWTUtils; |
| 21 | |
import org.eclipse.swtbot.swt.finder.utils.internal.Assert; |
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | 1 | class SWTKeyboardStrategy extends AbstractKeyboardStrategy { |
| 31 | |
|
| 32 | |
private final Display display; |
| 33 | |
|
| 34 | 1 | private static final Set<Integer> specialKeys = new HashSet<Integer>(); |
| 35 | |
|
| 36 | 51 | SWTKeyboardStrategy() { |
| 37 | 51 | this.display = SWTUtils.display(); |
| 38 | 51 | } |
| 39 | |
|
| 40 | |
public void pressKey(KeyStroke key) { |
| 41 | 50 | Assert.isTrue(display.post(keyEvent(key, SWT.KeyDown)), "Could not post keyevent."); |
| 42 | 50 | display.wake(); |
| 43 | 50 | } |
| 44 | |
|
| 45 | |
public void releaseKey(KeyStroke key) { |
| 46 | 50 | Assert.isTrue(display.post(keyEvent(key, SWT.KeyUp)), "Could not post keyevent."); |
| 47 | 50 | display.wake(); |
| 48 | 50 | } |
| 49 | |
|
| 50 | |
private Event keyEvent(KeyStroke key, int type) { |
| 51 | 100 | Event e = new Event(); |
| 52 | 100 | e.type = type; |
| 53 | 100 | e.keyCode = keycode(key); |
| 54 | 100 | e.character = character(key); |
| 55 | 100 | return e; |
| 56 | |
} |
| 57 | |
|
| 58 | |
private char character(KeyStroke key) { |
| 59 | 100 | int naturalKey = key.getNaturalKey(); |
| 60 | 100 | if (specialKeys.contains(naturalKey)) |
| 61 | 12 | return 0; |
| 62 | 88 | return (char) naturalKey; |
| 63 | |
} |
| 64 | |
|
| 65 | |
private int keycode(KeyStroke key) { |
| 66 | 100 | int naturalKey = key.getNaturalKey(); |
| 67 | 100 | int modifierKeys = key.getModifierKeys(); |
| 68 | |
|
| 69 | 100 | if (modifierKeys != 0) |
| 70 | 80 | return modifierKeys; |
| 71 | |
|
| 72 | 20 | if (specialKeys.contains(naturalKey)) |
| 73 | 12 | return naturalKey; |
| 74 | 8 | return 0; |
| 75 | |
} |
| 76 | |
|
| 77 | |
|
| 78 | |
static { |
| 79 | |
|
| 80 | 1 | addSpecialKeyMapping(SWT.F1); |
| 81 | 1 | addSpecialKeyMapping(SWT.F2); |
| 82 | 1 | addSpecialKeyMapping(SWT.F3); |
| 83 | 1 | addSpecialKeyMapping(SWT.F4); |
| 84 | 1 | addSpecialKeyMapping(SWT.F5); |
| 85 | 1 | addSpecialKeyMapping(SWT.F6); |
| 86 | 1 | addSpecialKeyMapping(SWT.F7); |
| 87 | 1 | addSpecialKeyMapping(SWT.F8); |
| 88 | 1 | addSpecialKeyMapping(SWT.F9); |
| 89 | 1 | addSpecialKeyMapping(SWT.F10); |
| 90 | 1 | addSpecialKeyMapping(SWT.F11); |
| 91 | 1 | addSpecialKeyMapping(SWT.F12); |
| 92 | |
|
| 93 | 1 | addSpecialKeyMapping(SWT.DEL); |
| 94 | |
|
| 95 | |
|
| 96 | 1 | addSpecialKeyMapping(SWT.HOME); |
| 97 | 1 | addSpecialKeyMapping(SWT.END); |
| 98 | 1 | addSpecialKeyMapping(SWT.PAGE_UP); |
| 99 | 1 | addSpecialKeyMapping(SWT.PAGE_DOWN); |
| 100 | 1 | addSpecialKeyMapping(SWT.ARROW_RIGHT); |
| 101 | 1 | addSpecialKeyMapping(SWT.ARROW_DOWN); |
| 102 | 1 | addSpecialKeyMapping(SWT.ARROW_LEFT); |
| 103 | 1 | addSpecialKeyMapping(SWT.ARROW_UP); |
| 104 | |
|
| 105 | |
|
| 106 | 1 | addSpecialKeyMapping(SWT.BS); |
| 107 | 1 | addSpecialKeyMapping(SWT.CR); |
| 108 | 1 | addSpecialKeyMapping(SWT.DEL); |
| 109 | 1 | addSpecialKeyMapping(SWT.ESC); |
| 110 | 1 | addSpecialKeyMapping(SWT.LF); |
| 111 | 1 | addSpecialKeyMapping(SWT.TAB); |
| 112 | |
} |
| 113 | |
|
| 114 | |
private static void addSpecialKeyMapping(int swtKey) { |
| 115 | 27 | specialKeys.add(swtKey); |
| 116 | 27 | } |
| 117 | |
|
| 118 | |
} |