A module for cross-platform control of the mouse and keyboard in python that is simple to install and use.

Related tags

HardwarePyUserInput
Overview

PyUserInput

PyUserInput is a group project so we've moved the project over to a group organization: https://github.com/PyUserInput/PyUserInput . That is now the active development repository and I'll be phasing this one out, so please go there for the latest code and to post new issues. This should be corrected on PyPI in the next version update of PyUserInput.

A module for cross-platform control of the mouse and keyboard in python that is simple to use.

Mouse control should work on Windows, Mac, and X11 (most Linux systems). Scrolling is implemented, but users should be aware that variations may exist between platforms and applications.

Keyboard control works on X11(linux) and Windows systems. Mac control is a work in progress.

Dependencies

Depending on your platform, you will need the following python modules for PyUserInput to function:

  • Linux - Xlib
  • Mac - Quartz, AppKit
  • Windows - pywin32, pyHook

How to get started

After installing PyUserInput, you should have pymouse and pykeyboard modules in your python path. Let's make a mouse and keyboard object:

from pymouse import PyMouse
from pykeyboard import PyKeyboard

m = PyMouse()
k = PyKeyboard()

Here's an example of clicking the center of the screen and typing "Hello, World!":

x_dim, y_dim = m.screen_size()
m.click(x_dim/2, y_dim/2, 1)
k.type_string('Hello, World!')

PyKeyboard allows for a range of ways for sending keystrokes:

# pressing a key
k.press_key('H')
# which you then follow with a release of the key
k.release_key('H')
# or you can 'tap' a key which does both
k.tap_key('e')
# note that that tap_key does support a way of repeating keystrokes with a interval time between each
k.tap_key('l',n=2,interval=5) 
# and you can send a string if needed too
k.type_string('o World!')

and it supports a wide range of special keys:

#Create an Alt+Tab combo
k.press_key(k.alt_key)
k.tap_key(k.tab_key)
k.release_key(k.alt_key)

k.tap_key(k.function_keys[5])  # Tap F5
k.tap_key(k.numpad_keys['Home'])  # Tap 'Home' on the numpad
k.tap_key(k.numpad_keys[5], n=3)  # Tap 5 on the numpad, thrice

Note you can also send multiple keystrokes together (e.g. when accessing a keyboard shortcut) using the press_keys method:

# Mac example
k.press_keys(['Command','shift','3'])
# Windows example
k.press_keys([k.windows_l_key,'d'])

Consistency between platforms is a big challenge; Please look at the source for the operating system that you are using to help understand the format of the keys that you would need to send. For example:

# Windows
k.tap_key(k.alt_key)
# Mac
k.tap_key('Alternate')

I'd like to make a special note about using PyMouseEvent and PyKeyboardEvent. These objects are a framework for listening for mouse and keyboard input; they don't do anything besides listen until you subclass them. I'm still formalizing PyKeyboardEvent, so here's an example of subclassing PyMouseEvent:

from pymouse import PyMouseEvent

def fibo():
    a = 0
    yield a
    b = 1
    yield b
    while True:
        a, b = b, a+b
        yield b

class Clickonacci(PyMouseEvent):
    def __init__(self):
        PyMouseEvent.__init__(self)
        self.fibo = fibo()

    def click(self, x, y, button, press):
        '''Print Fibonacci numbers when the left click is pressed.'''
        if button == 1:
            if press:
                print(self.fibo.next())
        else:  # Exit if any other mouse button used
            self.stop()

C = Clickonacci()
C.run()

Intended Functionality of Capturing in PyUserInput

For PyMouseEvent classes, the variables "capture" and "capture_move" may be passed during instantiation. If capture=True is passed, the intended result is that all mouse button input will go to your program and nowhere else. The same is true for capture_move=True except it deals with mouse pointer motion instead of the buttons. Both may be set simultaneously, and serve to prevent events from propagating further. If you notice any bugs with this behavior, please bring it to our attention.

A Short Todo List

These are a few things I am considering for future development in PyUserInput:

  • Ensuring that PyMouse capturing works for all platforms
  • Implement PyKeyboard capturing (add PyKeyboardEvent for Mac as well)
  • PyMouse dynamic delta scrolling (available in Mac and Windows, hard to standardize)
  • Make friends with more Mac developers, testing help is needed...

Many thanks to

Pepijn de Vos - For making PyMouse and allowing me to modify and distribute it along with PyKeyboard.

Jack Grigg - For contributions to cross-platform scrolling in PyMouse.

Owner
Paul Barton
Paul Barton
Hotplugger: Real USB Port Passthrough for VFIO/QEMU!

