create account

USB Keyboard Emulation with the Raspberry Pi Zero by makerhacks

View this thread on: hive.blogpeakd.comecency.com
· @makerhacks · (edited)
$11.45
USB Keyboard Emulation with the Raspberry Pi Zero
https://makerhacks.com/wp-content/uploads/2018/07/2018-07-18-17.51.21-e1532052915457.jpg

<p>One of the many productivity-boosters my nerd friends look to is text expanders and keyboard shortcuts.</p>
<p>You know, enter a combination of keypresses, and out pops a signature, particular animated gif, or an often-used regular expression.</p>

<p>I was about to launch into setting something like this up, and then Amazon Prime Day reminded me of the <a href="https://amzn.to/2uCtJ8l">Elgato Stream Deck</a>. It's a box of buttons for automating tasks using macros, usually as the name implies, for streaming shows for things like switching cameras and moderating the chat. </p>
<p>Yeah, it's pretty neat, but<em> just look</em> at the price.</p>
<p>Could I create (<a href="https://www.youtube.com/watch?v=lIFE7h3m40U">bodge</a>) something more basic for cheap? Heck yeah I can!</p>
<p>One of my mini-projects on my Steemit build blog was a <a href="https://steemit.com/programming/@makerhacks/emulate-a-usb-mouse-using-circult-playground-express-accelerometer-and-circuit-python">mouse emulator using the accelerometer</a> on the Circuit Playground Express. </p>
<p>For this, however, the Raspberry Pi offers more possibilites, especially if down the line I want to mimic the sneaky way the Elgato gets those graphical buttons.</p>
<h3>Thank you Random Nerd</h3>
<p>One of the wonderful things about the Raspberry Pi community is if you can think of a project, someone out there has done at least part of it.</p>
<p>This project was greatly helped by Random Nerd Tutorials. <a href="https://randomnerdtutorials.com/raspberry-pi-zero-usb-keyboard-hid/">This article</a> did most of the heavy lifting! Just follow the instructions to get your HID set up.</p>
<p>Next thing I needed was the codes for the keypresses I had in mind, and to wire up my big-assed button.</p>
<h3>Sending Keyboard character codes</h3>
<p>Using this document (Table 12) you can see each keyboard entry has a code. The system of communicating as a keyboard over USB requires these codes as part of an 8 byte message. </p>

<img src="https://makerhacks.com/wp-content/uploads/2018/07/Universal_Serial_Bus_HID_Usage_Tables.png" alt="" class="wp-image-114971" /><br/></figure>


<p>So to send the letter 'a', you would need to send the character code 4. </p>

<p>But how do you send upper case 'A'? Or combinations such as CTRL-C?</p>

<p><a href="https://www.rmedgar.com/blog/using-rpi-zero-as-keyboard-report-descriptor">This article</a> provided the answer. </p>

<blockquote class="wp-block-quote">
	<p>Input reports (sent from keyboard to computer) have the following structure for a total of 8 bytes:</p>

<ul>
	<li><strong>1 byte</strong>: modifier keys (Control, Shift, Alt, etc.), where each bit corresponds to a key</li>
	<li><strong>1 byte</strong>: unused/reserved for OEM</li>
	<li><strong>6 bytes</strong>: pressed key codes</li>
</ul>
</blockquote>

<p>Further into the article, there is a list of the modifiers, each represented by a bit:</p>

<ol>
	<li><code>Left Control</code>: 224 (0x00e0)</li>
	<li><code>Left Shift</code>: 225 (0x00e1)</li>
	<li><code>Left Alt</code>: 226 (0x00e2)</li>
	<li><code>Left Meta (Windows key)</code>: 227 (0x00e3)</li>
	<li><code>Right Control</code>: 228 (0x00e4)</li>
	<li><code>Right Shift</code>: 229 (0x00e5)</li>
	<li><code>Right Alt</code>: 230 (0x00e6)</li>
	<li><code>Right Meta (Windows key)</code>: 231 (0x00e7)</li>
</ol>

<p>For firing my screenshot app, I need CMD-SHIFT-5, so I need to set the 2nd and 4th bits on.</p>

<pre class="wp-block-preformatted">0b01010000</pre>
<h3>Buttons!</h3>

https://makerhacks.com/wp-content/uploads/2018/07/button.png

<p>You don't need an obnoxiously large red button like mine, any push button will do. In fact, you could just use wires and tap them together. The code just needs to see if the two pins are connected or not.</p>

<p>We simply connect one digital pin and ground. For my project, I chose pins 25 and 26.</p>

<p>Obviously, the Pi Zero needs to be connected to your computer with USB.</p>

<h3>Code</h3>

<p>All that is left is the code. As mentioned above, it takes heavily from the article I linked earlier, but with a check for the pin (in this case 26) going low (ie, connected to ground). </p>

<p>We send the data to the HID device, being careful to send the correct modifier bits, plus the encoded six bytes for the key press we wish to emulate.</p>

