├── .gitattributes ├── Images └── binance.jpg ├── Keyboard.jar ├── LICENSE ├── README.md └── src ├── Keyboard.java └── PressAkeyCommand.java /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /Images/binance.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IDouble/Keyboard/a6204dc5369b723e011710c1a02b11ea335ae8fb/Images/binance.jpg -------------------------------------------------------------------------------- /Keyboard.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IDouble/Keyboard/a6204dc5369b723e011710c1a02b11ea335ae8fb/Keyboard.jar -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Alpay Yildirim 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ⌨️ Keyboard ⌨️ 2 | A Simple **Java Library** for controlling the **Keyboard**, it is: 3 | 4 | - **Easy** to use 5 | - **Powerful** 6 | - And has **many Functions** 7 | 8 | I searched long to find a simple **Keyboard Library** for **Java** but I have found nothing that fits, 9 | so I wrote a simple **Java Library** to access the **Keyboard**. 10 | 11 | ## 📝 Example 📝 12 | 13 | ([**As an example, I used it to program a game controller with Arduino (JArduino).**](https://github.com/AYIDouble/IOT-Arduino-Game-Controller-Java)) 14 | 15 | ``` 16 | import java.awt.AWTException; 17 | import java.util.ArrayList; 18 | 19 | import com.sun.glass.events.KeyEvent; 20 | 21 | import Keyboard.Keyboard; 22 | 23 | public class Main { 24 | 25 | static Keyboard keyboard; 26 | 27 | public static void main(String[] args) throws AWTException, InterruptedException { 28 | keyboard = new Keyboard(); 29 | ArrayList keyyy = new ArrayList(); 30 | keyboard.type("Cool, "); 31 | keyboard.type("I can write this in just one line"); 32 | keyboard.press(KeyEvent.VK_WINDOWS); 33 | } 34 | } 35 | ``` 36 | ![Binance Ready to give crypto a try ? buy bitcoin and other cryptocurrencies on binance](Images/binance.jpg) 37 | -------------------------------------------------------------------------------- /src/Keyboard.java: -------------------------------------------------------------------------------- 1 | package Keyboard; 2 | 3 | import java.awt.AWTException; 4 | import java.awt.Robot; 5 | import java.awt.event.KeyEvent; 6 | import java.util.ArrayList; 7 | 8 | /** 9 | * A simple Library for controlling the Keyboard in Java. 10 | * License : Mozilla Public License 2.0 / http://choosealicense.com/licenses/mpl-2.0/# 11 | * 12 | * @author AYIDouble / https://github.com/AYIDouble 13 | * @version 1.0 14 | * @since 22.02.2016 15 | */ 16 | 17 | public class Keyboard { 18 | /** 19 | * you can use the methods of the robot and access his attributes 20 | */ 21 | public Robot robot; //public for adjustments on the robot 22 | 23 | /** 24 | * robot of the class will be created at initalize 25 | */ 26 | public Keyboard() throws AWTException{ 27 | robot = new Robot(); 28 | } 29 | 30 | /** 31 | * initalize Keyboard with your own robot 32 | * Otherwise just initalize it with the no parameters 33 | * 34 | * @param robot - use the robot of you class 35 | */ 36 | public Keyboard(Robot robot) { 37 | this.robot = robot; 38 | } 39 | 40 | /** 41 | * @param key - write the Charcode of the Key you want to press, example "p" : 80 42 | */ 43 | public void press(char key){ 44 | robot.keyPress(KeyEvent.getExtendedKeyCodeForChar(key)); 45 | robot.keyRelease(KeyEvent.getExtendedKeyCodeForChar(key)); 46 | } 47 | 48 | /** 49 | * @param text - write the Keys you want to be pressed, example : "hello" 50 | */ 51 | public void press(String text){ 52 | char[] keys = text.toCharArray(); 53 | for (int i = 0; i < keys.length; i++) { 54 | type(keys[i]); 55 | } 56 | } 57 | 58 | /** 59 | * Write a text , it's possible to Write Uppercase 60 | * 61 | * Example : "Hello" : 5 type 'H',wait 5 milliseconds,type 'E',wait 5 milliseconds ... 62 | * 63 | * @param text - write the Keys you want to be pressed, example : "hello" 64 | * @param speed - milliseconds before writing the next Key, example : 5 65 | */ 66 | public void press(String text,long speed) throws InterruptedException{ // you can define how fast the text should be typed [speed = milliseconds][1000 milliseconds = 1 second] 67 | char[] keys = text.toCharArray(); 68 | for (int i = 0; i < keys.length; i++) { 69 | type(keys[i]); 70 | Thread.sleep(speed); 71 | } 72 | } 73 | 74 | /** 75 | * Press Keys and keep the Keys pressed,you can use release() for releasing the Keys 76 | * 77 | * @param key - write the Charcode of the Key you want to be pressed, example "p" : 80 78 | */ 79 | public void press(int key){ 80 | robot.keyPress(key); 81 | robot.keyRelease(key); 82 | } 83 | /** 84 | * Press Keys and release them example Windows Key + O , to change Monitor Settings. 85 | * 86 | * @param keys - write the Charcodes of the Key you want to be pressed, example {KeyEvent.VK_WINDOWS,KeyEvent.VK_O} 87 | * @throws AWTException 88 | */ 89 | public void pressRelease(ArrayList keys) throws AWTException{ 90 | for (int i = 0; i < keys.size(); i++) { 91 | System.out.println(keys.get(i)); 92 | executePress(keys.get(i)); 93 | } 94 | } 95 | 96 | /** 97 | * Press Keys and release them example Windows Key + O , to change Monitor Settings. 98 | * 99 | * @param keys - write the Charcodes of the Key you want to be pressed, example {KeyEvent.VK_WINDOWS,KeyEvent.VK_O} 100 | * @param time - How many many milliseconds should the keys be pressed 1000 = 1 second 101 | * @throws InterruptedException 102 | * @throws AWTException 103 | */ 104 | public void pressRelease(ArrayList keys,long time) throws InterruptedException, AWTException{ 105 | for (int i = 0; i < keys.size(); i++) { 106 | System.out.println(keys.get(i)); 107 | executePress(keys.get(i),time); 108 | } 109 | } 110 | 111 | /** 112 | * This is good if you want to type a lowercase text 113 | * Otherwise use type() 114 | * 115 | * @param text - write the text you want to be typed in lowercase, example : "hello" 116 | */ 117 | public void lowerCasePress(String text){ 118 | char[] keys = text.toCharArray(); 119 | for (int i = 0; i < keys.length; i++) { 120 | robot.keyPress(KeyEvent.getExtendedKeyCodeForChar(keys[i])); 121 | robot.keyRelease(KeyEvent.getExtendedKeyCodeForChar(keys[i])); 122 | } 123 | } 124 | 125 | /** 126 | * Press Key and keep the Key pressed,you can use release() for releasing the Key 127 | * 128 | * @param key - write the Key you want to be pressed, example : 'a' 129 | */ 130 | public void keepPressed(char key){ 131 | robot.keyPress(KeyEvent.getExtendedKeyCodeForChar(key)); 132 | } 133 | 134 | /** 135 | * Press Keys and keep the Keys pressed,you can use release() for releasing the Keys 136 | * 137 | * @param text - write the Key you want to be pressed, example : "aswd" 138 | */ 139 | public void keepPressed(String text){ 140 | char[] keys = text.toCharArray(); 141 | for (int i = 0; i < keys.length; i++) { 142 | robot.keyPress(KeyEvent.getExtendedKeyCodeForChar(keys[i])); 143 | } 144 | } 145 | 146 | /** 147 | * Press Key and keep the Key pressed,you can use release() for releasing the Keys 148 | * 149 | * @param key - write the Key you want to be pressed, example "p" : 80 150 | */ 151 | public void keepPressed(int key){ 152 | robot.keyPress(key); 153 | } 154 | 155 | /** 156 | * hold these Keys down, with speed attribute that means it will wait x milliseconds before it keep pressed the other Key. 157 | * 158 | * Example : "Hello" : 5 keep Pressed 'H',wait 5 milliseconds,keep Pressed 'E',wait 5 milliseconds ... 159 | * 160 | * @param text - write the Keys you want to be keep pressed, example : "hello" 161 | * @param speed - milliseconds before keeping pressed the next Key, example : 5 162 | */ 163 | public void keepPressed(String text,long speed) throws InterruptedException{ // you can define how fast the text should be typed [speed = milliseconds][1000 milliseconds = 1 second] 164 | char[] keys = text.toCharArray(); 165 | for (int i = 0; i < keys.length; i++) { 166 | robot.keyPress(KeyEvent.getExtendedKeyCodeForChar(keys[i])); 167 | Thread.sleep(speed); 168 | } 169 | } 170 | 171 | /** 172 | * use release() for releasing the Keys 173 | * 174 | * @param key - write the Key you want to be released, example "p" : 'p' 175 | */ 176 | public void release(char key){ 177 | robot.keyRelease(KeyEvent.getExtendedKeyCodeForChar(key)); 178 | } 179 | 180 | /** 181 | * use release() for releasing the Keys 182 | * 183 | * @param text - write the Text you want to be released, example "hello", the Keys will be released when they were pressed 184 | */ 185 | public void release(String text){ 186 | char[] keys = text.toCharArray(); 187 | for (int i = 0; i < keys.length; i++) { 188 | robot.keyRelease(KeyEvent.getExtendedKeyCodeForChar(keys[i])); 189 | } 190 | } 191 | 192 | /** 193 | * use release() for releasing the Keys 194 | * 195 | * @param key - write the Key you want to be released, example "p" : 80 196 | */ 197 | public void release(int key){ 198 | robot.keyRelease(key); 199 | } 200 | 201 | /** 202 | * release these Keys, with speed attribute that means it will wait x milliseconds before it releases the other Key. 203 | * 204 | * Example : "Hello" : 5 release 'H',wait 5 milliseconds,release 'E',wait 5 milliseconds ... 205 | * 206 | * @param text - write the Keys you want to be keep pressed, example : "hello" 207 | * @param speed - milliseconds before releasing the next Key, example : 5 208 | * @throws InterruptedException 209 | */ 210 | public void release(String text,long speed) throws InterruptedException{ // you can define how fast the text should be typed [speed = milliseconds][1000 milliseconds = 1 second] 211 | char[] keys = text.toCharArray(); 212 | for (int i = 0; i < keys.length; i++) { 213 | robot.keyPress(KeyEvent.getExtendedKeyCodeForChar(keys[i])); 214 | Thread.sleep(speed); 215 | } 216 | } 217 | 218 | /** 219 | * Important! This will work with uppercase Text and you should use it if you want to type a uppercase Text. 220 | * 221 | * @param character - write the Key or Text you want to be typed, example "Type This" or 'A' 222 | */ 223 | public void type(char character) { 224 | switch (character) { 225 | case 'a': doType(KeyEvent.VK_A); break; 226 | case 'b': doType(KeyEvent.VK_B); break; 227 | case 'c': doType(KeyEvent.VK_C); break; 228 | case 'd': doType(KeyEvent.VK_D); break; 229 | case 'e': doType(KeyEvent.VK_E); break; 230 | case 'f': doType(KeyEvent.VK_F); break; 231 | case 'g': doType(KeyEvent.VK_G); break; 232 | case 'h': doType(KeyEvent.VK_H); break; 233 | case 'i': doType(KeyEvent.VK_I); break; 234 | case 'j': doType(KeyEvent.VK_J); break; 235 | case 'k': doType(KeyEvent.VK_K); break; 236 | case 'l': doType(KeyEvent.VK_L); break; 237 | case 'm': doType(KeyEvent.VK_M); break; 238 | case 'n': doType(KeyEvent.VK_N); break; 239 | case 'o': doType(KeyEvent.VK_O); break; 240 | case 'p': doType(KeyEvent.VK_P); break; 241 | case 'q': doType(KeyEvent.VK_Q); break; 242 | case 'r': doType(KeyEvent.VK_R); break; 243 | case 's': doType(KeyEvent.VK_S); break; 244 | case 't': doType(KeyEvent.VK_T); break; 245 | case 'u': doType(KeyEvent.VK_U); break; 246 | case 'v': doType(KeyEvent.VK_V); break; 247 | case 'w': doType(KeyEvent.VK_W); break; 248 | case 'x': doType(KeyEvent.VK_X); break; 249 | case 'y': doType(KeyEvent.VK_Y); break; 250 | case 'z': doType(KeyEvent.VK_Z); break; 251 | case 'A': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_A); break; 252 | case 'B': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_B); break; 253 | case 'C': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_C); break; 254 | case 'D': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_D); break; 255 | case 'E': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_E); break; 256 | case 'F': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_F); break; 257 | case 'G': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_G); break; 258 | case 'H': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_H); break; 259 | case 'I': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_I); break; 260 | case 'J': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_J); break; 261 | case 'K': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_K); break; 262 | case 'L': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_L); break; 263 | case 'M': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_M); break; 264 | case 'N': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_N); break; 265 | case 'O': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_O); break; 266 | case 'P': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_P); break; 267 | case 'Q': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_Q); break; 268 | case 'R': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_R); break; 269 | case 'S': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_S); break; 270 | case 'T': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_T); break; 271 | case 'U': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_U); break; 272 | case 'V': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_V); break; 273 | case 'W': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_W); break; 274 | case 'X': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_X); break; 275 | case 'Y': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_Y); break; 276 | case 'Z': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_Z); break; 277 | case '`': doType(KeyEvent.VK_BACK_QUOTE); break; 278 | case '0': doType(KeyEvent.VK_0); break; 279 | case '1': doType(KeyEvent.VK_1); break; 280 | case '2': doType(KeyEvent.VK_2); break; 281 | case '3': doType(KeyEvent.VK_3); break; 282 | case '4': doType(KeyEvent.VK_4); break; 283 | case '5': doType(KeyEvent.VK_5); break; 284 | case '6': doType(KeyEvent.VK_6); break; 285 | case '7': doType(KeyEvent.VK_7); break; 286 | case '8': doType(KeyEvent.VK_8); break; 287 | case '9': doType(KeyEvent.VK_9); break; 288 | case '-': doType(KeyEvent.VK_MINUS); break; 289 | case '=': doType(KeyEvent.VK_EQUALS); break; 290 | case '~': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_BACK_QUOTE); break; 291 | case '!': doType(KeyEvent.VK_EXCLAMATION_MARK); break; 292 | case '@': doType(KeyEvent.VK_AT); break; 293 | case '#': doType(KeyEvent.VK_NUMBER_SIGN); break; 294 | case '$': doType(KeyEvent.VK_DOLLAR); break; 295 | case '%': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_5); break; 296 | case '^': doType(KeyEvent.VK_CIRCUMFLEX); break; 297 | case '&': doType(KeyEvent.VK_AMPERSAND); break; 298 | case '*': doType(KeyEvent.VK_ASTERISK); break; 299 | case '(': doType(KeyEvent.VK_LEFT_PARENTHESIS); break; 300 | case ')': doType(KeyEvent.VK_RIGHT_PARENTHESIS); break; 301 | case '_': doType(KeyEvent.VK_UNDERSCORE); break; 302 | case '+': doType(KeyEvent.VK_PLUS); break; 303 | case '\t': doType(KeyEvent.VK_TAB); break; 304 | case '\n': doType(KeyEvent.VK_ENTER); break; 305 | case '[': doType(KeyEvent.VK_OPEN_BRACKET); break; 306 | case ']': doType(KeyEvent.VK_CLOSE_BRACKET); break; 307 | case '\\': doType(KeyEvent.VK_BACK_SLASH); break; 308 | case '{': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_OPEN_BRACKET); break; 309 | case '}': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_CLOSE_BRACKET); break; 310 | case '|': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_BACK_SLASH); break; 311 | case ';': doType(KeyEvent.VK_SEMICOLON); break; 312 | case ':': doType(KeyEvent.VK_COLON); break; 313 | case '\'': doType(KeyEvent.VK_QUOTE); break; 314 | case '"': doType(KeyEvent.VK_QUOTEDBL); break; 315 | case ',': doType(KeyEvent.VK_COMMA); break; 316 | case '<': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_COMMA); break; 317 | case '.': doType(KeyEvent.VK_PERIOD); break; 318 | case '>': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_PERIOD); break; 319 | case '/': doType(KeyEvent.VK_SLASH); break; 320 | case '?': doType(KeyEvent.VK_SHIFT, KeyEvent.VK_SLASH); break; 321 | case ' ': doType(KeyEvent.VK_SPACE); break; 322 | default: 323 | try{ 324 | robot.keyPress(KeyEvent.getExtendedKeyCodeForChar(character)); 325 | }catch(Exception e){ 326 | throw new IllegalArgumentException("Cannot type character " + character); 327 | } 328 | } 329 | } 330 | 331 | /** 332 | * Important! This will work with uppercase Text and you should use it if you want to type a uppercase Text. 333 | * 334 | * @param characters - write the Key or Text you want to be typed, example "Type This" or 'A' 335 | */ 336 | public void type(CharSequence characters) { 337 | int length = characters.length(); 338 | for (int i = 0; i < length; i++) { 339 | char character = characters.charAt(i); 340 | type(character); 341 | } 342 | } 343 | 344 | private void doType(int... keyCodes) { 345 | doType(keyCodes, 0, keyCodes.length); 346 | } 347 | 348 | private void doType(int[] keyCodes, int offset, int length) { 349 | if (length == 0) { 350 | return; 351 | }else{ 352 | robot.keyPress(keyCodes[offset]); 353 | doType(keyCodes, offset + 1, length - 1); 354 | robot.keyRelease(keyCodes[offset]); 355 | } 356 | } 357 | 358 | //Presses a key for a amount of time 359 | 360 | private void executePress(int keyEvent,long duration) throws AWTException { 361 | PressAKeyCommand pakc = new PressAKeyCommand(keyEvent,duration); 362 | pakc.execute(); 363 | } 364 | 365 | private void executePress(int keyEvent) throws AWTException { 366 | PressAKeyCommand pakc = new PressAKeyCommand(keyEvent,10); 367 | pakc.execute(); 368 | pakc.stop(); 369 | } 370 | } 371 | -------------------------------------------------------------------------------- /src/PressAkeyCommand.java: -------------------------------------------------------------------------------- 1 | package Keyboard; 2 | 3 | import java.awt.AWTException; 4 | import java.awt.Robot; 5 | 6 | /** 7 | * A simple Library for controlling the Keyboard in Java. 8 | * License : Mozilla Public License 2.0 / http://choosealicense.com/licenses/mpl-2.0/# 9 | * 10 | * @author AYIDouble / https://github.com/AYIDouble 11 | * @version 1.0 12 | * @since 22.02.2016 13 | */ 14 | 15 | class PressAKeyCommand{ 16 | 17 | public PressAKeyCommand(int key){ 18 | this.key = key; 19 | } 20 | 21 | public PressAKeyCommand(int key,long time){ 22 | this.key = key; 23 | this.time = time; 24 | } 25 | 26 | private int key; 27 | private long time = 0; 28 | private volatile boolean isContinue = true; 29 | 30 | public void execute() { 31 | try { 32 | Robot robot = new Robot(); 33 | if(time == 0){ 34 | while (isContinue) { 35 | robot.keyPress(key); 36 | System.out.println(isContinue); 37 | } 38 | robot.keyRelease(key); 39 | } 40 | else{ 41 | long curTime = System.currentTimeMillis(); 42 | while (isContinue) { 43 | while (System.currentTimeMillis() - curTime < time) { 44 | robot.keyPress(key); 45 | } 46 | stop(); 47 | } 48 | robot.keyRelease(key); 49 | } 50 | } catch (AWTException ex) { 51 | ex.printStackTrace(); 52 | } 53 | } 54 | 55 | public void stop() { 56 | isContinue = false; 57 | } 58 | 59 | } --------------------------------------------------------------------------------