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.