2

I've been building a small macro keypad based on the ATmega32U4. So far, I've been able to send standard key presses to the host. But since I want a program on the host system to react to the key presses, I only send the keys to serial to avoid typing anything. For now, this does work.

I want to send custom HID values to the host which won't result in regular key presses. As I've been reading around different blog and forum posts, most of them are just about building a regular keyboard or gamepad but don't go into how to make use of anything else.

This is what I got so far:

#include <Keypad.h>

const byte rows = 3;
const byte cols = 5;

char keys[rows][cols] = {
  { '1', '2', '3', '4', '5' },
  { 'q', 'w', 'e', 'r', 't' },
  { 'a', 's', 'd', 'f', 'g' }
};

byte rowPins[rows] = { 10, 16, 14 };
byte colPins[cols] = { 9, 8, 7, 6, 5 };

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, rows, cols );

void setup(){
  Serial.begin(9600);
}

void loop(){
  char key = keypad.getKey();

  if (key != NO_KEY){
    Serial.println(key);
  }
}

The only obvious way I see to achieve this is to change the keys array but I don't know to which values.


UPDATE

I changed the code according to the answer by Majenko:

#include <HID-Project.h>
#include <HID-Settings.h>

uint8_t rawhidData[255];

byte rows[] = { 10, 16, 14 };
byte cols[] = { 9, 8, 7, 6, 5 };

const int rowCount = sizeof(rows)/sizeof(rows[0]);
const int colCount = sizeof(cols)/sizeof(cols[0]);

void setup() {
  for (int row = 0; row < rowCount; row++) {
    pinMode(rows[row], OUTPUT);
    digitalWrite(rows[row], HIGH);
  }

  for (int col = 0; col < colCount; col++) {
    pinMode(cols[col], INPUT_PULLUP);
  }

  RawHID.begin(rawhidData, sizeof(rawhidData));
}

void loop() {
  for (int row = 0; row < rowCount; row++) {
    digitalWrite(rows[row], LOW);

    for (int col = 0; col < colCount; col++) {
      if (digitalRead(cols[col]) == LOW) {
        String coordinatesString = String(row+1) + String(col+1);
        uint8_t coordinates = coordinatesString.toInt();

        uint8_t megabuff[sizeof(coordinates)];

        megabuff[0] = coordinates;
      
        RawHID.write(megabuff, sizeof(megabuff));
        
        while (digitalRead(cols[col]) == LOW) {
          delay(50);
        }
        break;
      }
    }

    digitalWrite(rows[row], HIGH);
  }
}

I removed the serial output since I don't need that anymore. The code looks crude and needs further refactoring but it does work.

1 Answer 1

3

HID is a strange beast to get your head around.

It operates by way of "Report Descriptors". These are "maps" that lay out what the values you send mean. It's these descriptors that make a keyboard a keyboard and a mouse a mouse.

When you use Keyboard.h you attach a Keyboard report descriptor to the USB HID endpoint, and the computer knows to interpret the data coming in as keypresses. When you use Mouse.h you attach a Mouse report descriptor and the computer knows to interpret the data coming in as mouse movements and button presses.

If you want your computer to ignore the data that you send and allow your own program to be able to receive the raw data instead then you need to use a descriptor that the computer doesn't recognise. In such a case it just ignores the data.

There's a "Raw HID" library for Arduino that does that - it uses a "blank" report descriptor that just gives you a plain 64 byte block of data to do what you want with.

There's a good HID library by Nico Hood that implements the raw mode (as well as others).

For receiving the data on your computer you will have to have your program directly interface with the USB HID system. There's plenty of libraries for most languages to do this for you (hidapi in C / C++, hid in Python, hid4java for Java, etc).

1
  • Great information. For anyone looking, there is a critical bug in referenced HID library that breaks RawHID.write(); . Be aware of this bug before proceeding down that path. github.com/NicoHood/HID/issues/133
    – spuder
    Commented Aug 7, 2022 at 3:49

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.