Certainly! Below is a Python script that allows you to control your mouse cursor using the keyboard:
- Arrow Keys: Move the mouse cursor.
- Z key: Perform a left-click.
- X key: Perform a right-click.
- K key: Hold down the left mouse button.
- L key: Hold down the right mouse button.
Prerequisites
First, you'll need to install the required Python packages:
- pynput: For controlling the mouse.
- keyboard: For capturing keyboard events.
You can install these packages using pip:
pip install pynput keyboard
⚠️ Note: On some systems, especially Windows, you may need to run your Python script with administrative privileges to capture global keyboard events.
The Script
import keyboard
from pynput.mouse import Button, Controller
# Initialize the mouse controller
mouse = Controller()
# Variables to track if mouse buttons are being held down
left_button_held = False
right_button_held = False
# Amount of pixels to move per arrow key press
move_amount = 10
def on_press(key_event):
global left_button_held, right_button_held
key = key_event.name
# Move cursor with arrow keys
if key == 'up':
x, y = mouse.position
mouse.position = (x, y - move_amount)
elif key == 'down':
x, y = mouse.position
mouse.position = (x, y + move_amount)
elif key == 'left':
x, y = mouse.position
mouse.position = (x - move_amount, y)
elif key == 'right':
x, y = mouse.position
mouse.position = (x + move_amount, y)
# Left click with 'z'
elif key == 'z':
mouse.click(Button.left, 1)
# Right click with 'x'
elif key == 'x':
mouse.click(Button.right, 1)
# Hold down left mouse button with 'k'
elif key == 'k':
if not left_button_held:
mouse.press(Button.left)
left_button_held = True
# Hold down right mouse button with 'l'
elif key == 'l':
if not right_button_held:
mouse.press(Button.right)
right_button_held = True
def on_release(key_event):
global left_button_held, right_button_held
key = key_event.name
# Release left mouse button when 'k' is released
if key == 'k':
if left_button_held:
mouse.release(Button.left)
left_button_held = False
# Release right mouse button when 'l' is released
if key == 'l':
if right_button_held:
mouse.release(Button.right)
right_button_held = False
# Start the keyboard listener
keyboard.on_press(on_press)
keyboard.on_release(on_release)
# Keep the script running until 'esc' is pressed
print("Mouse control script is running. Press 'Esc' to exit.")
keyboard.wait('esc')
How to Use the Script
-
Save the script: Copy and paste the script into a file named mouse_control.py.
-
Run the script: Execute the script using Python:
python mouse_control.py
-
Control the mouse:
- Move the Cursor: Use the arrow keys to move the mouse cursor in small increments.
- Left Click: Press the Z key to perform a left-click.
- Right Click: Press the X key to perform a right-click.
- Hold Left Button: Press and hold the K key to hold down the left mouse button. Release the K key to release the button.
- Hold Right Button: Press and hold the L key to hold down the right mouse button. Release the L key to release the button.
-
Exit the Script: Press the Esc key to stop the script.
Notes
-
Adjusting Movement Speed: The move_amount variable controls how many pixels the cursor moves with each arrow key press. Increase this number for faster movement.
move_amount = 10 # Increase or decrease this value as needed
-
Administrative Privileges: If the script doesn't capture keyboard events, try running it as an administrator.
-
Cross-Platform Compatibility: The script should work on Windows, macOS, and Linux, but administrative privileges requirements may vary.
-
Safety: Always be cautious when running scripts that capture keyboard input or control the mouse to ensure they come from a trusted source.
Troubleshooting
-
Keyboard Module Issues: If you encounter an error related to the keyboard module not being able to capture events:
- On Linux, you may need to run the script as root.
- On macOS, you might need to grant accessibility permissions to the terminal or Python interpreter in System Preferences.
-
Permission Errors: If the script fails due to permissions, ensure that your user account has the necessary rights, or try running the script with elevated privileges.
Conclusion
This script provides a simple way to control the mouse cursor using the keyboard. It's useful in scenarios where the mouse is not functioning correctly, or for accessibility purposes.
Feel free to modify and extend the script to suit your needs!