Programmatically accessing Logitech MX Master 4 haptic engine
I recently bought the Logitech MX Master 4 (affiliate link). Definitely better than the Apple Magic Mouse, but the bar isn’t very high. Anyway, one of the features of the 4th generation of this mouse is the haptic engine. The mouse can vibrate on certain actions, configurable via proprietary Logi Options+ software. By default, it supports Adobe Photoshop and Figma, and Zoom with a plugin. But what if we could control the vibration ourselves, whenever we feel like it?
Straight to the point, here’s the source code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""Trigger haptic feedback on a Logitech MX Master 4 via HID++ 2.0."""
import argparse
import sys
import time
import hid
VID, PID = 0x046D, 0xB042
HID_PP_USAGE_PAGE = 0xFF43
HAPTIC_FEATURE = 0x19B0
SW_ID = 0x0A
DEV_IDX = 0xFF # direct (Bluetooth) device
def open_device() -> "hid.device":
for d in hid.enumerate(VID, PID):
if d["usage_page"] == HID_PP_USAGE_PAGE:
h = hid.device()
h.open_path(d["path"])
return h
sys.exit("MX Master 4 HID++ interface not found (is it connected?)")
def call(h, feat_idx: int, func: int, params: bytes = b"") -> bytes:
msg = bytes([0x11, DEV_IDX, feat_idx, (func << 4) | SW_ID]) + params
h.write(msg.ljust(20, b"\0"))
end = time.time() + 2
while time.time() < end:
r = h.read(64, 200)
if not r:
continue
if r[2] == 0xFF:
raise RuntimeError(f"HID++ error {r[5]:#x} (feature {r[3]:#x})")
if r[2] == feat_idx and r[3] == ((func << 4) | SW_ID):
return bytes(r[4:])
raise TimeoutError("no response from device")
def feature_index(h, feature_id: int) -> int | None:
idx = call(h, 0x00, 0, feature_id.to_bytes(2, "big"))[0]
return idx or None
def main() -> None:
p = argparse.ArgumentParser(prog="mx4-haptic", description=__doc__)
p.add_argument("waveform", nargs="?", type=int, help="waveform id to play (0-14)")
p.add_argument("--demo", action="store_true", help="play all waveforms in turn")
args = p.parse_args()
h = open_device()
try:
hap = feature_index(h, HAPTIC_FEATURE)
if hap is None:
sys.exit("Haptic feature 0x19B0 not supported by this device")
if args.demo:
for wf in range(15):
print("waveform", wf)
call(h, hap, 4, bytes([wf]))
time.sleep(1)
elif args.waveform is not None:
call(h, hap, 4, bytes([args.waveform]))
else:
p.print_help()
finally:
h.close()
Full source code available on GitHub. The script uses HID++, a vendor protocol developed by Logitech. BTW, there’s an entire OpenLogi initiative, an open source alternative to Logi Options+. The crucial part of the code is pushing a binary message to h device. The message contains one out of 15 different signals (params) which translate to different vibration patterns (“melodies”):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
VID, PID = 0x046D, 0xB042
DEV_IDX = 0xFF # direct (Bluetooth) device
def open_device() -> "hid.device":
for d in hid.enumerate(VID, PID):
if d["usage_page"] == HID_PP_USAGE_PAGE:
h = hid.device()
h.open_path(d["path"])
return h
h = open_device()
def call(h, feat_idx: int, func: int, params: bytes = b"") -> bytes:
msg = bytes([0x11, DEV_IDX, feat_idx, (func << 4) | SW_ID]) + params
h.write(msg.ljust(20, b"\0"))
Why?
You can run this script on any system action. One use case I thought of is running this script as a Claude Code CLI hook, on Stop event. Put this into your settings.json:
1
2
3
4
5
6
7
8
9
10
11
12
"hooks": {
"Stop": [
{
"hooks": [
{
"type": "command",
"command": "uv run mx4_haptic.py"
}
]
}
]
}
Remember how the Witcher’s medallion vibrated in the presence of monsters? Now your mouse can vibrate in the presence of AI slop!