├── LICENSE ├── README.md └── usb-gadget-multimedia-keys.py /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [2022] [Farley / Neon Surge / DevOps Nirvana] [farley _at_ neonsurge (dot) com] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Send Multimedia USB HID Keys via Python 2 | As an USB Gadget in Linux 3 | 4 | This gives a simple script with zero dependencies that can easily run on any Linux device (eg: Raspberry Pi) that 5 | is setup to emulate an USB Gadget, and send Multimedia Key presses such as Volume Up, Play, Next Song, etc. The author 6 | found this code/example didn't exist and especially not in Python. 7 | 8 | ## Purpose 9 | 10 | 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. 11 | 12 | This code is wonderful when paired with a tool such as [Triggerhappy](https://github.com/wertarbyte/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. 13 | 14 | ## Setup / Installation 15 | 16 | 1. Setup a fresh Linux device (eg: Raspberry Pi) 17 | 1. Follow walkthrough [here](https://mtlynch.io/key-mime-pi/) to setup your device as an USB Gadget 18 | 1. Use an editor and edit the file that the above walkthrough created at `/opt/enable-rpi-hid`, update the line 35 from... 19 | 20 | ``` 21 | 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" 22 | ``` 23 | And replace it with this line which adds the extra HID device "multimedia" profile to your USB HID Gadget. Found this info [here](https://www.stefanjones.ca/blog/arduino-leonardo-remote-multimedia-keys/). 24 | ``` 25 | 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" 26 | ``` 27 | 28 | 1. After the above manual change, reboot your device 29 | 1. 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. 30 | 1. Download this script with `wget https://raw.githubusercontent.com/DevOps-Nirvana/python-usb-gadget-send-multimedia-hid-commands/master/usb-gadget-multimedia-keys.py` 31 | 1. Allow execute with `chmod a+x usb-gadget-multimedia-keys.py` 32 | 33 | ## Usage (CLI) 34 | 35 | ```bash 36 | # Run the command, asking for help 37 | ./usb_gadget_multimedia_keys.py -h 38 | # Try sending volume up 39 | ./usb_gadget_multimedia_keys.py -k VOLUME_UP 40 | ``` 41 | 42 | ## Installation 43 | 44 | Now that you've confirmed it works, to install it on your system, a good recommended place is to 45 | 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. 46 | 47 | ```bash 48 | # Move this to the right folder 49 | mv ./usb_gadget_multimedia_keys.py /usr/local/bin 50 | # Try this globally now with... 51 | usb_gadget_multimedia_keys.py -h 52 | ``` 53 | 54 | ## Usage (Triggerhappy) 55 | 56 | See: https://github.com/wertarbyte/triggerhappy 57 | 58 | Using a sample configuration such as... 59 | 60 | ``` 61 | # FORMAT: 62 | # This is the + key on an extended keyboard on the number pad 63 | KEY_KPPLUS 1 /usr/local/bin/usb_gadget_multimedia_keys.py -k VOLUME_UP 64 | # This is the - key on an extended keyboard on the number pad 65 | KEY_KPMINUS 1 /usr/local/bin/usb_gadget_multimedia_keys.py -k VOLUME_DOWN 66 | ``` 67 | 68 | ## Manual / Manpage 69 | 70 | ```bash 71 | root@localhost$ ./usb_gadget_multimedia_keys.py -h 72 | Usage: usb_gadget_multimedia_keys.py -k VOLUME_UP 73 | 74 | Options: 75 | -h, --help show this help message and exit 76 | -k KEYPRESS, --keypress=KEYPRESS 77 | Key to send to USB Gadget Keyboard device, must be one 78 | of (WAKE, SCRUB_FORWARD, SCRUB_BACKWARD, NEXT_SONG, 79 | PREVIOUS_SONG, STOP, PLAY, MUTE, VOLUME_UP, 80 | VOLUME_DOWN) 81 | -d DEVICE, --hid-device=DEVICE 82 | What HID device to use (Default: /dev/hidg0) 83 | -v, --verbose If we want to print some more stuff, it's fairly quiet 84 | without this 85 | -w, --wake If we want to send an internal/unprintable/unused 86 | keypress to trigger a 'wake' on the device (eg. iPad) 87 | before sending the desired key press, this can be 88 | useful on devices which sleep for battery/energy 89 | purposes to wake them first so they fully process the 90 | keypress. Eg: While asleep on an iPad if you press 91 | 'next song' it will simply wake, and not go to the 92 | next song 93 | ``` 94 | 95 | ## Author / Support 96 | 97 | Authored by Farley _at_ (neonsurge) **dot** com 98 | 99 | This is considered open source code, do whatever you want with it. File issues if you have them, or email me if you want. 100 | 101 | ## References 102 | 103 | Loosely inspired/based on the [Key Mime Pi](https://mtlynch.io/key-mime-pi/) project and its [accompanying repo](https://github.com/mtlynch/key-mime-pi). Key/control/gadget/device setup code found with lots of googling and research from pages found such as... 104 | * https://www.stefanjones.ca/blog/arduino-leonardo-remote-multimedia-keys/ 105 | * https://forum.arduino.cc/t/keyboard-library-sending-multimedia-keys/428068 106 | * https://www.instructables.com/USB-Volume-Control-and-Caps-Lock-LED-Simple-Cheap-/ 107 | * https://gist.github.com/MightyPork/6da26e382a7ad91b5496ee55fdc73db2 108 | * https://github.com/adafruit/Adafruit_CircuitPython_HID/blob/main/adafruit_hid/consumer_control.py 109 | * https://github.com/adafruit/Adafruit_CircuitPython_HID/blob/main/adafruit_hid/consumer_control_code.py 110 | * https://uboopenfactory.univ-brest.fr/Les-Labs/MusicLab/Projets/Arduino-Media-Keys 111 | -------------------------------------------------------------------------------- /usb-gadget-multimedia-keys.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | import os 3 | import time 4 | from optparse import OptionParser 5 | 6 | # What HID device to use, defaults to hidg0 for easier/less configuration 7 | DEFAULT_HID = '/dev/hidg0' 8 | # Note: Multimedia devices are hardcoded to send the control code of 0x02 9 | # in contrast to keyboard stuff which lives on 0x00, the other thing 10 | # that differs for multimedia devices is using the reserved bit [1] 11 | # for the button code, instead of [2] which keyboards use 12 | CONTROL_CODE = 0x02 13 | DEBUG = False 14 | 15 | KEYS_ALLOWED = { 16 | 'WAKE': {'rsvd': 0, 'ctrl': 0x00, 'kbd': 0x81, 'delay': 2, 'ctrl': 0x00}, # Basically just send a "empty/invalid" keypress, to "wake" whatever device before sending a real key below 17 | 'SCRUB_FORWARD': {'rsvd': 1, 'ctrl': CONTROL_CODE, 'kbd': 0x00, 'delay': 1.2, 'ctrl': CONTROL_CODE}, # Basically, a "long-press" of the next-song button, about 1.2 seconds seems to be the most reliable 18 | 'FORWARD': {'rsvd': 1, 'ctrl': CONTROL_CODE, 'kbd': 0x00, 'delay': 1.2, 'ctrl': CONTROL_CODE}, # Alias of above 19 | 'SCRUB_BACKWARD': {'rsvd': 2, 'ctrl': CONTROL_CODE, 'kbd': 0x00, 'delay': 1.2, 'ctrl': CONTROL_CODE}, # Basically, a "long-press" of the previous-song button 20 | 'BACKWARD': {'rsvd': 2, 'ctrl': CONTROL_CODE, 'kbd': 0x00, 'delay': 1.2, 'ctrl': CONTROL_CODE}, # Alias of above 21 | 'NEXT_SONG': {'rsvd': 1, 'ctrl': CONTROL_CODE, 'kbd': 0x00, 'delay': 0.1, 'ctrl': CONTROL_CODE}, 22 | 'NEXT': {'rsvd': 1, 'ctrl': CONTROL_CODE, 'kbd': 0x00, 'delay': 0.1, 'ctrl': CONTROL_CODE}, # Alias of above 23 | 'PREVIOUS_SONG': {'rsvd': 2, 'ctrl': CONTROL_CODE, 'kbd': 0x00, 'delay': 0.1, 'ctrl': CONTROL_CODE}, 24 | 'BACK': {'rsvd': 2, 'ctrl': CONTROL_CODE, 'kbd': 0x00, 'delay': 0.1, 'ctrl': CONTROL_CODE}, # Alias of above 25 | 'STOP': {'rsvd': 4, 'ctrl': CONTROL_CODE, 'kbd': 0x00, 'delay': 0.1, 'ctrl': CONTROL_CODE}, # Not all devices respond to this, Eg: iPads do not. Use play(pause) below 26 | 'PLAY': {'rsvd': 8, 'ctrl': CONTROL_CODE, 'kbd': 0x00, 'delay': 0.1, 'ctrl': CONTROL_CODE}, # This is play/pause, but simplified the name 27 | 'MUTE': {'rsvd': 16, 'ctrl': CONTROL_CODE, 'kbd': 0x00, 'delay': 0.1, 'ctrl': CONTROL_CODE}, # This acts as mute and unmute, simplified the name 28 | 'VOLUME_UP': {'rsvd': 32, 'ctrl': CONTROL_CODE, 'kbd': 0x00, 'delay': 0.1, 'ctrl': CONTROL_CODE}, 29 | 'VOLUME_DOWN': {'rsvd': 64, 'ctrl': CONTROL_CODE, 'kbd': 0x00, 'delay': 0.1, 'ctrl': CONTROL_CODE}, 30 | } 31 | 32 | # Helper script to send data directly to our HID Gadget emulation device 33 | def send_to_gadget(hid_path, reserved_code, control_code=CONTROL_CODE, keyboard_code=0x00): 34 | if DEBUG: 35 | print("Sending {}:{}:{} to {}".format(control_code, reserved_code, keyboard_code, hid_path)) 36 | 37 | with open(hid_path, 'wb+') as hid_handle: 38 | buf = [0] * 8 39 | buf[0] = control_code 40 | buf[1] = reserved_code 41 | buf[2] = keyboard_code 42 | hid_handle.write(bytearray(buf)) 43 | hid_handle.write(bytearray([0] * 8)) 44 | 45 | # Helper script to translate KEYS_ALLOWED into the above 46 | def send(key_name, hid_path=DEFAULT_HID): 47 | if DEBUG: 48 | print("Requested to send {} to {}".format(key_name, hid_path)) 49 | # Sending keypress (down) 50 | send_to_gadget(hid_path, reserved_code=KEYS_ALLOWED[key_name]['rsvd'], control_code=KEYS_ALLOWED[key_name]['ctrl'], keyboard_code=KEYS_ALLOWED[key_name]['kbd']) 51 | # Wait 52 | if DEBUG: 53 | print("Waiting {} second(s)...".format(KEYS_ALLOWED[key_name]['delay'])) 54 | time.sleep(KEYS_ALLOWED[key_name]['delay']) 55 | # Send end of keypress (up) 56 | send_to_gadget(hid_path, reserved_code=0) 57 | 58 | 59 | # If run via the CLI 60 | if __name__ == '__main__': 61 | # Setup usage arguments/switches 62 | usage = "usage: %prog -k VOLUME_UP" 63 | parser = OptionParser(usage=usage) 64 | parser.add_option("-k", "--keypress", 65 | dest="keypress", 66 | default="", 67 | help="Key to send to USB Gadget Keyboard device, must be one of ({})".format(", ".join(KEYS_ALLOWED)), 68 | ) 69 | parser.add_option("-d", "--hid-device", 70 | dest="device", 71 | default=DEFAULT_HID, 72 | help="What HID device to use (Default: {})".format(DEFAULT_HID), 73 | ) 74 | parser.add_option("-v", "--verbose", 75 | dest="verbose", 76 | action="store_true", 77 | default=False, 78 | help="If we want to print some more stuff, it's fairly quiet without this" 79 | ) 80 | parser.add_option("-w", "--wake", 81 | dest="wake", 82 | action="store_true", 83 | default=False, 84 | help="""If we want to send an internal/unprintable/unused keypress to trigger a 'wake' on the device 85 | (eg. iPad) before sending the desired key press, this can be useful on devices which sleep for battery/energy purposes to wake them first 86 | 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""" 87 | ) 88 | (options, args) = parser.parse_args() 89 | 90 | #### Startup simple checks... 91 | # Validate keypress is set validly 92 | if options.keypress == "": 93 | print("ERROR: You MUST specify the keypress to send to an USB Gadget HID device with -k") 94 | parser.print_usage() 95 | exit(1) 96 | if options.keypress not in KEYS_ALLOWED: 97 | print("ERROR: You MUST specify the a valid keypress from this list: ({})".format(", ".join(KEYS_ALLOWED))) 98 | parser.print_usage() 99 | exit(1) 100 | # Validate device is set, this is set by default though, but just incase 101 | if options.device == "": 102 | print("ERROR: There somehow isn't an valid device specified. Please specify one with -d") 103 | parser.print_usage() 104 | exit(1) 105 | # If we want to print out what we're doing as we're doing it 106 | if options.verbose: 107 | DEBUG=True 108 | # If we want to send an unprintable escape sequence ahead of time to "wake" the device (Eg: iPad energy saving mode) 109 | if options.wake: 110 | send('WAKE', options.device) 111 | 112 | # Finally, send our desired command 113 | send(options.keypress, options.device) 114 | --------------------------------------------------------------------------------