I have an Arduino Uno and a usb 2.0 shield in which I connected a keyboard to. I have all the necessary libraries installed but what I am trying to figure out is how to light an led when I press a specific key.

I am new to codding Arduino boards and I have done a lot of reading but I cant figure out my issue I feel like if I get this figured out I will be a lot better off in my future projects.

#include <hidboot.h>
#include <usbhub.h>
#ifdef dobogusinclude
#include <spi4teensy3.h>
#include <SPI.h>

int SKEY = 8;

class KbdRptParser : public KeyboardReportParser
    void PrintKey(uint8_t mod, uint8_t key);


    void OnKeyDown  (uint8_t mod, uint8_t key);
    void OnKeyUp    (uint8_t mod, uint8_t key);
    void OnKeyPressed            (uint8_t key);

void KbdRptParser::PrintKey(uint8_t m, uint8_t key)
  *((uint8_t*)&mod) = m;
  Serial.print((mod.bmLeftCtrl   == 1) ? "C" : " ");
  Serial.print((mod.bmLeftShift  == 1) ? "S" : " ");
  Serial.print((mod.bmLeftAlt    == 1) ? "A" : " ");
  Serial.print((mod.bmLeftGUI    == 1) ? "G" : " ");

  Serial.print(" >");
  PrintHex<uint8_t>(key, 0x80);
  Serial.print("< ");

  Serial.print((mod.bmRightCtrl   == 1) ? "C" : " ");
  Serial.print((mod.bmRightShift  == 1) ? "S" : " ");
  Serial.print((mod.bmRightAlt    == 1) ? "A" : " ");
  Serial.println((mod.bmRightGUI  == 1) ? "G" : " ");

void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key)
  Serial.print("DN ");
  PrintKey(mod, key);
  uint8_t c = OemToAscii(mod, key);


void KbdRptParser::OnKeyUp(uint8_t mod, uint8_t key)
  Serial.print("UP ");
  PrintKey(mod, key);

void KbdRptParser::OnKeyPressed(uint8_t key)
  Serial.print("ASCII: ");

USB Usb;

KbdRptParser Prs;

void setup() { 
#if !defined(__MIPSEL__)
  while (!Serial); 

  if (Usb.Init() == -1)
    Serial.println("OSC did not start.");

  delay( 1 );

  HidKeyboard.SetReportParser(0, &Prs);


void loop() {

Anyways I know how to make if statements and how to activate pins on the Arduino but I cant figure it out in this keyboard library. For now I just made it print "TRUE" since there is not need to get complicated if I cant get the first step to work. Before I made that if statement the serial monitor would show the value assigned to the key and label it DN # and when I let up on the key it would say UP # but how can I translate that to sending a pulse to whatever pin when I push a key and stop sending a pulse when I let up on the key?

Also in my if statement that I made I know I need something before == sign but I tried multiple things and they did not work so I just left it blank and the reason I used the number 16 was because that was the value assigned to the S key in which I wanted to use to light an led. Anyways any help would be greatly appreciated!

  • what LED are you talking about? ... you have no LED control code in your sketch
    – jsotola
    Aug 8, 2020 at 23:46
  • you can print DN when a key is pressed ... why can't you turn on an LED when a key is pressed?
    – jsotola
    Aug 9, 2020 at 0:22
  • sending a pulse to whatever pin when I push a key and stop sending a pulse ... that only applies to a flashing LED ... if you turn on a LED and leave it on, then that is not a pulse
    – jsotola
    Aug 9, 2020 at 0:31
  • I dont want to leave an led on... I want to be able to push a key to turn on an led and when I let up on that key the led should turn off. Also I made it clear that I have not made the led code yet because if I cant figure out how to make the proper if-statement none of it will work. Every time I try I get an error which was the whole reason I asked this question I know the code to make an led light up but I dont know how to do that while using the keyboard library...
    – CyClone
    Aug 9, 2020 at 1:10
  • 1
    Did you post the wrong code? Where is this if statement you are talking about?
    – Delta_G
    Sep 10, 2020 at 1:23

1 Answer 1


Keyboards cannot be used quite like you wish - the keyboard will send a key (scan) code when a key is pressed and then repeat that code if the button continues to be pressed. There is a different code sent when the same key is released.

The scancode for key release is obtained from it by setting the high order bit (adding 0x80 = 128). Thus, Esc press produces scancode 01, Esc release scancode 81 (hex). For sequences things are similar: Keypad-/ gives e0 35 when pressed, e0 b5 when released. Most keyboards will repeat the make code (key down code) when the key repeats.

From: https://www.win.tue.nl/~aeb/linux/kbd/scancodes-1.html

  • Very strange because when I press a key there is a specific number or number letter combo attached to it. For example when I look at the serial monitor it knows what key I am pressing and for how long. I dont understand how the Arduino knows via the serial monitor what key is being pressed and is able to print it but wont cant send a pulse to a specific pin based on the duration the key is held down. If the Arduino is aware of the specific key being pressed and its duration there is no reason it cant light an led up.
    – CyClone
    Aug 11, 2020 at 4:03
  • @CyClone Well, this question and its answer were left alone, because you did not edit your question answering 1) how you tried to control the LED, and 2) which error you got trying. Jan 8, 2021 at 7:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.