Keyboard Shortcut, Qwiic Keypad
Arduino Example
Arduino Library
The easiest way to install the Arduino libraries are by searching SparkFun Qwiic Keypad and HID-Project inside the Arduino library manager. To manually install, head on over to their respective GitHub repositories:
or feel free to download the libraries below:
Using the Qwiic Keypad Library
The functionality of the Qwiic Keypad library is pretty straight forward. The example code, primarily draws from the Example1_ReadButton.ino
sketch. Below is a quick summary of the library functions that are used in the example code.
keypad1.begin()
- Used to connect to Qwiic Keypad.keypad1.updateFIFO()
- Used to increment the FIFO.char button = keypad1.getButton()
- Used to read button press from FIFO with value stored as a character in the variable button.
Using the HID-Project Library
The HID-Project library, built by NicoHood work similarly to the Arduino Keyboard library. It is also compatible with the original key definitions, just make sure you use the name, not the number. (i.e Keyboard.write(0xB0)
will not work, use Keyboard.write(KEY_RETURN)
instead.)
Button Combinations
To create shortcut using button combinations, find out the button combination for your shortcut. These should be listed in the user manual for your program or operating system; otherwise, the internet is a great resource. Make sure to test that the shortcut works before programming it.
To code your shortcut in Arduino, use the Keyboard.press(Define_Key)
function, designating the individual key. To press multiple keys together, call this function again while designating each of the individual keys. Then use Keyboard.releaseAll()
to release all the keys pressed at once. Please refer to the ImprovedKeylayouts.h header file from the HID-Project library for the naming convention (or keycode) of each keyboard key.
Example: Ctrl + Alt + F1
Code:
Keyboard.press(KEY_CTRL); Keyboard.press(KEY_F1); Keyboard.press(KEY_ALT); Keyboard.releaseAll();
Functional Buttons
To create a functional shortcut in Arduino, use the Consumer.write(Define_Key)
function, designating the functional key to implement. Please refer to the ConsumerAPI.h header file from the HID-Project library for the naming convention (or keycode) of the available keys.
Example:
Code:
Consumer.write(MEDIA_VOLUME_DOWN); delay(50); Keyboard.releaseAll();
Text String
To type out a string of text in Arduino, use the Keyboard.print("Text")
or Keyboard.println("Text")
function.
<table> <tr> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td></td> <td></td> </tr> </table>
Keyboard.println("<table border=\"1\">"); Keyboard.println(" <tr style=\"padding:10px\">"); Keyboard.println(" <td style=\"padding:10px\" valign=\"center\"></td>"); Keyboard.println(" <td style=\"padding:10px\" valign=\"center\"></td>"); Keyboard.println(" <td style=\"padding:10px\" valign=\"center\"></td>"); Keyboard.println(" </tr>"); Keyboard.println(" <tr style=\"padding:10px\">"); Keyboard.println(" <td style=\"padding:10px\" valign=\"top\"></td>"); Keyboard.println(" <td style=\"padding:10px\"></td>"); Keyboard.println(" <td style=\"padding:10px\"></td>"); Keyboard.println(" </tr>"); Keyboard.println(" <tr style=\"padding:10px\">"); Keyboard.println(" <td></td>"); Keyboard.println(" <td></td>"); Keyboard.println(" <td></td>"); Keyboard.println(" </tr>"); Keyboard.println("</table>");
Example Code
In the example code below I have implemented specific keyboard shortcuts for my personal needs. You will also notice, that in certain instances, I have combined a series of different shortcuts for greater functionality.
/* Hot-Shortcut KeyPad By: Wes Furuya SparkFun Electronics Date: March 27th, 2019 License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license). Feel like supporting our work? Buy a board from SparkFun! https://www.sparkfun.com/products/14641 https://www.sparkfun.com/products/14812 This sketch allows the RedBoard Turbo to emulate a HID keyboard. The buttons on the Qwiic Keypad are set for specific keyboard entries. For more details, please refer to the project guide: https://learn.sparkfun.com/tutorials/keyboard-shortcut-qwiic-keypad */ // KEYPAD keypad1; //Create instance of this object void setup(void) { // Initializes serial output // SerialUSB is used for debug messages to the serial monitor SerialUSB.begin(9600); delay(3000); SerialUSB.println("Qwiic KeyPad Example"); // Connects Turbo to the Qwiic Keypad if (keypad1.begin() == false) // Note, using begin() like this will use default I2C address, 0x4B. // You can pass begin() a different address like so: keypad1.begin(Wire, 0x4A). { SerialUSB.println("Keypad does not appear to be connected. Please check wiring. Freezing..."); while (1); } SerialUSB.print("Initialized. Firmware Version: "); SerialUSB.println(keypad1.getVersion()); SerialUSB.println("Press a button: * to do a space. // Initializes keyboard functions Keyboard.begin(); Consumer.begin(); } void loop(void) { // Checks for next keypad press keypad1.updateFIFO(); // necessary for keypad to pull button from stack to readable register delay(50); char button = keypad1.getButton(); if (button == -1) { SerialUSB.println("No keypad detected"); delay(1000); } else if (button != 0) { SerialUSB.print(button); if (button == '1') { //Mute Button Consumer.write(MEDIA_VOLUME_MUTE); Keyboard.releaseAll(); } else if (button == '2') { //Volume Down Consumer.write(MEDIA_VOLUME_DOWN); Keyboard.releaseAll(); } else if (button == '3') { //Volume Up Consumer.write(MEDIA_VOLUME_UP); Keyboard.releaseAll(); } else if (button == '4') { //Calculator Consumer.write(CONSUMER_CALCULATOR); Keyboard.releaseAll(); } else if (button == '5') { //Used custom shortcut to launch Snipping tool //In shorcut properties, set to: Ctrl+Alt+Insert Keyboard.press(KEY_LEFT_CTRL); Keyboard.press(KEY_LEFT_ALT); Keyboard.press(KEY_INSERT); Keyboard.releaseAll(); } else if (button == '6') { //Windows Button Keyboard.press(KEY_LEFT_WINDOWS); Keyboard.releaseAll(); delay(50); //Enters "ter" into search Keyboard.println("ter"); delay(50); //Presses enter button to select entry //On my computer, this is TeraTerm Keyboard.press(KEY_ENTER); Keyboard.releaseAll(); } else if (button == '7') { //Cuts selected entry Keyboard.press(KEY_LEFT_CTRL); Keyboard.press(KEY_X); Keyboard.releaseAll(); delay(50); //Types text Keyboard.print("<!-- product_big("); delay(50); //Pastes entry Keyboard.press(KEY_LEFT_CTRL); Keyboard.press(KEY_V); Keyboard.releaseAll(); delay(50); //Types text Keyboard.println(") -->"); Keyboard.print("<!-- products_by_id("); delay(50); //Pastes entry Keyboard.press(KEY_LEFT_CTRL); Keyboard.press(KEY_V); Keyboard.releaseAll(); delay(50); //Types text Keyboard.println("ID1, ID2) -->"); Keyboard.println("<div class=\"clearfix\"></div>"); } else if (button == '8') { //Types text Keyboard.println("<table border=\"1\">"); Keyboard.println(" <tr style=\"padding:10px\">"); Keyboard.println(" <td style=\"padding:10px\" valign=\"center\"></td>"); Keyboard.println(" <td style=\"padding:10px\" valign=\"center\"></td>"); Keyboard.println(" <td style=\"padding:10px\" valign=\"center\"></td>"); Keyboard.println(" </tr>"); Keyboard.println(" <tr style=\"padding:10px\">"); Keyboard.println(" <td style=\"padding:10px\" valign=\"top\"></td>"); Keyboard.println(" <td style=\"padding:10px\"></td>"); Keyboard.println(" <td style=\"padding:10px\"></td>"); Keyboard.println(" </tr>"); Keyboard.println(" <tr style=\"padding:10px\">"); Keyboard.println(" <td></td>"); Keyboard.println(" <td></td>"); Keyboard.println(" <td></td>"); Keyboard.println(" </tr>"); Keyboard.println("</table>"); } else if (button == '9') { //Cuts selected entry Keyboard.press(KEY_LEFT_CTRL); Keyboard.press(KEY_X); Keyboard.releaseAll(); delay(50); //Types text Keyboard.print("<div class=\"alert alert-info\"><b>Note:</b>"); delay(50); //Pastes entry Keyboard.press(KEY_LEFT_CTRL); Keyboard.press(KEY_V); Keyboard.releaseAll(); delay(50); //Types text Keyboard.println("</div>"); } else if (button == '*') { //Cuts selected entry Keyboard.press(KEY_LEFT_CTRL); Keyboard.press(KEY_X); Keyboard.releaseAll(); delay(50); //Types text Keyboard.print("-> [![alt text]("); delay(50); //Pastes entry Keyboard.press(KEY_LEFT_CTRL); Keyboard.press(KEY_V); Keyboard.releaseAll(); delay(50); //Types text Keyboard.print(")]("); delay(50); //Pastes entry Keyboard.press(KEY_LEFT_CTRL); Keyboard.press(KEY_V); Keyboard.releaseAll(); delay(50); //Types text Keyboard.println(") <-"); Keyboard.println("<div class="center-block text-center"> *Caption* </div>"); } else if (button == '0') { //Cuts selected entry Keyboard.press(KEY_LEFT_CTRL); Keyboard.press(KEY_X); Keyboard.releaseAll(); delay(50); //Types text Keyboard.print("<center><a href=\""); delay(50); //Pastes entry Keyboard.press(KEY_LEFT_CTRL); Keyboard.press(KEY_V); Keyboard.releaseAll(); delay(50); //Types text Keyboard.print("\"><img src=\""); delay(50); //Pastes entry Keyboard.press(KEY_LEFT_CTRL); Keyboard.press(KEY_V); Keyboard.releaseAll(); delay(50); //Types text Keyboard.println("\" alt=\"\"></a></center>"); Keyboard.println("<center><i>Caption</i></center>"); } else if (button == ' { //Cuts selected entry Keyboard.press(KEY_LEFT_CTRL); Keyboard.press(KEY_X); Keyboard.releaseAll(); delay(50); //Types text Keyboard.print("<kbd>"); delay(50); //Pastes entry Keyboard.press(KEY_LEFT_CTRL); Keyboard.press(KEY_V); Keyboard.releaseAll(); delay(50); //Types text Keyboard.print("</kbd>"); } else { Keyboard.println(button); } } //Do something else. Don't call your Keypad a ton otherwise you'll tie up the I2C bus delay(25); //25 is good, more is better }