Smulating mouse click, cursor movement and keystrokes
To install auto_click:
gem install auto_click
Using auto_click is easy. To use the methods provided by auto_click. You need to require it.
To move the mouse cursor to 50,50 ( the top left corner is 0,0) and perform a double left click:
To move the mouse cursor to 50,50 and perform a right click:
To scroll up (forward) 10 wheel steps:
To scroll down (backward) 5 wheel steps:
To get the current cursor position
To drag the icon on (50,50) to (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:
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 puts cursor_position
To move the mouse cursor downward 300 pixels
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:
And remember to release the button later:
To perform a double_click, instead of using:
You can use two click calls:
And of course if for some reasons you need double middle mouse click, you can:
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':
To send keystroke 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:
is the same as
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.
To Do List
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
More effective way to move mouse relative to current position
(for full change log please refer to the CHANGELOG file in the repository)
0.1.15 Add more general mouse control methods: mouse_down(button_name), mouse_up(button_name)
0.1.14 Add middle_click and double_click
0.1.12 Super ugly implementation of the method type (need refactoring or even rewite later)
0.1.10 Implement get_key_state method. Refine the key_stroke, key_down and key_up method
0.1.9 Add support for keybaord region specific keys using US-keyboard standard
0.1.8 Implement key_stroke, key_down and key_up
0.1.7 Primitive implementation of key_stroke. Need good api before putting it into method list
0.1.4 Implement left_drag and right_drag
Ruby 1.9.1-p430, Windows 7
Ruby 1.9.2-p0, Windows 7
Ruby 1.9.2-p136, Windows 7
Copyright (C) 2010-2011 by 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.