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
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.

Code: Select all

# -- 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

Code: Select all

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

import pyb
#pyb.main('') # 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
Posts: 3214
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada

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