Skip to Content
Raspberry Pi 3 Cookbook for Python Programmers - Third Edition
book

Raspberry Pi 3 Cookbook for Python Programmers - Third Edition

by Steven Lawrence Fernandes, Tim Cox
April 2018
Beginner content levelBeginner
552 pages
13h 58m
English
Packt Publishing
Content preview from Raspberry Pi 3 Cookbook for Python Programmers - Third Edition

Emulating mouse events

The uinput library can emulate mouse and joystick events, as well as keyboard presses. To use the buttons to simulate a mouse, we can adjust the script to use mouse events (as well as defining mousemove to set the step size of the movement) using the following code:

MSG = ["M_UP","M_DOWN","M_LEFT","M_RIGHT","1","Enter"] 
events_mouse=(uinput.REL_Y,uinput.REL_Y, uinput.REL_X, 
             uinput.REL_X,uinput.BTN_LEFT,uinput.BTN_RIGHT) 
mousemove=1 

We also need to modify the button handling to provide continuous movement, as we don't need to keep track of the state of the keys for the mouse. To do so, use the following code:

#Perform the button presses/releases #(but only change state once) for idx, val in enumerate(btn_state): if ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Raspberry Pi for Python Programmers Cookbook - Second Edition

Raspberry Pi for Python Programmers Cookbook - Second Edition

Tim Cox

Publisher Resources

ISBN: 9781788629874Supplemental Content