gdsports / USB_Host_Library_SAMD

USB host library 2.0 for Zero/M0/SAMD
126 stars 39 forks source link

Capturing and transmitting arrow key presses (directional keys: up down left right) for Arduino USB Host #10

Closed kmatch98 closed 4 years ago

kmatch98 commented 4 years ago

The directional keys are not captured in the ASCII codes and are not returned by the Arduino KeyboardController getKey() function. However, for the KeyboardController library there is a function getOemKey() that can be used to capture presses of the directional arrow keys (up down left right). This function returns 79-81 for the arrow keys and then can be translated into the appropriate escape codes in a printKey() function as below.

I simplified the KeyboardController.ino sample file by deleting all the extra serial write outputs and left only the output for the printKey() statement as below.

Hardware setup: I ran this on an Adafruit ItsyBitsy M4 with serial output via UART. The UART was setup using #define SerialDebug Serial1

Below is the printkey() function that translates the getOemKey() codes into arrow key escape codes for my application.

void printKey() {
  switch (keyboard.getOemKey()) { // Trial on 5/4/2020 to see if it will fix arrow keys

    case 80:
      SerialDebug.write(27); // left
      SerialDebug.write(91);
      SerialDebug.write(68);
      break;

    case 79:
      SerialDebug.write(27); // Right
      SerialDebug.write(91);
      SerialDebug.write(67);
      break;

    case 81:
      SerialDebug.write(27); // down
      SerialDebug.write(91);
      SerialDebug.write(66);
      break;

    case 82:
      SerialDebug.write(27); // up
      SerialDebug.write(91);
      SerialDebug.write(65);
      break;

    default:
      SerialDebug.write(keyboard.getKey());
      break;
  }
}