├── .gitattributes ├── Images └── binance.jpg ├── LICENSE ├── Mouse.jar ├── README.md └── src └── Mouse.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/Mouse/2fb7b7eeb22b9433ca430374a5f8b66daeb5b495/Images/binance.jpg -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /Mouse.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IDouble/Mouse/2fb7b7eeb22b9433ca430374a5f8b66daeb5b495/Mouse.jar -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 🖱 Mouse 🖱 2 | A Simple **Java Library** for controlling the **Mouse**, it is: 3 | 4 | - **Easy** to use 5 | - **Powerful** 6 | - **Works on multiple Monitors** 7 | - And has **many Functions** 8 | 9 | I searched long to find a simple **Mouse Library** for **Java** but I have found nothing that fits, 10 | so I wrote a simple **Java Library** to access the **Mouse**. 11 | 12 | ## 📝 Example 📝 13 | 14 | ([**As an example, I used it to program a game controller with Arduino (JArduino).**](https://github.com/AYIDouble/IOT-Arduino-Game-Controller-Java)) 15 | 16 | ``` 17 | import java.awt.AWTException; 18 | 19 | import Mouse.Mouse; 20 | 21 | public class Main { 22 | 23 | private static Mouse mouse; 24 | 25 | public static void main(String [] args) throws Exception { 26 | mouse = new Mouse(); 27 | mouse.animateMove(100, 150, 1); 28 | mouse.click(); 29 | } 30 | 31 | } 32 | ``` 33 | ![Binance Ready to give crypto a try ? buy bitcoin and other cryptocurrencies on binance](Images/binance.jpg) 34 | -------------------------------------------------------------------------------- /src/Mouse.java: -------------------------------------------------------------------------------- 1 | package Mouse; 2 | 3 | /** 4 | * A simple Library for controlling the Mouse in Java. 5 | * License : Mozilla Public License 2.0 / http://choosealicense.com/licenses/mpl-2.0/# 6 | * 7 | * @author AYIDouble 8 | * @version 1.1 9 | * @since 26.06.2016 10 | */ 11 | 12 | import java.awt.AWTException; 13 | import java.awt.Color; 14 | import java.awt.GraphicsDevice; 15 | import java.awt.GraphicsEnvironment; 16 | import java.awt.MouseInfo; 17 | import java.awt.Robot; 18 | import java.awt.event.InputEvent; 19 | 20 | public class Mouse { 21 | 22 | private GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); // Get Screen Height & Width 23 | private final int MAX_Y = gd.getDisplayMode().getHeight(); // Gets the Monitor Height & Width (Works with multiple Monitors) 24 | private final int MAX_X = gd.getDisplayMode().getWidth(); 25 | private int mouseX = MouseInfo.getPointerInfo().getLocation().x; // Gets the x Coordinate of the Mouse 26 | private int mouseY = MouseInfo.getPointerInfo().getLocation().y; // Gets the y Coordinate of the Mouse 27 | 28 | /** 29 | * you can use the methods of the robot and access his attributes 30 | */ 31 | public Robot robot; //public for adjustments on the robot 32 | 33 | /** 34 | * robot of the class will be created at initialize 35 | */ 36 | public Mouse() throws AWTException{ 37 | robot = new Robot(); 38 | } 39 | 40 | /** 41 | * Initialize Keyboard with your own robot 42 | * Otherwise just initialize it with the no parameters 43 | * 44 | * @param robot - use the robot of you class 45 | */ 46 | public Mouse(Robot robot){ 47 | this.robot = robot; 48 | } 49 | 50 | /** 51 | * This will refresh the Mouse Position and save it to the variables mouseX and mouseY , you can access them with the getter methods. 52 | */ 53 | public void refreshMouseInfo(){ //updates the Actual Position of the Mouse 54 | mouseX = MouseInfo.getPointerInfo().getLocation().x; 55 | mouseY = MouseInfo.getPointerInfo().getLocation().y; 56 | } 57 | 58 | /** 59 | * Moves the mouse to the position. You can see what the maximum of your monitor/s is in the MAX_Y and MAX_X variables. 60 | * 61 | * @param x , where the Mouse should move 62 | * @param y , where the Mouse should move 63 | * @throws AWTException the AWT exception 64 | */ 65 | public void move(int x,int y) throws AWTException{ //Moves the Mouse to the position 66 | refreshMouseInfo(); 67 | robot.mouseMove(x,y); 68 | } 69 | 70 | //Moving Mouse in Directions 71 | 72 | /** 73 | * Moves the mouse 1 pixel up 74 | */ 75 | public void moveUp(){ //Moves the Mouse to the position 76 | refreshMouseInfo(); 77 | robot.mouseMove(mouseX,--mouseY ); 78 | } 79 | 80 | /** 81 | * Moves the mouse 1 pixel down 82 | */ 83 | public void moveDown(){ //Moves the Mouse to the position 84 | refreshMouseInfo(); 85 | robot.mouseMove(mouseX,++mouseY ); 86 | } 87 | 88 | /** 89 | * Moves the mouse 1 pixel to the left 90 | */ 91 | public void moveLeft(){ //Moves the Mouse to the position 92 | refreshMouseInfo(); 93 | robot.mouseMove(--mouseX,mouseY ); 94 | } 95 | 96 | /** 97 | * Moves the mouse 1 pixel to the right 98 | */ 99 | public void moveRight(){ //Moves the Mouse to the position 100 | refreshMouseInfo(); 101 | robot.mouseMove(++mouseX,mouseY ); 102 | } 103 | 104 | //Moving Mouse in Directions with adjustable Mouse Speed 105 | 106 | /** 107 | * Moves the mouse x pixel up 108 | * @param x , x stands for how many pixel to move 109 | */ 110 | public void moveUp(Integer x){ //Moves the Mouse to the position 111 | refreshMouseInfo(); 112 | robot.mouseMove(mouseX,mouseY -= x ); 113 | } 114 | 115 | /** 116 | * Moves the mouse x pixel down 117 | * @param x , x stands for how many pixel to move 118 | */ 119 | public void moveDown(Integer x){ //Moves the Mouse to the position 120 | refreshMouseInfo(); 121 | robot.mouseMove(mouseX,mouseY += x ); 122 | } 123 | 124 | /** 125 | * Moves the mouse x pixel to the left 126 | * @param x , x stands for how many pixel to move 127 | */ 128 | public void moveLeft(Integer x){ //Moves the Mouse to the position 129 | refreshMouseInfo(); 130 | robot.mouseMove(mouseX -= x,mouseY ); 131 | } 132 | 133 | /** 134 | * Moves the mouse x pixel to the right 135 | * @param x , x stands for how many pixel to move 136 | */ 137 | public void moveRight(Integer x){ //Moves the Mouse to the position 138 | refreshMouseInfo(); 139 | robot.mouseMove(mouseX += x,mouseY ); 140 | } 141 | 142 | //Mouse Left Click 143 | 144 | /** 145 | * performs a left click 146 | */ 147 | public void click(){ 148 | robot.mousePress(InputEvent.BUTTON1_MASK); 149 | robot.mouseRelease(InputEvent.BUTTON1_MASK); 150 | } 151 | 152 | /** 153 | * performs x left clicks 154 | * 155 | * @param repeat , does x left clicks. 156 | * @throws Exception the exception 157 | */ 158 | public void click(int repeat) throws Exception{ //multiple clicks 159 | if(repeat > 0){ 160 | for (int i = 0; i < repeat; i++) { 161 | robot.mousePress(InputEvent.BUTTON1_MASK); 162 | robot.mouseRelease(InputEvent.BUTTON1_MASK); 163 | } 164 | }else{ 165 | throw new Exception("repeat can't be under 0"); 166 | } 167 | } 168 | 169 | //Mouse double left Click 170 | 171 | /** 172 | * performs a double left Click 173 | */ 174 | public void doubleClick(){ 175 | robot.mousePress(InputEvent.BUTTON1_MASK); 176 | robot.mouseRelease(InputEvent.BUTTON1_MASK); 177 | robot.mousePress(InputEvent.BUTTON1_MASK); 178 | robot.mouseRelease(InputEvent.BUTTON1_MASK); 179 | } 180 | 181 | // Press & Release (example: if you want to mark a Text) 182 | 183 | /** 184 | * keeps the mouse pressed (example: if you want to mark a Text) , look at release() 185 | */ 186 | public void press(){ 187 | robot.mousePress(InputEvent.BUTTON1_MASK); 188 | } 189 | /** 190 | * releases the mouse (example: if you want to mark a Text) , look at press() 191 | */ 192 | public void release(){ 193 | robot.mouseRelease(InputEvent.BUTTON1_MASK); 194 | } 195 | 196 | //Mouse Wheel 197 | 198 | /** 199 | * moves mouseWheel 1 down 200 | */ 201 | public void mouseWheelDown(){ 202 | robot.mouseWheel(1); 203 | } 204 | /** 205 | * moves mouseWheel 1 up 206 | */ 207 | public void mouseWheelUp(){ 208 | robot.mouseWheel(-1); 209 | } 210 | 211 | //Mouse Wheel (int move: -10 for up , 10 for down) 212 | 213 | /** 214 | * Mouse wheel. 215 | * 216 | * @param move , Move up with negative value ex: -3, Move down with positive value ex: 3 217 | */ 218 | public void mouseWheel(int move){ 219 | robot.mouseWheel(move); 220 | } 221 | 222 | //Mouse Wheel Click 223 | 224 | /** 225 | * performs a mouse wheel click 226 | */ 227 | public void mouseWheelClick(){ 228 | robot.mousePress(InputEvent.BUTTON2_MASK); 229 | robot.mouseRelease(InputEvent.BUTTON2_MASK); 230 | } 231 | 232 | //Mouse Right Click 233 | 234 | /** 235 | * performs a right click 236 | */ 237 | public void rightClick(){ 238 | robot.mousePress(InputEvent.BUTTON3_MASK); 239 | robot.mouseRelease(InputEvent.BUTTON3_MASK); 240 | } 241 | 242 | /** 243 | * Animates a Mouse move 244 | * 245 | * @param x , where the Mouse should move 246 | * @param y , where the Mouse should move 247 | * @param speed , the milliseconds between the next move 248 | * @throws InterruptedException the interrupted exception 249 | */ 250 | public void animateMove(int x,int y,int speed) throws InterruptedException{ //Makes a Mouse animation to the Point 251 | refreshMouseInfo(); 252 | 253 | while(x != mouseX || y != mouseY){ 254 | Thread.sleep(speed); 255 | refreshMouseInfo(); 256 | if(mouseX > x){ 257 | moveLeft(); 258 | }else if(mouseX < x){ 259 | moveRight(); 260 | }else if(mouseY < y){ 261 | moveDown(); 262 | }else if(mouseY > y){ 263 | moveUp(); 264 | } 265 | } 266 | } 267 | 268 | /** 269 | * Animate move. 270 | * 271 | * @param x , where the Mouse should move 272 | * @param y , where the Mouse should move 273 | * @param speed , the milliseconds between the next move 274 | * @param step , length of a step in pixels 275 | * @throws InterruptedException the interrupted exception 276 | */ 277 | public void animateMove(int x,int y,int speed,int step) throws InterruptedException{ //Makes a Mouse animation to the Point 278 | refreshMouseInfo(); 279 | 280 | while(x != mouseX || y != mouseY){ 281 | Thread.sleep(speed); 282 | refreshMouseInfo(); 283 | if(mouseX > x){ 284 | moveLeft(step); 285 | }else if(mouseX < x){ 286 | moveRight(step); 287 | }else if(mouseY < y){ 288 | moveDown(step); 289 | }else if(mouseY > y){ 290 | moveUp(step); 291 | } 292 | } 293 | } 294 | 295 | //Getters for Monitor Coordinates 296 | 297 | /** 298 | * MAX_Y is the maximal Y Value of the Monitor or the Monitors. 299 | * 300 | * @return the MAX_Y of the Monitor or Monitors 301 | */ 302 | public int getMAX_Y() { 303 | return MAX_Y; 304 | } 305 | 306 | /** 307 | * MAX_Y is the maximal X Value of the Monitor or the Monitors. 308 | * 309 | * @return the MAX_X of the Monitor or Monitors 310 | */ 311 | public int getMAX_X() { 312 | return MAX_X; 313 | } 314 | 315 | //Getters for Mouse Coordinates 316 | 317 | /** 318 | * Gets the mouse x position. 319 | * 320 | * @return the mouse x position 321 | */ 322 | public int getMouseX() { 323 | refreshMouseInfo(); 324 | return mouseX; 325 | } 326 | 327 | /** 328 | * Gets the mouse y position. 329 | * 330 | * @return the mouse y position 331 | */ 332 | public int getMouseY() { 333 | refreshMouseInfo(); 334 | return mouseY; 335 | } 336 | 337 | //Gets the color of the pixel, where your Mouse is. (example: useful for a color picker) 338 | 339 | /** 340 | * Gets the pixel color of the current position. (example: useful for a color picker) 341 | * 342 | * @return the pixel color of the current mouse position. 343 | */ 344 | public Color getPixelColor(){ 345 | refreshMouseInfo(); 346 | return robot.getPixelColor(mouseX,mouseY); 347 | } 348 | } 349 | 350 | 351 | --------------------------------------------------------------------------------