Example code to sending USB Gadget multimedia keys via Python

Overview

Send Multimedia USB HID Keys via Python

As an USB Gadget in Linux

This gives a simple script with zero dependencies that can easily run on any Linux device (eg: Raspberry Pi) that is setup to emulate an USB Gadget, and send Multimedia Key presses such as Volume Up, Play, Next Song, etc. The author found this code/example didn't exist and especially not in Python.

Purpose

This code was made for some automation and integration of mobile devices to an Raspberry Pi via an USB interface. Specifically, this was designed for an iPad as a kiosk with external inputs to allow for various input and sensors from Raspberry Pi to trigger/inform things on the iPad. This especially helps you do lots if you use the "Shortcuts iOS App" paired with enabling "Full Keyboard Support" on your iPad.

This code is wonderful when paired with a tool such as Triggerhappy to remap keys/input to other keys and forward those keyboard presses into the Host USB Device via USB Gadget mode on Linux. This code also acts as an simple library, so it can easily be imported in other Python scripts.

Setup / Installation

  1. Setup a fresh Linux device (eg: Raspberry Pi)
  2. Follow walkthrough here to setup your device as an USB Gadget
  3. Use an editor and edit the file that the above walkthrough created at /opt/enable-rpi-hid, update the line 35 from...
echo -ne \\x05\\x01\\x09\\x06\\xa1\\x01\\x05\\x07\\x19\\xe0\\x29\\xe7\\x15\\x00\\x25\\x01\\x75\\x01\\x95\\x08\\x81\\x02\\x95\\x01\\x75\\x08\\x81\\x03\\x95\\x05\\x75\\x01\\x05\\x08\\x19\\x01\\x29\\x05\\x91\\x02\\x95\\x01\\x75\\x03\\x91\\x03\\x95\\x06\\x75\\x08\\x15\\x00\\x25\\x65\\x05\\x07\\x19\\x00\\x29\\x65\\x81\\x00\\xc0 > "${FUNCTIONS_DIR}/report_desc"

And replace it with this line which adds the extra HID device "multimedia" profile to your USB HID Gadget. Found this info here.

echo -ne \\x05\\x01\\x09\\x06\\xa1\\x01\\x05\\x07\\x19\\xe0\\x29\\xe7\\x15\\x00\\x25\\x01\\x75\\x01\\x95\\x08\\x81\\x02\\x95\\x01\\x75\\x08\\x81\\x03\\x95\\x05\\x75\\x01\\x05\\x08\\x19\\x01\\x29\\x05\\x91\\x02\\x95\\x01\\x75\\x03\\x91\\x03\\x95\\x06\\x75\\x08\\x15\\x00\\x25\\x65\\x05\\x07\\x19\\x00\\x29\\x65\\x81\\x00\\xc0\\x05\\x0c\\x09\\x01\\xa1\\x01\\x85\\x02\\x05\\x0c\\x15\\x00\\x25\\x01\\x75\\x01\\x95\\x07\\x09\\xb5\\x09\\xb6\\x09\\xb7\\x09\\xcd\\x09\\xe2\\x09\\xe9\\x09\\xea\\x81\\x02\\x95\\x01\\x81\\x01\\xc0 > "${FUNCTIONS_DIR}/report_desc"
  1. After the above manual change, reboot your device
  2. Plug your Linux Device (eg: Raspberry Pi) into a USB host device (eg: iPhone, iPad, Mac/PC/Linux). Note: You may need an USB "OTG" cable. Also note: Not ALL USB ports (especially on the Raspberry Pi) can act as USB OTG, only a specific port.
  3. Download this script with wget https://raw.githubusercontent.com/DevOps-Nirvana/python-usb-gadget-send-multimedia-hid-commands/master/usb-gadget-multimedia-keys.py
  4. Allow execute with chmod a+x usb-gadget-multimedia-keys.py

Usage (CLI)

# Run the command, asking for help
./usb_gadget_multimedia_keys.py -h
# Try sending volume up
./usb_gadget_multimedia_keys.py -k VOLUME_UP

