| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| Keyboard |
|
| 1.25;1.25 |
| 1 | /******************************************************************************* | |
| 2 | * Copyright (c) 2009 Ketan Padegaonkar and others. | |
| 3 | * All rights reserved. This program and the accompanying materials | |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 | |
| 5 | * which accompanies this distribution, and is available at | |
| 6 | * http://www.eclipse.org/legal/epl-v10.html | |
| 7 | * | |
| 8 | * Contributors: | |
| 9 | * Ketan Padegaonkar - initial API and implementation | |
| 10 | *******************************************************************************/ | |
| 11 | package org.eclipse.swtbot.swt.finder.keyboard; | |
| 12 | ||
| 13 | import static org.eclipse.swtbot.swt.finder.utils.SWTUtils.sleep; | |
| 14 | ||
| 15 | import java.util.ArrayList; | |
| 16 | import java.util.Arrays; | |
| 17 | import java.util.Collections; | |
| 18 | import java.util.List; | |
| 19 | ||
| 20 | import org.apache.log4j.Logger; | |
| 21 | import org.eclipse.jface.bindings.keys.KeyStroke; | |
| 22 | import org.eclipse.swt.SWT; | |
| 23 | import org.eclipse.swtbot.swt.finder.utils.MessageFormat; | |
| 24 | import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences; | |
| 25 | ||
| 26 | /** | |
| 27 | * Represents a Keyboard. Allows for typing keys and pressing shortcuts. Pressing shortcuts is different from pressing | |
| 28 | * normal characters and 'special characters'. | |
| 29 | * <p> | |
| 30 | * <b>NOTE:</b> This class needs that a {@link KeyStroke} be split. This means that a single {@link KeyStroke} | |
| 31 | * representing a SHIFT+T needs to be split into two {@link KeyStroke}s, one representing a SHIFT and another | |
| 32 | * representing a 'T'. | |
| 33 | * </p> | |
| 34 | * <p> | |
| 35 | * <b>Shortcut:</b> CTRL+SHIFT+T for e.g. needs to press CTRL, SHIFT, T in that order while holding them down, and | |
| 36 | * release them in the order T, SHIFT, CTRL. | |
| 37 | * </p> | |
| 38 | * <p> | |
| 39 | * <b>Normal characters:</b> 't' requires that you type 'T'. 'T' requires that you type the shortcut SHIFT+T. | |
| 40 | * </p> | |
| 41 | * <p> | |
| 42 | * <b>Special characters:</b> On a US keyboard '#' requires that you type SHIFT+3. ':' requires you to type SHIFT+;. | |
| 43 | * </p> | |
| 44 | * | |
| 45 | * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com> | |
| 46 | * @version $Id$ | |
| 47 | * @see Keystrokes#toKeys(int, char) | |
| 48 | */ | |
| 49 | 1 | public class Keyboard { |
| 50 | ||
| 51 | 1 | private static final Logger log = Logger.getLogger(Keyboard.class); |
| 52 | ||
| 53 | private final KeyboardStrategy strategy; | |
| 54 | ||
| 55 | /** | |
| 56 | * Creates a new keyboard. | |
| 57 | */ | |
| 58 | Keyboard() { | |
| 59 | 0 | this(new AWTKeyboardStrategy()); |
| 60 | 0 | } |
| 61 | ||
| 62 | /** | |
| 63 | * Creates a new keyboard that uses the specified strategy to type on the keyboard. | |
| 64 | * | |
| 65 | * @param strategy the keyboard strategy. | |
| 66 | */ | |
| 67 | 4 | public Keyboard(KeyboardStrategy strategy) { |
| 68 | 4 | this.strategy = strategy; |
| 69 | 4 | } |
| 70 | ||
| 71 | /** | |
| 72 | * Types the string on the keyboard. | |
| 73 | * | |
| 74 | * @param text the text to type on the keyboard. | |
| 75 | */ | |
| 76 | public void typeText(String text) { | |
| 77 | 2 | typeText(text, SWTBotPreferences.TYPE_INTERVAL); |
| 78 | 2 | } |
| 79 | ||
| 80 | /** | |
| 81 | * Types the string on the keyboard. | |
| 82 | * | |
| 83 | * @param text the text to type on the keyboard. | |
| 84 | * @param interval the interval between the keystrokes. | |
| 85 | */ | |
| 86 | public void typeText(String text, int interval) { | |
| 87 | 4 | log.debug(MessageFormat.format("Typing text ''{0}'' with an interval of {1}ms. between characters.", text, interval)); |
| 88 | 72 | for (int i = 0; i < text.length(); i++) { |
| 89 | 68 | typeCharacter(text.charAt(i)); |
| 90 | 68 | sleep(interval); |
| 91 | } | |
| 92 | 4 | } |
| 93 | ||
| 94 | /** | |
| 95 | * Types the character on the keyboard. Note that the character may refer to multiple keystrokes. | |
| 96 | * | |
| 97 | * @param ch the character to type on the keyboard. | |
| 98 | */ | |
| 99 | public void typeCharacter(char ch) { | |
| 100 | 68 | pressShortcut(Keystrokes.create(ch)); |
| 101 | 68 | } |
| 102 | ||
| 103 | /** | |
| 104 | * Presses the shortcut specified by the given keys. | |
| 105 | * | |
| 106 | * @param modificationKeys the combination of {@link SWT#ALT} | {@link SWT#CTRL} | {@link SWT#SHIFT} | | |
| 107 | * {@link SWT#COMMAND}. | |
| 108 | * @param c the character | |
| 109 | * @see Keystrokes#toKeys(int, char) | |
| 110 | */ | |
| 111 | public void pressShortcut(int modificationKeys, char c) { | |
| 112 | 0 | pressShortcut(Keystrokes.toKeys(modificationKeys, c)); |
| 113 | 0 | } |
| 114 | ||
| 115 | /** | |
| 116 | * Presses the shortcut specified by the given keys. | |
| 117 | * | |
| 118 | * @param keys the keys to press | |
| 119 | * @see Keystrokes#toKeys(int, char) | |
| 120 | */ | |
| 121 | public void pressShortcut(KeyStroke... keys) { | |
| 122 | 68 | log.trace(MessageFormat.format("Pressing shortcut {0}", Arrays.asList(keys))); |
| 123 | 68 | pressKeys(keys); |
| 124 | 68 | releaseKeys(reverse(keys)); |
| 125 | 68 | } |
| 126 | ||
| 127 | /** | |
| 128 | * Presses the shortcut specified by the given keys. | |
| 129 | * | |
| 130 | * @param modificationKeys the combination of {@link SWT#ALT} | {@link SWT#CTRL} | {@link SWT#SHIFT} | | |
| 131 | * {@link SWT#COMMAND}. | |
| 132 | * @param keyCode the keyCode, these may be special keys like F1-F12, or navigation keys like HOME, PAGE_UP | |
| 133 | * @param c the character | |
| 134 | * @see Keystrokes#toKeys(int, char) | |
| 135 | */ | |
| 136 | public void pressShortcut(int modificationKeys, int keyCode, char c) { | |
| 137 | ||
| 138 | 0 | List<KeyStroke> keys = new ArrayList<KeyStroke>(Arrays.asList(Keystrokes.toKeys(modificationKeys, c))); |
| 139 | 0 | if (keyCode != 0) |
| 140 | 0 | addKeyCode(keyCode, c, keys); |
| 141 | ||
| 142 | 0 | pressShortcut(keys.toArray(new KeyStroke[0])); |
| 143 | ||
| 144 | 0 | } |
| 145 | ||
| 146 | private KeyStroke[] reverse(KeyStroke... keys) { | |
| 147 | 68 | ArrayList<KeyStroke> copy = new ArrayList<KeyStroke>(Arrays.asList(keys)); |
| 148 | 68 | Collections.reverse(copy); |
| 149 | 68 | return copy.toArray(new KeyStroke[0]); |
| 150 | } | |
| 151 | ||
| 152 | private void releaseKeys(KeyStroke... keys) { | |
| 153 | 68 | strategy.releaseKeys(keys); |
| 154 | 68 | } |
| 155 | ||
| 156 | private void pressKeys(KeyStroke... keys) { | |
| 157 | 68 | strategy.pressKeys(keys); |
| 158 | 68 | } |
| 159 | ||
| 160 | private void addKeyCode(int keyCode, char c, List<KeyStroke> keys) { | |
| 161 | 0 | if (c == 0) |
| 162 | 0 | keys.add(KeyStroke.getInstance(0, keyCode)); |
| 163 | else | |
| 164 | 0 | keys.add(keys.size() - 1, KeyStroke.getInstance(0, keyCode)); |
| 165 | 0 | } |
| 166 | ||
| 167 | } |