1PyUserInput 2=========== 3 4A module for cross-platform control of the mouse and keyboard in python that is 5simple to use. 6 7Mouse control should work on Windows, Mac, and X11 (most Linux systems). 8Scrolling is implemented, but users should be aware that variations may 9exist between platforms and applications. 10 11Keyboard control works on X11(linux) and Windows systems. Mac control is a work 12in progress. 13 14Dependencies 15------------ 16 17Depending on your platform, you will need the following python modules for 18PyUserInput to function: 19 20 * Linux - Xlib 21 * Mac - Quartz, AppKit 22 * Windows - pywin32, pyHook 23 24How to get started 25------------------ 26 27After installing PyUserInput, you should have pymouse and pykeyboard modules in 28your python path. Let's make a mouse and keyboard object: 29 30```python 31 32from pymouse import PyMouse 33from pykeyboard import PyKeyboard 34 35m = PyMouse() 36k = PyKeyboard() 37``` 38 39Here's an example of clicking the center of the screen and typing "Hello, World!": 40 41```python 42 43x_dim, y_dim = m.screen_size() 44m.click(x_dim/2, y_dim/2, 1) 45k.type_string('Hello, World!') 46``` 47 48PyKeyboard allows for a range of ways for sending keystrokes: 49 50```python 51 52# pressing a key 53k.press_key('H') 54# which you then follow with a release of the key 55k.release_key('H') 56# or you can 'tap' a key which does both 57k.tap_key('e') 58# note that that tap_key does support a way of repeating keystrokes with a interval time between each 59k.tap_key('l',n=2,interval=5) 60# and you can send a string if needed too 61k.type_string('o World!') 62``` 63 64 65and it supports a wide range of special keys: 66 67```python 68 69#Create an Alt+Tab combo 70k.press_key(k.alt_key) 71k.tap_key(k.tab_key) 72k.release_key(k.alt_key) 73 74k.tap_key(k.function_keys[5]) # Tap F5 75k.tap_key(k.numpad_keys['Home']) # Tap 'Home' on the numpad 76k.tap_key(k.numpad_keys[5], n=3) # Tap 5 on the numpad, thrice 77``` 78 79Note you can also send multiple keystrokes together (e.g. when accessing a keyboard shortcut) using the press_keys method: 80 81```python 82 83# Mac example 84k.press_keys(['Command','shift','3']) 85# Windows example 86k.press_keys([k.windows_l_key,'d']) 87``` 88 89Consistency 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: 90 91```python 92 93# Windows 94k.tap_key(k.alt_key) 95# Mac 96k.tap_key('Alternate') 97``` 98 99I'd like to make a special note about using PyMouseEvent and PyKeyboardEvent. 100These objects are a framework for listening for mouse and keyboard input; they 101don't do anything besides listen until you subclass them. I'm still formalizing 102PyKeyboardEvent, so here's an example of subclassing PyMouseEvent: 103 104```python 105 106from pymouse import PyMouseEvent 107 108def fibo(): 109 a = 0 110 yield a 111 b = 1 112 yield b 113 while True: 114 a, b = b, a+b 115 yield b 116 117class Clickonacci(PyMouseEvent): 118 def __init__(self): 119 PyMouseEvent.__init__(self) 120 self.fibo = fibo() 121 122 def click(self, x, y, button, press): 123 '''Print Fibonacci numbers when the left click is pressed.''' 124 if button == 1: 125 if press: 126 print(self.fibo.next()) 127 else: # Exit if any other mouse button used 128 self.stop() 129 130C = Clickonacci() 131C.run() 132``` 133 134Intended Functionality of Capturing in PyUserInput 135-------------------------------------------------- 136 137For PyMouseEvent classes, the variables "capture" and "capture_move" may be 138passed during instantiation. If `capture=True` is passed, the intended result 139is that all mouse button input will go to your program and nowhere else. The 140same is true for `capture_move=True` except it deals with mouse pointer motion 141instead of the buttons. Both may be set simultaneously, and serve to prevent 142events from propagating further. If you notice any bugs with this behavior, 143please bring it to our attention. 144 145A Short Todo List 146----------------- 147 148These are a few things I am considering for future development in 149PyUserInput: 150 151 * Ensuring that PyMouse capturing works for all platforms 152 * Implement PyKeyboard capturing (add PyKeyboardEvent for Mac as well) 153 * PyMouse dynamic delta scrolling (available in Mac and Windows, hard to standardize) 154 * Make friends with more Mac developers, testing help is needed... 155 156 157Many thanks to 158-------------- 159 160[Pepijn de Vos](https://github.com/pepijndevos) - For making 161[PyMouse](https://github.com/pepijndevos/PyMouse) and allowing me to modify 162and distribute it along with PyKeyboard. 163 164[Jack Grigg](https://github.com/pythonian4000) - For contributions to 165cross-platform scrolling in PyMouse. 166