Installation

Now that you've confirmed it works, to install it on your system, a good recommended place is to put it in /usr/local/bin. On most Unix-ey machines this is already in your PATH, so you can simply download and chmod +x it to begin using it.

# Move this to the right folder
mv ./usb_gadget_multimedia_keys.py /usr/local/bin
# Try this globally now with...
usb_gadget_multimedia_keys.py -h

Usage (Triggerhappy)

See: https://github.com/wertarbyte/triggerhappy

Using a sample configuration such as...

# FORMAT: 
   	
    	
     
# This is the + key on an extended keyboard on the number pad
KEY_KPPLUS	1		/usr/local/bin/usb_gadget_multimedia_keys.py -k VOLUME_UP
# This is the - key on an extended keyboard on the number pad
KEY_KPMINUS	1		/usr/local/bin/usb_gadget_multimedia_keys.py -k VOLUME_DOWN

    
   

Manual / Manpage

[email protected]$ ./usb_gadget_multimedia_keys.py  -h
Usage: usb_gadget_multimedia_keys.py -k VOLUME_UP

Options:
  -h, --help            show this help message and exit
  -k KEYPRESS, --keypress=KEYPRESS
                        Key to send to USB Gadget Keyboard device, must be one
                        of (WAKE, SCRUB_FORWARD, SCRUB_BACKWARD, NEXT_SONG,
                        PREVIOUS_SONG, STOP, PLAY, MUTE, VOLUME_UP,
                        VOLUME_DOWN)
  -d DEVICE, --hid-device=DEVICE
                        What HID device to use (Default: /dev/hidg0)
  -v, --verbose         If we want to print some more stuff, it's fairly quiet
                        without this
  -w, --wake            If we want to send an internal/unprintable/unused
                        keypress to trigger a 'wake' on the device (eg. iPad)
                        before sending the desired key press, this can be
                        useful on devices which sleep for battery/energy
                        purposes to wake them first so they fully process the
                        keypress.  Eg: While asleep on an iPad if you press
                        'next song' it will simply wake, and not go to the
                        next song

Author / Support

Authored by Farley at (neonsurge) dot com

This is considered open source code, do whatever you want with it. File issues if you have them, or email me if you want.

References

Loosely inspired/based on the Key Mime Pi project and its accompanying repo. Key/control/gadget/device setup code found with lots of googling and research from pages found such as...

Owner
DevOps Nirvana
What happens when you set everything up perfectly? Nirvana happens
DevOps Nirvana
A circle of LEDs

This repository contains all the design files, production files and example code for a simple circular LED display.

Pim de Groot 15 Aug 21, 2022
Pure micropython ESP32 SPI driver for sdcard and screen at the same SPI bus