<p><a href="https://gist.github.com/omiq/48f305cfb9ef7c76748c7f9db4293145">Get the full code at this Gist here</a>.</p>

```
import time
import RPi.GPIO as GPIO

# We are going to use the BCM numbering
GPIO.setmode(GPIO.BCM)

# Set pin 26 as input using pull up resistor
GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_UP)


# function to send the data
def write_report(report):
    with open('/dev/hidg0', 'rb+') as fd:
        fd.write(report.encode())

# infinite loop to check the pins and send data
while True:
    if not(GPIO.input(26)):

        # shift-cmd-5
        shift_cmd_5 = str(0b01010000) + "\0\x22\0\0\0\0\0"
        write_report(shift_cmd_5)
        print("SNAP!!")
        time.sleep(0.2)
        write_report("\0\0\0\0\0\0\0\0")
```
 <br /><center><hr/>

https://steemitimages.com/0x0/https://cdn.steemitimages.com/DQmQt85v2egWPFwnp55DneenrwKCR6sQLinEfq3JAo68FwR/makerhacks.png

<em>Posted from my blog with <a href='https://wordpress.org/plugins/steempress/'>SteemPress</a> : https://makerhacks.com/usb-keyboard-emulation-with-the-raspberry-pi-zero/ </em><hr/></center>
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
authormakerhacks
permlinkusbkeyboardemulationwiththeraspberrypizero-7rtog0l8du
categorymaking
json_metadata{"community":"steempress","app":"steemit/0.1","image":["https://makerhacks.com/wp-content/uploads/2018/07/2018-07-18-17.51.21-e1532052915457.jpg","https://makerhacks.com/wp-content/uploads/2018/07/Universal_Serial_Bus_HID_Usage_Tables.png","https://makerhacks.com/wp-content/uploads/2018/07/button.png","https://steemitimages.com/0x0/https://cdn.steemitimages.com/DQmQt85v2egWPFwnp55DneenrwKCR6sQLinEfq3JAo68FwR/makerhacks.png"],"tags":["making","python","programming","raspberrypi","steemmakers"],"original_link":"https://makerhacks.com/usb-keyboard-emulation-with-the-raspberry-pi-zero/","links":["https://amzn.to/2uCtJ8l","https://www.youtube.com/watch?v=lIFE7h3m40U","https://steemit.com/programming/@makerhacks/emulate-a-usb-mouse-using-circult-playground-express-accelerometer-and-circuit-python","https://randomnerdtutorials.com/raspberry-pi-zero-usb-keyboard-hid/","https://www.rmedgar.com/blog/using-rpi-zero-as-keyboard-report-descriptor","https://gist.github.com/omiq/48f305cfb9ef7c76748c7f9db4293145","https://wordpress.org/plugins/steempress/","https://makerhacks.com/usb-keyboard-emulation-with-the-raspberry-pi-zero/"],"format":"markdown"}
created2018-07-20 02:25:21
last_update2018-07-20 02:31:12
depth0
children1
last_payout2018-07-27 02:25:21
cashout_time1969-12-31 23:59:59
total_payout_value8.775 HBD
curator_payout_value2.674 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length5,532
author_reputation156,478,986,501,043
root_title"USB Keyboard Emulation with the Raspberry Pi Zero"
beneficiaries
0.
accountsteempress-io
weight1,500
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id65,310,179
net_rshares6,199,966,959,084
author_curate_reward""
vote details (44)
@steemmakers ·
<div class='pull-right'><center><a href='http://www.steemmakers.com'><img src='https://www.steemmakers.com/img/comment_logo_makers.png' /></a></center></div><b>Congratulations</b> This post has been upvoted by SteemMakers. We are a community-based project that aims to support makers and DIYers on the blockchain in every way possible. <br/><br/>Join our <a href='https://discord.gg/EFGbRuW'>Discord Channel</a> to connect with us and nominate your own or somebody else's posts in our review channel.<br/><br/><b>Help us to reward you for making it !</b> Join <a href='https://www.steemmakers.com/#/Trail'>our voting trail</a> or <a href='https://www.steemmakers.com/#/Delegation'>delegate steem power</a> to the community account. <br/><br/>Your post is also presented on the community website <a href='http://www.steemmakers.com'>www.steemmakers.com</a> where you can find other selected content. <br/><br/>If you like our work, please consider upvoting this comment to support the growth of our community. Thank you.
properties (22)
authorsteemmakers
permlinkre-makerhacks-usbkeyboardemulationwiththeraspberrypizero-7rtog0l8du-20180723t091633293z
categorymaking
json_metadata""
created2018-07-23 09:16:24
last_update2018-07-23 09:16:24
depth1
children0
last_payout2018-07-30 09:16:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,019
author_reputation1,907,312,584,548
root_title"USB Keyboard Emulation with the Raspberry Pi Zero"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id65,674,809
net_rshares0