Skip to content

Commit 76bba13

Browse files
authored
usbhid: add support for mouse buttons (#2900)
* usbhid: add support for mouse buttons
1 parent ada1109 commit 76bba13

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

src/machine/usb/hid/mouse/mouse.go

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,17 @@ import (
66

77
var Mouse *mouse
88

9+
type Button byte
10+
11+
const (
12+
Left Button = 1 << iota
13+
Right
14+
Middle
15+
)
16+
917
type mouse struct {
10-
buf *hid.RingBuffer
18+
buf *hid.RingBuffer
19+
button Button
1120
}
1221

1322
func init() {
@@ -57,11 +66,33 @@ func (m *mouse) Move(vx, vy int) {
5766
}
5867

5968
m.buf.Put([]byte{
60-
0x01, 0x00, byte(vx), byte(vy), 0x00,
69+
0x01, byte(m.button), byte(vx), byte(vy), 0x00,
70+
})
71+
}
72+
73+
// Cilck clicks the mouse button.
74+
func (m *mouse) Click(btn Button) {
75+
m.Press(btn)
76+
m.Release(btn)
77+
}
78+
79+
// Press presses the given mouse buttons.
80+
func (m *mouse) Press(btn Button) {
81+
m.button |= btn
82+
m.buf.Put([]byte{
83+
0x01, byte(m.button), 0x00, 0x00, 0x00,
84+
})
85+
}
86+
87+
// Release releases the given mouse buttons.
88+
func (m *mouse) Release(btn Button) {
89+
m.button &= ^btn
90+
m.buf.Put([]byte{
91+
0x01, byte(m.button), 0x00, 0x00, 0x00,
6192
})
6293
}
6394

64-
// WHEEL controls the mouse wheel.
95+
// Wheel controls the mouse wheel.
6596
func (m *mouse) Wheel(v int) {
6697
if v == 0 {
6798
return
@@ -75,7 +106,7 @@ func (m *mouse) Wheel(v int) {
75106
}
76107

77108
m.buf.Put([]byte{
78-
0x01, 0x00, 0x00, 0x00, byte(v),
109+
0x01, byte(m.button), 0x00, 0x00, byte(v),
79110
})
80111
}
81112

0 commit comments

Comments
 (0)