5846 views|19 replies

1w

Posts

25

Resources
The OP
 

[SAMR21 new gameplay] 34. Simulate USB mouse and keyboard [Copy link]

 
 

Like MicroPython, CircuitPython can easily simulate USB HID devices, and can use the device as a USB keyboard or USB mouse. Let's take a look at the basic usage of USB HID:

import usb_hid

kb = usb_hid.devices[0]
kb.send_report(buf)


buf is bytes or bytearray type. For keyboard, 8 bytes of data need to be sent, and for mouse, 4 bytes of data need to be sent. This content is originally created by dcexpert

, a user of EEWORLD forum. If you want to reprint or use it for commercial purposes, you need to obtain the author's consent and indicate the source

Latest reply

SAMR21 New Gameplay] 34. Simulate USB Mouse and Keyboard [ Modify ]   Details Published on 2023-10-7 17:14
 
 

1w

Posts

25

Resources
2
 

The above is just the basic usage. The specific usage is as follows: How to use the USB keyboard:

游客,如果您要查看本帖隐藏内容请回复

First, define the keyboard class kb, and then send data to represent the key press. Note that after sending the data, you need to send 0 again to represent the key release, otherwise the system will continue to send key presses. For example, the following will send the letter a:

kb_send(4)

 
 
 

1w

Posts

25

Resources
3
 

Because the keyboard buffer is 8 bytes, the first byte represents the status of the ctrl, shift, alt keys, and bytes 2-7 represent characters, so a maximum of 6 characters can be sent at a time. By slightly modifying the above program, you can send multiple characters:

游客,如果您要查看本帖隐藏内容请回复

For example, using kb_sends(4,5,6) will send abc.

 
 
 

1w

Posts

25

Resources
4
 

Note that the keyboard data sent by USB HID is not the ASCII code of the character, but the data defined by the USB protocol. For specific data, please refer to the provisions of the USB protocol, or refer to the simplified table below.

USBKeyScan.pdf (16.4 KB, downloads: 30)

 
 
 

1w

Posts

25

Resources
5
 

The use of the mouse is similar to that of the keyboard, except that device[1] is used and the data is 4 bytes:

mouse = usb_hid.devices[1]
mouse.send_report(bytes([0, 100, 0, 0]))

The above program moves the mouse position 100 to the right (in the x direction).

The data in the buffer, the first byte represents the mouse button; the second/third byte represents the movement distance in the X/Y direction; the fourth byte represents scrolling.

 
 
 

1w

Posts

25

Resources
6
 

USB mouse and keyboard demonstration, when you press the touch key A0, the mouse moves in a circle; press A1 to send the number 1; press A2 to send the letter a.

import usb_hid
from time import sleep
import touchio
from board import *
import math

tp1 = touchio.TouchIn(A0)
tp2 = touchio.TouchIn(A1)
tp3 = touchio.TouchIn(A2)

kb = usb_hid.devices[0]
mouse = usb_hid.devices[1]

n = 0
while 1:
    sleep(0.1)
    if tp1.value:
        t = n*math.pi/360
        x, y = round(10*math.cos(t)), round(10*math.sin(t))
        if x < 0: x += 256
        if y < 0: y += 256
        buf = bytes([0, x, y, 0])
        print(n, buf[0], buf[1] ,buf[2], buf[3])
        mouse.send_report(buf)
        n += 10

    if tp2.value:
        kb.send_report(bytes([0, 0, 0x1E, 0, 0, 0, 0, 0]))
        sleep(0.01)
        kb.send_report(bytes(8))
        kb.send_report(bytes(8))
    
    if tp3.value:
        kb.send_report(bytes([0, 0, 0x04, 0, 0, 0, 0, 0]))
        sleep(0.01)
        kb.send_report(bytes(8))
        kb.send_report(bytes(8))

 
 
 

1

Posts

0

Resources
7
 

SAMR21 New Gameplay] 34. Simulate USB Mouse and Keyboard [ Modify ]

 
 
 

1

Posts

0

Resources
8
 

Hello, is usb-hib built into the device? I want to use python to call it, but it keeps reporting No module named 'usb_hid'.

Comments

It comes with the software. Is it your version problem?  Details Published on 2019-12-19 09:02
 
 
 

1w

Posts

25

Resources
9
 
feiandrou posted on 2019-12-18 22:01 Hello, is usb-hib built into the device? I want to use python to call it now, but I keep getting "No module named 'usb_hi ...

It comes with the software. Is it your version problem?

Comments

Can 8266 or ESP32 also play this?  Details Published on 2019-12-24 10:02
 
 
 

128

Posts

0

Resources
10
 
dcexpert posted on 2019-12-19 09:02 It comes with it. Is it your version problem?

Can 8266 or ESP32 also play this?

Comments

cannot  Details Published on 2019-12-24 10:02
 
 
 

1w

Posts

25

Resources
11
 
9zhmke posted on 2019-12-24 10:02 Can 8266 or ESP32 also play this?

cannot

 
 
 

3

Posts

0

Resources
12
 

See how to use it

 
 
 

10

Posts

0

Resources
13
 

Which development board can run the above code?

Comments

Anything that supports CircuitPython is fine, such as SAM D21/R21/D51, etc.  Details Published on 2020-11-27 15:49
 
 
 

1w

Posts

25

Resources
14
 
soidea posted on 2020-11-27 14:31 Which development board can run the above code?

Anything that supports CircuitPython is fine, such as SAM D21/R21/D51, etc.

 
 
 

1

Posts

0

Resources
15
 

Thank you for sharing the technique.

 
 
 

6

Posts

0

Resources
16
 

RE: [SAMR21 New Gameplay] 34. Simulate USB Mouse and Keyboard [ Modify ]

 
 
 

6

Posts

0

Resources
17
 

RE: [SAMR21 New Gameplay] 34. Simulate USB Mouse and Keyboard [ Modify ]

 
 
 

6

Posts

0

Resources
18
 
  • RE: [SAMR21 New Gameplay] 34. Simulate USB Mouse and Keyboard [ Modify ]
 
 
 

6

Posts

0

Resources
19
 

RE: [SAMR21 New Gameplay] 34. Simulate USB Mouse and Keyboard [ Modify ]

 
 
 

6

Posts

0

Resources
20
 

SAMR21 New Gameplay] 34. Simulate USB Mouse and Keyboard [ Modify ]

 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list