run script when plugged into usb

June 4, 2024, 19:11

thatoneham

i need a python script to run
python
 
import time
import json
HID_DEVICE_PATH = "dev/hidg0"

def send_keys(keys):
    report =[0x00,0x00, keys,0x00,0x00,0x00]
    with open (HID_DEVICE_PATH, 'wb') as fd:
        fd.write(bytearray(report))
        time.sleep(0.01)
        
fd.write(bytearray[0x00] * 8)

def main():
    f = open("keys.json")
    data = json.load(f)
    for i in data:
        send_keys(i)
    f.close()
if __name__ == "__name__":
    main()
when i plug it into a usb socket. currently i have usb-gadget setup, in /etc/systemd/system/usb-gadget.service i have the script
[Unit]
Description=Run Python script on USB gadget connection
After=network.target

[Service]
ExecStart=/usr/bin/python3 /admin/home/start_keys.py
WorkingDirectory=/admin/home
StandardOutput=inherit
StandardError=inherit
Restart=always
User=admin

[Install]
WantedBy=multi-user.target
which i found and modified for my own use, the python script returns no errors, but i keep getting the error

oops.se

Post text as text!

thatoneham

?

thatoneham

oh im using putty and it wont let me copy paste stuff from it

nayab5655

What is your projects goal?

thatoneham

bad usb to troll friend, and as a simple thing to learn how to use raspberry pi

thatoneham

ive looked into the error and it says

thatoneham

/dev/hidg0 doenst exist?