Mouse Click

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
nedla2004
Posts: 5
Joined: Sun Sep 06, 2015 12:33 am

Mouse Click

Post by nedla2004 » Sun Sep 06, 2015 12:46 am

I recently got a PyBoard, and I am trying to use it as a mouse. I can make my cursor move, but I am trying to make it click when I press the button. Here is my code so far.
main.py

Code: Select all

# main.py -- put your code here!
import pyb
import select

switch = pyb.Switch()
accel = pyb.Accel()

while True:
    pyb.hid((0,  0 - accel.x(), 0 - accel.y(), 0))
    if switch():
        #here is where it should click
    pyb.delay(20)
boot.py

Code: Select all

# boot.py -- run on boot-up
# can run arbitrary Python, but best to keep it minimal

import pyb
#pyb.main('main.py') # main script to run after this one
#pyb.usb_mode('CDC+MSC') # act as a serial and a storage device
pyb.usb_mode('CDC+HID') # act as a serial device and a mouse
I am using Windows 8. Thanks!

User avatar
dhylands
Posts: 3214
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Mouse Click

Post by dhylands » Sun Sep 06, 2015 2:49 am

If you use pyb.hid() then the format is pyb.hid(buttons, x, y, z))

where buttons is a bit mask. 0x01 for the left mouse button, 0x02 for the right mouse button, and 0x04 for the middle mouse button.

When you call pyb.hid((1,0,0,0)) that will send a mouse down event. You probably want to follow it shortly by pyb.hid((0,0,0,0)) to do a mouse up. A mouse click is a combination of mouse down and mouse up.

You could do a drag by send mouse down, move, mouse up.

pyb.hid is deprecated, and the "proper" way to do things is to do:

Code: Select all

hid = pyb.USB_HID()
hid.send((buton, x, y, z))
but I see that isn't actually documented.

Post Reply