auto_click
AutoClick for Crystal
A Windows GUI automation library for Crystal that provides mouse and keyboard automation capabilities by interfacing with Windows User32 API functions.
This is a Crystal port of the Ruby AutoClick library.
Features
- Mouse Operations: Click, drag, move, scroll functionality
- Keyboard Operations: Key presses, text typing with special character support
- System Information: Screen resolution, cursor position, key states
- Windows API Integration: Direct calls to User32.dll functions
- Key Mapping: Support for multiple key naming conventions
Installation
Add this to your application's shard.yml
:
dependencies:
auto_click:
github: kojix2/auto_click
Then run:
shards install
Usage
Method 1: Include the module
require "auto_click"
include AutoClick
# Mouse operations
left_click()
mouse_move(100, 100)
mouse_move_percentage(0.5, 0.5) # Move to center of screen
right_click()
double_click()
mouse_scroll(3) # Scroll up 3 steps
# Keyboard operations
input_text("Hello World!")
key_stroke("enter")
key_combination("ctrl", "c") # Copy
copy() # Shorthand for Ctrl+C
paste() # Shorthand for Ctrl+V
# System information
width, height = screen_resolution()
x, y = cursor_position()
Method 2: Use as namespace
require "auto_click"
AutoClick.left_click()
AutoClick.mouse_move(100, 100)
AutoClick.input_text("Hello World!")
API Reference
Mouse Operations
Basic Clicks
left_click()
- Left mouse button clickright_click()
- Right mouse button clickmiddle_click()
- Middle mouse button clickdouble_click(interval = nil)
- Double click (optional custom interval seconds; default derives from system setting)
Mouse State Control
mouse_down(button_name)
- Press and hold mouse button (:left
,:right
,:middle
)mouse_up(button_name)
- Release mouse button
Cursor Movement
mouse_move(x, y)
- Move cursor to coordinatesmouse_move_percentage(x_percent, y_percent)
- Move using screen percentagecursor_position()
- Get current cursor positionsmooth_move(x, y, steps, delay)
- Smooth cursor movement
Drag Operations
left_drag(sx, sy, ex, ey)
- Left drag from start to end coordinatesright_drag(sx, sy, ex, ey)
- Right drag from start to end coordinatesdrag(sx, sy, ex, ey, button)
- Drag with specified button
Scroll Operations
mouse_scroll(steps)
- Scroll wheel (positive=up, negative=down)scroll_up(steps)
- Scroll upscroll_down(steps)
- Scroll down
Utility Methods
click_at(x, y, button)
- Click at specific coordinatesdouble_click_at(x, y, interval = nil)
- Double click at coordinates with optional intervalmouse_button_pressed?(button)
- Check if button is pressed
Keyboard Operations
Key Presses
key_stroke(key_name)
- Press and release keykey_down(key_name)
- Press and hold keykey_up(key_name)
- Release key
Text Input
input_text(text, toggle_capslock: false, raise_unknown: false)
- Send text with automatic shift handling (optionally ignore or raise on unknown chars)input_text_with_delay(text, delay, toggle_capslock: false, raise_unknown: false)
- Same with per‑character delay
Key Combinations
key_combination(*keys)
- Press multiple keys simultaneouslykey_combination(keys_array)
- Press keys from array
Common Shortcuts
copy()
- Ctrl+Cpaste()
- Ctrl+Vcut()
- Ctrl+Xselect_all()
- Ctrl+Aundo()
- Ctrl+Zredo()
- Ctrl+Ysave()
- Ctrl+Salt_tab()
- Alt+Tabwindows_key()
- Windows keyalt_f4()
- Alt+F4task_manager()
- Ctrl+Shift+Esclock_screen()
- Win+Lshow_desktop()
- Win+Drun_dialog()
- Win+R
Key State
get_key_state(key_name)
- Get key statekey_pressed?(key_name)
- Check if key is pressedkey_toggled?(key_name)
- Check if toggle key is on
Utility
hold_key(key_name, duration)
- Hold key for durationrepeat_key(key_name, count, delay)
- Press key multiple times
System Information
screen_resolution()
- Get screen width and heightcursor_position()
- Get current cursor coordinates
Key Names
The library supports flexible key naming:
Letters and Numbers
"a", "b", "c", ..., "z" # Letters (case insensitive)
"0", "1", "2", ..., "9" # Numbers
Function Keys
"f1", "f2", ..., "f12" # Function keys
Modifier Keys
"shift", "leftshift", "rightshift"
"ctrl", "control", "leftctrl", "rightctrl"
"alt", "leftalt", "rightalt"
"win", "windows", "leftwin", "rightwin"
Special Keys
"space", "enter", "tab", "esc", "escape"
"backspace", "delete", "insert"
"home", "end", "pageup", "pagedown"
"left", "right", "up", "down" # Arrow keys
"capslock", "numlock", "scrolllock"
"pause", "printscreen"
Symbol Keys
"semicolon", "equal", "comma", "hyphen", "period"
"slash", "grave", "bracket", "backslash"
"closebracket", "quote"
Special Characters
The library automatically handles special characters that require Shift:
input_text("Hello World!") # Automatically presses Shift for '!'
input_text("user@example.com") # Handles @ symbol
input_text("Price: $19.99") # Handles $ symbol
Supported special characters: ! @ # $ % ^ & * ( ) _ + { } | : " < > ? ~
Options:
toggle_capslock: true
– temporarily turn off Caps Lock while sending (restored after)raise_unknown: true
– raiseArgumentError
if a character/key can't be mapped (default silently skips)
Platform Support
- Windows: Full support (primary platform)
- Linux/macOS: Not supported (Windows-specific User32 API)
Requirements
- Crystal 1.0.0 or later
- Windows operating system
- User32.dll (standard Windows library)
Examples
See the examples/
directory for complete usage examples.
Architecture
The library is organized into several modules:
AutoClick::User32
- Windows API bindingsAutoClick::InputStructure
- INPUT structure definitionsAutoClick::VirtualKey
- Virtual key code mappingsAutoClick::Mouse
- Mouse operationsAutoClick::Keyboard
- Keyboard operations
Contributing
- Fork it (https://github.com/kojix2/auto_click/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
License
MIT License. See LICENSE file for details.
Credits
This library is a Crystal port of the Ruby AutoClick library by erinata.
auto_click
- 0
- 0
- 0
- 0
- 0
- 2 days ago
- August 29, 2025
MIT License
Mon, 08 Sep 2025 19:32:34 GMT