Smulating mouse click, cursor movement and keystrokes
To install auto_click:
gem install auto_click
To update auto_click:
gem update auto_click
Using auto_click is easy. To use the methods provided by auto_click. You need to require the gem and include the module:
require 'auto_click' include AutoClickMethods
To move the mouse cursor to 50,50 ( the top left corner is 0,0) and perform a double left click:
mouse_move(50,50) double_click
To move the mouse cursor to 50,50 and perform a right click:
mouse_move(50,50) right_click
To move the mouse cursor to the middle of the screen:
mouse_move_percentage(0.5,0.5)
To scroll up (forward) 10 wheel steps:
mouse_scroll(10)
To scroll down (backward) 5 wheel steps:
mouse_scroll(-5)
To get the current cursor position
cursor_position
To drag the icon on (50,50) to (200,300):
left_drag(50,50,200,300)
Suppose the notepad window on the top left corner of the screen, to type “Auto Click” in notepad. (Methods like get_windows(“notepad”) will be implemented in future release. So that you do not need to use this left click things to get focus on the notepad window.…..in later release.…..)
mouse_move(100,100) leftclick type('Auto Click!')
The type method will Send keystroke for every characters of the string. It will ensure that the capslock is off and hold shift when it is typing a capital character or a character that need shift down (e.g. ‘!’). Notice that you need to escape the character \ and ‘ in single quote string, and you need to escape \ “ and # in double quote string. For example, to type #”\ , you can either do:
type('#\'\'\\')
or
type("\#''\\")
The methods shown in Basic Usage are convenient, but sometimes you may need more control on your mouse actions and key strokes.
To show the current cursor x and y position in pixels:
puts cursor_position[0] puts cursor_position[1]
To move the mouse cursor downward 300 pixels
mouse_move(cursor_position[0]+300,cursor_position[1])
Yes this is not very efficient but mouse_move_relative methods will be implemented in later releases.
If you wish to left click and hold:
mouse_down(:left)
And remember to release the button later:
mouse_up(:left)
To perform a double_click, instead of using:
double_click
You can use two click calls:
left_click left_click
And of course if for some reasons you need double middle mouse click, you can:
middle_click middle_click
For get more refined control to key strokes simulation, you can use the method key_stroke, key_up and key_down methods
To send keystroke ‘a’:
key_stroke('a')
To send keystroke tab:
key_stroke('tab')
Therefore, to type “Auto Click!” , you can either use the simple type method stated basic usage section, or you can do:
key_down('shift') key_stroke('a') key_up('shift') key_stroke('u') key_stroke('t') key_stroke('o') key_stroke('space') key_down('shift') key_stroke('c') key_up('shift') key_stroke('l') key_stroke('i') key_stroke('c') key_stroke('k') key_down('shift') key_stroke('num1') key_up('shift')
And you can have detail adjustments to the keyboard behaviour that you need.
Notice that ‘a’ means the key ‘a’ instead of the letter ‘a’. Therefore key_stroke(‘a’) and key_stroke(‘A’) are basically the same. If the capslock is off but you want to type the capitalized A, you need to:
key_down('shift') key_stroke('a') key_up('shift')
A list of key stroke names will be included in the readme later. ‘a’ to ‘z’, ‘num0’ to ‘num9’, ‘f1’ to ‘f12’, ‘shift’ ‘ctrl’ ‘alt’ ‘win’ ‘tab’ ‘del’ ‘ins’ ‘return’ ‘esc’ ‘pageup’ ‘pagedown’ ‘left’ ‘right’ ‘up’ ‘down’ ‘equal’ ‘hyphen’.…..etc are all acceptable key names. I am trying to cater a certain number of name variations, for example:
key_stroke('left') key_stroke('leftarrow') key_stroke('leftkey')
will do the same thing.
If you know the virtual key code you can directly enter the key code, for example:
key_stroke(0x41)
is the same as
key_stroke('a')
The list of virtual key codes can be found at msdn.microsoft.com/en-us/library/dd375731(v=vs.85).aspx
There are some methods which are not discussed here. Please see the following Method List for all the available methods.
If you do not want include the auto_click methods, instead of using
include AutoClickMethods
you can construct a new instance of AutoClick and invoke the methods from the instance:
ac = AutoClick.new() ac.mouse_move(50,50)
-
left_click
-
right_click
-
middle_click
-
double_click
-
mouse_down(button_name)
-
mouse_up(button_name)
-
cursor_position
-
mouse_move(x,y)
-
mouse_move_percentage(x_percent,y_percent)
-
mouse_scroll(step)
-
left_drag(x_start,y_start,x_end,y_end)
-
right_drag(x_start,y_start,x_end,y_end)
-
type(string)
-
key_stroke(key_name)
-
key_down(key_name)
-
key_up(key_name)
-
Backward compatibility with 32-bit Ruby
-
Add options of movement speed of mouse so the cursor is actually “moving” instead of just show up at the destination instantly
-
GetWindow and GetWindowFocus Method
-
Refactor the ugly type(string) method
-
Use code block to implement modifier keys
-
More effective way to move mouse relative to current position
(for full change log please refer to the CHANGELOG file in the repository)
-
0.5.0 Users have to explicitly use “include AutoClickMethods” to include the methods, add mouse_move_percentage and get_screen_resolution
-
0.4.0 Fix the 64bit bug and Fixnum! bug
-
0.3.0 Update to use DL Library
MIT license
Copyright © 2010-2019 by Chungsang Tom Lam
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.