mouse
usb 设备鼠标
备注
当前模块只适用于 CoreS3 主机
Micropython 案例
usb 鼠标
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 12 13label0 = None 14mouse = None 15touch_active = None 16sensitivity = None 17x = None 18last_touch_time = None 19y = None 20dx = None 21click_active = None 22dy = None 23prev_x = None 24prev_y = None 25 26 27def setup(): 28 global label0, mouse, touch_active, sensitivity, x, last_touch_time, y, dx, click_active, dy, prev_x, prev_y 29 M5.begin() 30 Widgets.fillScreen(0x222222) 31 label0 = Widgets.Label("USB Mouse", 91, 6, 1.0, 0x158ee6, 0x222222, Widgets.FONTS.DejaVu24) 32 mouse = Mouse() 33 touch_active = False 34 sensitivity = 2 35 last_touch_time = 0 36 click_active = False 37 38 39def loop(): 40 global label0, mouse, touch_active, sensitivity, x, last_touch_time, y, dx, click_active, dy, prev_x, prev_y 41 M5.update() 42 if mouse.is_open(): 43 if M5.Touch.getCount(): 44 x = m5utils.remap(M5.Touch.getX(), 0, 320, -127, 127) 45 y = m5utils.remap(M5.Touch.getY(), 0, 240, -127, 127) 46 if not touch_active: 47 touch_active = True 48 prev_x = x 49 prev_y = y 50 last_touch_time = time.ticks_ms() 51 if x != prev_x or y != prev_y: 52 dx = x - prev_x 53 dy = y - prev_y 54 prev_x = x 55 prev_y = y 56 mouse.move(int(dx * sensitivity), int(dy * sensitivity)) 57 else: 58 touch_active = False 59 dx = 0 60 dy = 0 61 if (time.ticks_diff((time.ticks_ms()), last_touch_time)) < 100: 62 if click_active and dx < 30 and dy < 30: 63 click_active = False 64 mouse.click_left(True) 65 else: 66 click_active = True 67 if BtnPWR.wasClicked(): 68 mouse.click_right(True) 69 else: 70 time.sleep_ms(100) 71 72 73if __name__ == '__main__': 74 try: 75 setup() 76 while True: 77 loop() 78 except (Exception, KeyboardInterrupt) as e: 79 try: 80 from utility import print_error_msg 81 print_error_msg(e) 82 except ImportError: 83 print("please update to latest firmware") 84
UIFlow2.0 案例
usb 鼠标
class Mouse
- class usb.device.mouse.Mouse
创建 Mouse 对象
UIFlow2.0

- Mouse.set_axes(x: int = 0, y: int = 0)
设置光标位置
x水平移动,范围:-127~127,小于0为向左移动,大于0为向右移动。y纵向移动,范围:-127~127,小于0为向上滚动,大于0为向下滚动。
- Note:
需要调用 Mouse.send_report() 后生效
UIFlow2.0

- Mouse.set_wheel(w: int = 0)
设置鼠标滚轮值
w滚轮值,范围:-127~127,小于0为向下滚动,大于0为向上滚动。
- Note:
需要调用 Mouse.send_report() 后生效
UIFlow2.0

- Mouse.set_buttons(left: bool = False, right: bool = False, middle: bool = False)
设置鼠标按钮状态
leftTrue 为按下左键rightTrue 为按下右键middleTrue 为按下滚轮
- Note:
需要调用 Mouse.send_report() 后生效
需要调用 Mouse.send_report() 后生效
set_buttons(left=True) # press send_report() set_buttons(left=False) # release send_report()
UIFlow2.0

- Mouse.send_report()
Send the mouse status report.
UIFlow2.0

- Mouse.move(x: int = 0, y: int = 0)
移动光标
x水平移动,范围:-127~127,小于0为向左移动,大于0为向右移动。y纵向移动,范围:-127~127,小于0为向上滚动,大于0为向下滚动。
UIFlow2.0

- Mouse.click_forawrd()
单击前进按键
release设为 True 为按下鼠标前进键后释放, False 为不释放
UIFlow2.0

- Mouse.click_backward()
单击后退按键
release设为 True 为按下鼠标前后退后释放, False 为不释放
UIFlow2.0





