Hello everyone,
I am working on a project that uses a normal USB keyboard as an input. I have Arduino due, and I installed the latest Arduino IDE. I downloaded the needed settings, and libraries. my plan initially is to write a simple code that will:
- take an input from the keyboard, and show the pressed button in the serial monitor:
example: if I press an " K" in the keyboard the serial monitor shows "K".
My hardware setup:
- keyboard connected to the native USB port of Arduino due
- Arduino due is connected to the PC using the programming port
This is the code that I am trying, it compile error free, and it upload in the Due error free. however the code does not show anything in the serial monitor except for "Program started".
---------------CODE-----------------------
// Require keyboard control library
#include <KeyboardController.h>
// Initialize USB Controller
USBHost usb;
// Attach keyboard controller to USB
KeyboardController keyboard(usb);
// This function intercepts key press
void keyPressed() {
Serial.print("Pressed: ");
printKey();
}
// This function intercepts key release
void keyReleased() {
Serial.print("Released: ");
printKey();
}
void printKey() {
// getOemKey() returns the OEM-code associated with the key
Serial.print(" key:");
Serial.print(keyboard.getOemKey());
// getModifiers() returns a bits field with the modifiers-keys
int mod = keyboard.getModifiers();
Serial.print(" mod:");
Serial.print(mod);
Serial.print(" => ");
if (mod & LeftCtrl)
Serial.print("L-Ctrl ");
if (mod & LeftShift)
Serial.print("L-Shift ");
if (mod & Alt)
Serial.print("Alt ");
if (mod & LeftCmd)
Serial.print("L-Cmd ");
if (mod & RightCtrl)
Serial.print("R-Ctrl ");
if (mod & RightShift)
Serial.print("R-Shift ");
if (mod & AltGr)
Serial.print("AltGr ");
if (mod & RightCmd)
Serial.print("R-Cmd ");
// getKey() returns the ASCII translation of OEM key
// combined with modifiers.
Serial.write(keyboard.getKey());
Serial.println();
}
void setup()
{
Serial.begin(115200);
Serial.println("Program started");
delay(200);
}
void loop()
{
// Process USB tasks
usb.Task();
}