3
3
4
4
.. image :: https://raw.githubusercontent.com/zeth/inputs/master/devices.png
5
5
6
+ **Inputs ** aims to provide cross-platform Python support for keyboards, mice and gamepads.
7
+
6
8
Install
7
9
-------
8
10
9
11
Install through pypi::
10
12
11
13
pip install inputs
12
14
13
- Or download it from github::
15
+ Or download the whole repository from github::
14
16
15
17
git clone https://github.com/zeth/inputs.git
16
18
cd inputs
@@ -32,216 +34,13 @@ To get started quickly, just use the following::
32
34
33
35
from inputs import devices
34
36
35
- For other examples, keep reading.
37
+ For more information, read the documentation at ReadTheDocs _
36
38
37
- Inputs is in pure Python and there are no dependencies on Raspberry
38
- Pi, Linux or Windows. On the Mac, inputs needs PyObjC which the
39
- included setup.py file will install automatically (as will pip).
39
+ (Also available in the docs directory of the repository)
40
40
41
41
To get involved with inputs, please visit the github project at:
42
42
43
43
https://github.com/zeth/inputs
44
44
45
45
46
- Why Inputs?
47
- -----------
48
-
49
- Obviously high level graphical libraries such as PyGame and PyQT will
50
- provide user input support in a very friendly way. However, the inputs
51
- module does not require your program to use any particular graphical
52
- toolkit, or even have a monitor at all.
53
-
54
- In the Embedded Linux, Raspberry Pi or Internet of Things type
55
- situation, it is quite common not to have an X-server installed or
56
- running.
57
-
58
- This module may also be useful where a computer needs to run a
59
- particular application full screen but you would want to listen out in
60
- the background for a particular set of user inputs, e.g. to bring up
61
- an admin panel in a digital signage setup.
62
-
63
- It is still early days. Android should be tested soon and hopefully
64
- also BSD. Optional Asyncio-based event loop support will probably be
65
- included eventually.
66
-
67
- This module is a single file, so if you cannot or are not allowed to
68
- use setuptools for some reason, just copy the file inputs.py into your
69
- project.
70
-
71
- The inputs module is very simple. The majority of the file is just
72
- constants, so that no matter what platform you are on, input devices
73
- will report the same codes to your program.
74
-
75
- Note to Children
76
- ----------------
77
-
78
- It is pretty easy to use any user input device library, including this
79
- one, to build a keylogger. Using this module to spy on your mum or
80
- teacher or sibling is not cool and may get you into trouble. So please
81
- do not do that. Make a game instead, games are cool.
82
-
83
- Quick Start
84
- -----------
85
-
86
- To access all the available input devices on the current system:
87
-
88
- >>> from inputs import devices
89
- >>> for device in devices:
90
- ... print (device)
91
-
92
- You can also access devices by type:
93
-
94
- >>> devices.gamepads
95
- >>> devices.keyboards
96
- >>> devices.mice
97
- >>> devices.other_devices
98
-
99
- Each device object has the obvious methods and properties that you
100
- expect, stop reading now and just get playing!
101
-
102
- If that is not high level enough, there are three basic functions that
103
- simply give you the latest events (key press, mouse movement/press or
104
- gamepad activity) from the first connected device in the category, for
105
- example:
106
-
107
- >>> from inputs import get_gamepad
108
- >>> while 1 :
109
- ... events = get_gamepad()
110
- ... for event in events:
111
- ... print (event.ev_type, event.code, event.state)
112
-
113
- >>> from inputs import get_key
114
- >>> while 1 :
115
- ... events = get_key()
116
- ... for event in events:
117
- ... print (event.ev_type, event.code, event.state)
118
-
119
- >>> from inputs import get_mouse
120
- >>> while 1 :
121
- ... events = get_mouse()
122
- ... for event in events:
123
- ... print (event.ev_type, event.code, event.state)
124
-
125
- Advanced documentation
126
- ----------------------
127
-
128
- A keyboard is represented by the Keyboard class, a mouse by the Mouse
129
- class and a gamepad by the Gamepad class. These themselves are
130
- subclasses of InputDevice.
131
-
132
- The devices object is an instance of DeviceManager, as you can prove:
133
-
134
- >>> from inputs import DeviceManager
135
- >>> devices = DeviceManager()
136
-
137
- The DeviceManager is reponsible for finding input devices on the
138
- user's system and setting up InputDevice objects.
139
-
140
- The InputDevice objects emit instances of InputEvent. So from top
141
- down, the classes are arranged thus:
142
-
143
- DeviceManager > InputDevice > InputEvent
144
-
145
- So when you have a particular InputEvent instance, you can access its
146
- device and manager:
147
-
148
- >>> event.device.manager
149
-
150
- The event object has a property called device and the device has a
151
- property called manager.
152
-
153
- As you can see, it is really very simple. The device manager has an
154
- attribute called codes which is giant dictionary of key, button and
155
- other codes.
156
-
157
- Gamepads
158
- --------
159
-
160
- An approach often taken by PC games, especially open source games, is
161
- to assume that all gamepads are Microsoft Xbox 360 controllers and
162
- then users use software such as x360ce (on Windows) or xboxdrv (on
163
- Linux) to make other models of gamepad report Xbox 360 style button
164
- and joystick codes to the operating system.
165
-
166
- So for inputs the primary target device is the Microsoft Xbox 360
167
- Wired Controller and this has the best support. Another gamepad might
168
- just work but if not you can use xboxdrv or x360ce to configure it
169
- yourself.
170
-
171
- More testing and support for common gamepads will come in due course.
172
-
173
- Apple Mac OS X doesn't natively support gamepads. If you use third
174
- party driver software that allows a gamepad to emulate a keyboard or a
175
- mouse, then you may be able to use the keyboard or mouse class.
176
-
177
- Raspberry Pi Sense HAT
178
- ----------------------
179
-
180
- The microcontroller on the Raspberry Pi Sense HAT presents the
181
- joystick to the operating system as a keyboard, so find it there under
182
- keyboards. If you worry about this, you are over-thinking things.
183
-
184
- Raspberry Pi Touch Screen
185
- -------------------------
186
-
187
- This presents as a mouse. Again please do not over think it.
188
-
189
-
190
- Windows permissions
191
- -------------------
192
-
193
- By default Windows doesn't stop inputs. However, if you have some
194
- third-party security software you may need to white-list Python. Try
195
- it and find out.
196
-
197
- Linux permissions
198
- -----------------
199
-
200
- On the Raspberry Pi's Raspbian everything just works.
201
-
202
- However, each Linux distribution is different. Some will work straight
203
- away, for some you need to fiddle with permissions.
204
-
205
- Linux distributions often (quite rightly) assume that applications are
206
- installed through their package manager and given the relevant
207
- permissions to access the input devices. However, inputs.py is brand
208
- new and not yet packaged by any Linux distribution.
209
-
210
- Therefore, if the inputs module works as root (e.g. using sudo) but
211
- not as your normal user, then you usually need to add yourself to an
212
- inputs group or similar.
213
-
214
- Mac permissions
215
- ---------------
216
-
217
- On the Mac, until you write a proper installer for your program, you
218
- will probably have to use the settings application to allow your
219
- program to access the input devices.
220
-
221
- .. image :: https://raw.githubusercontent.com/zeth/inputs/master/macsecurity.png
222
-
223
- The first time you use inputs, it will not have any output, then you
224
- will either get the above settings window pop up automatically, or you
225
- will need to find your way there.
226
-
227
- Credits
228
- -------
229
-
230
- Inputs is by Zeth, all mistakes are mine.
231
-
232
- Thanks to Dave Jones for stick.py which is not only the basis for
233
- Sense HAT stick support in this module but more importantly also
234
- taught me an easier way to parse the Evdev event format in Python:
235
-
236
- https://github.com/RPi-Distro/python-sense-hat/blob/master/sense_hat/stick.py
237
-
238
- https://github.com/waveform80/pisense/blob/master/pisense/stick.py
239
-
240
- Thanks to Andy (r4dian) and Jason R. Coombs whose existing (MIT
241
- licenced) Python examples for Xbox 360 controller support on Windows
242
- helped me understand xinput greatly. Xbox 360 controller support on
243
- Windows here is based on their work:
244
-
245
- https://github.com/r4dian/Xbox-360-Controller-for-Python
246
-
247
- http://pydoc.net/Python/jaraco.input/1.0.1/jaraco.input.win32.xinput/
46
+ .. _ReadTheDocs : https://inputs.readthedocs.io/
0 commit comments