Hotplugger: Real USB Port Passthrough for VFIO/QEMU! Welcome to Hotplugger! This app, as the name might tell you, is a combination of some scripts (py

DARKGuy (Alemar) 66 Nov 24, 2022
Code for the onshape macropad.

Onshape_Macropad Code for the onshape macropad. This is a macropad built using the Pimoroni Keybow and the KPrepublic Enclosure. pimoroni_keybow kprep

Justin Cole 1 Nov 23, 2021
Uses the Duke Energy Gateway to import near real time energy usage into Home Assistant

Duke Energy Gateway This is a custom integration for Home Assistant. It pulls near-real-time energy usage from Duke Energy via the Duke Energy Gateway

Michael Meli 28 Dec 23, 2022
Micropython automatic watering

micropython-automatic-watering micropython automatic watering his code was developed to be used with nodemcu esp8266, but can be modified to work with

1 Nov 24, 2021
Programmable Rainbow Redstone Computer

Programmable Rainbow Redstone Computer Table of contents What is it? Program flasher How to use it What is it? PRRC is Programmable Rainbow Redstone C

Fern H 2 Jun 07, 2022
Implemented robot inverse kinematics.

robot_inverse_kinematics Project setup # put the package in the workspace $ cd ~/catkin_ws/ $ catkin_make $ source devel/setup.bash Description In thi

Jianming Han 2 Dec 08, 2022
Imbalaced Classification and Robust Semantic Segmentation

Imbalaced Classification and Robust Semantic Segmentation This repo implements two algoritms. The imbalance clibration (IC) algorithm for image classi

24 Jul 23, 2022
Brogrammer-keyboard - FIrmware for the Brogrammer Keyboard v1.0

Brogrammer Keyboard Firmware The package contains the firmware that runs on the Brogrammer Keyboard v1.0 See https://imgur.com/a/oY5QZ14 This keyboard

Devin Hartleben 1 Apr 21, 2022
Provide Unifi device info via api to Home Assistant that will give ap sensors

Unifi AP Device info Provide Unifi device info via api to Home Assistant that will give ap sensors

12 Jan 07, 2023
Designed a system that can efficiently sort recyclables and transfer them to corresponding bins using Python, a Raspberry Pi, and Quanser Labs.

System for Sorting and Recycling Containers - Project 3 Table of contents Overview The challenge Screenshot My process Built with Code snippets What I

Mit Patel 2 Dec 02, 2022
Интеграция Home Assistant с ЛК "Интер РАО"

ЕЛК ЖКХ «Интер РАО» для Home Assistant Предоставление информации о текущем состоянии ваших аккаунтов в ЕЛК ЖКХ. Введение @ TODO @ Установка Посредство

Alexander Ryazanov 27 Nov 05, 2022
Code for the paper "Planning with Diffusion for Flexible Behavior Synthesis"

Planning with Diffusion Training and visualizing of diffusion models from Planning with Diffusion for Flexible Behavior Synthesis. Guided sampling cod

Michael Janner 310 Jan 07, 2023
Controlling fireworks with micropython

Controlling-fireworks-with-micropython How the code works line 1-4 from machine

Montso Mokake 1 Jan 08, 2022
Python library to interact with the GCE Electronics IPX800 device

A python library to control a GCE-Electronics IPX800 V4 device through its API.

Marc-Aurèle Brothier 2 Oct 20, 2021
Beam designs for infinite Z 3D printers

A 3D printed beam that is as stiff as steel A while ago Naomi Wu 机械妖姬 very kindly sent us one of Creality's infinite-Z belt printers. Lots of people h

RepRap Ltd 105 Oct 22, 2022
Andreas Frisch 1 Jan 10, 2022
Playing diabolo with two robot arms in ROS + Gazebo

Playing diabolo with robots This repository holds the ROS packages for playing diabolo with two UR5e robot arms on ROS Melodic (Ubuntu 18.04). Read ou

23 Dec 18, 2022
Raspberry Pi Pico as a Rubber Ducky

Raspberry-Pi-Pico-as-a-Rubber-Ducky Kurulum Raspberry Pi Pico cihazınız için CircuitPython'u indirin. Boot düğmesine basılı tutarken cihazı bir USB ba

Furkan Enes POLATOĞLU 6 Dec 13, 2022
A DiY holiday project to demonstrate how you can send data from adafruitIO cloud to a balena edge device

holiday-star balena ❤️ adafruitIO Introduction A DiY holiday project to demonstrate how you can send data from adafruitIO cloud to a balena edge devic

Ayan Pahwa 3 Dec 20, 2021
CircuitPython library for the CH559 USB to Serial chip

CH559 (USB to Serial) CircuitPython Library Why? Because you might want to get keyboard/mouse/gamepad/HID input into your CircuitPython projects witho

Guy Dupont 3 Nov 19, 2022