mouse

usb device mouse

Note

This module is only applicable to the CoreS3 Controller

Micropython Example

USB Mouse

  1# SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
  2#
  3# SPDX-License-Identifier: MIT
  4import os, sys, io
  5import M5
  6from M5 import *
  7from usb.device.mouse import Mouse
  8import time
  9import m5utils
 10
 11
 12label0 = None
 13mouse = None
 14touch_active = None
 15sensitivity = None
 16x = None
 17last_touch_time = None
 18y = None
 19dx = None
 20click_active = None
 21dy = None
 22prev_x = None
 23prev_y = None
 24
 25
 26def setup():
 27    global \
 28        label0, \
 29        mouse, \
 30        touch_active, \
 31        sensitivity, \
 32        x, \
 33        last_touch_time, \
 34        y, \
 35        dx, \
 36        click_active, \
 37        dy, \
 38        prev_x, \
 39        prev_y
 40    M5.begin()
 41    Widgets.fillScreen(0x222222)
 42    label0 = Widgets.Label("USB Mouse", 91, 6, 1.0, 0x158EE6, 0x222222, Widgets.FONTS.DejaVu24)
 43    mouse = Mouse()
 44    touch_active = False
 45    sensitivity = 2
 46    last_touch_time = 0
 47    click_active = False
 48
 49
 50def loop():
 51    global \
 52        label0, \
 53        mouse, \
 54        touch_active, \
 55        sensitivity, \
 56        x, \
 57        last_touch_time, \
 58        y, \
 59        dx, \
 60        click_active, \
 61        dy, \
 62        prev_x, \
 63        prev_y
 64    M5.update()
 65    if mouse.is_open():
 66        if M5.Touch.getCount():
 67            x = m5utils.remap(M5.Touch.getX(), 0, 320, -127, 127)
 68            y = m5utils.remap(M5.Touch.getY(), 0, 240, -127, 127)
 69            if not touch_active:
 70                touch_active = True
 71                prev_x = x
 72                prev_y = y
 73                last_touch_time = time.ticks_ms()
 74            if x != prev_x or y != prev_y:
 75                dx = x - prev_x
 76                dy = y - prev_y
 77                prev_x = x
 78                prev_y = y
 79                mouse.move(int(dx * sensitivity), int(dy * sensitivity))
 80        else:
 81            touch_active = False
 82            dx = 0
 83            dy = 0
 84            if (time.ticks_diff((time.ticks_ms()), last_touch_time)) < 100:
 85                if click_active and dx < 30 and dy < 30:
 86                    click_active = False
 87                    mouse.click_left(True)
 88            else:
 89                click_active = True
 90        if BtnPWR.wasClicked():
 91            mouse.click_right(True)
 92    else:
 93        time.sleep_ms(100)
 94
 95
 96if __name__ == "__main__":
 97    try:
 98        setup()
 99        while True:
100            loop()
101    except (Exception, KeyboardInterrupt) as e:
102        try:
103            from utility import print_error_msg
104
105            print_error_msg(e)
106        except ImportError:
107            print("please update to latest firmware")

UIFlow2.0 Example

USB Mouse

example.png

m5cores3_usbd_mouse_example.m5f2

class Mouse

class usb.device.mouse.Mouse

Create Mouse object

UIFlow2.0

init.png

Mouse.set_axes(x: int = 0, y: int = 0)

Set Cursor Position

  • x Horizontal movement, range: -127 to 127. A value less than 0 moves the cursor to the left, and a value greater than 0 moves it to the right.

  • y Vertical movement, range: -127 to 127. A value less than 0 scrolls the cursor up, and a value greater than 0 scrolls it down.

Note:

Changes will take effect after calling Mouse.send_report().

UIFlow2.0

set_axes.png

Mouse.set_wheel(w: int = 0)

Set mouse wheel value

  • w Wheel value, range: -127 to 127. A value less than 0 scrolls the wheel down, and a value greater than 0 scrolls the wheel up.

Note:

Changes will take effect after calling Mouse.send_report().

UIFlow2.0

set_wheel.png

Mouse.set_buttons(left: bool = False, right: bool = False, middle: bool = False)

Set mouse button status

  • left True indicates the left button is pressed.

  • right True indicates the right button is pressed.

  • middle True indicates the middle (wheel) button is pressed.

Note:

Changes will take effect after calling Mouse.send_report().

example: Mouse click left button

set_buttons(left=True)  # press
send_report()
set_buttons(left=False) # release
send_report()

UIFlow2.0

set_buttons.png

Mouse.send_report()

Send the mouse status report.

UIFlow2.0

send_report.png

Mouse.move(x: int = 0, y: int = 0)

Move cursor

  • x Horizontal movement, range: -127 to 127. A value less than 0 moves the cursor to the left, and a value greater than 0 moves it to the right.

  • y Vertical movement, range: -127 to 127. A value less than 0 moves the cursor up, and a value greater than 0 moves it down.

UIFlow2.0

move.png

Mouse.click_left(release: bool = True)

Click left button

  • release Set to True to release the left mouse button after pressing, or False to not release.

UIFlow2.0

click_left.png

Mouse.click_right(release: bool = True)

Click right button

  • release Set to True to release the right mouse button after pressing, or False to not release.

UIFlow2.0

click_right.png

Mouse.click_middle(release: bool = True)

Click middle button

  • release Set to True to release the left middle button after pressing, or False to not release.

UIFlow2.0

click_middle.png

Mouse.click_forawrd()

Click forward button

  • release Set to True to release the left forward button after pressing, or False to not release.

UIFlow2.0

click_forward.png

Mouse.click_backward()

Click backward button

  • release Set to True to release the left backward button after pressing, or False to not release.

UIFlow2.0

click_backward.png

Mouse.scroll(w: int)

Scroll wheel

  • w range: -127 to 127, values less than 0 scroll up, and values greater than 0 scroll down.

UIFlow2.0

scroll.png