micropython-esp32-spi-sdcard-and-screen-driver Proof of concept of Pure micropython espidf SPI driver for sdcard with screen at the same SPI bus (exam

Thomas Favennec 7 Mar 14, 2022
🏡 My Home Assistant Configs. Be sure to 🌟 my repo to follow the updates!

Home Assistant Configuration Here's my Home Assistant configuration. I have installed HA on a Lenovo ThinkCentre M93P Tiny with an Intel Dual-Core i5-

iLyas Bakouch 25 Dec 30, 2022
a library for using WS2812b leds (aka neopixels) with Raspberry Pi Pico

pico_ws2812b a library for using WS2812b leds (aka neopixels) with Raspberry Pi Pico You'll first need to save the ws2812b.py file to your device (for

76 Nov 25, 2022
ROS2 nodes for Waveshare Alphabot2-Pi mobile robot.

ROS2 for Waveshare Alphabot2-Pi This repo contains ROS2 packages for the Waveshare Alphabot2-Pi mobile robot: alphabot2: it contains the nodes used to

Michele Rizzo 2 Oct 11, 2022
Iec62056-21-mqtt - Publish DSMR P1 telegrams acquired over IEC62056-21 to MQTT

IEC 62056-21 Publish DSMR P1 telegrams acquired over IEC62056-21 to MQTT. -21 is

Marijn Suijten 1 Jun 05, 2022
rPico KMK powered macropad with IPS screen

MacroPact rPico KMK powered macropad with IPS screen Idea/Desing: Sean Yin Build/Coding: kbjunky ( In case of any problems hit me up on Discord kbjunk

81 Dec 21, 2022
A blender 2.9x addon for managing camera settings

TMG-Camera-Tools A blender 2.9x addon for managing camera settings Tutorial showcasing current features

Mainman002 12 Apr 16, 2022
Controlling fireworks with micropython

Controlling-fireworks-with-micropython How the code works line 1-4 from machine

Montso Mokake 1 Jan 08, 2022
智能无人机路径规划仿真系统是一个具有操作控制精细、平台整合性强、全方向模型建立与应用自动化特点的软件

Drone智能无人机路径规划仿真系统是一个具有操作控制精细、平台整合性强、全方向模型建立与应用自动化特点的软件。它以A、B两国在C区开展无人机战争为背景,该系统的核心功能是通过仿真平台规划无人机航线,并进行验证输出,数据可导入真实无人机,使其按照规定路线精准抵达战场任一位置,支持多人多设备编队联合行动。

wwy 349 Jan 03, 2023
iot-dashboard: Fully integrated architecture platform with a dashboard for Logistics Monitoring, Internet of Things.

Fully integrated architecture platform with a dashboard for Logistics Monitoring, Internet of Things. Written in Python. Flask applicati

2 Jul 29, 2022
A python script to poll RPi GPIO pins and subscribe and publish their state via MQTT

MQTT-GPIO A python script to poll RPi GPIO pins and subscribe and publish their state via MQTT using TLS. This script is short and meant to be edited

23 Oct 12, 2021
Examples to accompany the

Examples to accompany the "Raspberry Pi Pico Python SDK" book published by Raspberry Pi Trading, which forms part of the technical documentation in support of Raspberry Pi Pico and the MicroPython po

Raspberry Pi 589 Jan 08, 2023
A Simple Python KeyLogger App

✨ Kurulum Uygulamayı bilgisayarınızda kullana bilmek için bazı işlemler yapmanız gerekiyor. Aşağıdaki yönlendirmeleri takip ederek bunu yapabilirsiniz

VorteX 7 Jun 11, 2022
Point Density-Aware Voxels for LiDAR 3D Object Detection (CVPR 2022)

PDV PDV is LiDAR 3D object detection method. This repository is based off [OpenPCDet]. Point Density-Aware Voxels for LiDAR 3D Object Detection Jordan

Toronto Robotics and AI Laboratory 114 Dec 21, 2022
Home Assistant integration for energy consumption data from UK SMETS (Smart) meters using the Hildebrand Glow API.

Hildebrand Glow (DCC) Integration Home Assistant integration for energy consumption data from UK SMETS (Smart) meters using the Hildebrand Glow API. T

Aniket 153 Dec 30, 2022
Mini Pupper - Open-Source,ROS Robot Dog Kit

Mini Pupper - Open-Source,ROS Robot Dog Kit

MangDang 747 Dec 28, 2022
Pinion — Nice-looking interactive diagrams for KiCAD PCBs

Pinion — Nice-looking interactive diagrams for KiCAD PCBs Pinion is a simple tool that allows you to make a nice-looking pinout diagrams for your PCBs

Jan Mrázek 297 Jan 06, 2023
NYCT-GTFS - Real-time NYC subway data parsing for humans

NYCT-GTFS - Real-time NYC subway data parsing for humans This python library provides a human-friendly, native python interface for dealing with the N

Andrew Dickinson 37 Dec 27, 2022
Simple Microservice to control 433Mhz wireless sockets over HTTP, e.g. on a RaspberryPi

REST-light is a simple microservice to control 433Mhz wireless sockets over HTTP, e.g. on a RaspberryPi. The main usage is an easy integration of 433M

Pascal Höhnel 1 Jan 09, 2022