In order to capture the keys from an usb-kbd and send yo Pd, I tried this and I think it work:
render.cpp
#include <fcntl.h>
#include <linux/input.h>
// Task for read the qwerty kbd
AuxiliaryTask gInputTask;
void read_kbd();
// global variables to receive keys from the auxiliary thread
bool newKeyEvent = false;
int keyValue = 0;
int keyType = 0;
in setup()
// start qwerty kbd capture thread
if((gInputTask = Bela_createAuxiliaryTask(&read_kbd, 50, "bela-read-input")) == 0)
return false;
in render()
if (newKeyEvent)
{
if(keyType == 0)
libpd_float("keyup", (int)keyValue);
if(keyType == 1)
libpd_float("key", (int)keyValue);
newKeyEvent = false;
}
Bela_scheduleAuxiliaryTask(gInputTask);
and
void read_kbd(){
// Variables keyboard control
struct input_event ev;
ssize_t n;
const char *dev = "/dev/input/event0";
int fd;
// qwerty keyboard capture
fd = open(dev, O_RDONLY);
if (fd == -1) {
fprintf(stderr, "Cannot open %s:.\n", dev);
return;
}
bool shouldStop = false;
int n;
while (!shouldStop && !gShouldStop)
{
n = read(fd, &ev, sizeof(ev));
if (n == (ssize_t)-1) {
shouldStop = true;
} else if (n != sizeof(ev)) {
shouldStop = true;
} else
{
if (ev.type == EV_KEY && ev.value == 0 && ev.value <= 2)
{
keyType = 0; // key released
keyValue = (int)ev.code;
newKeyEvent = true;
}
if (ev.type == EV_KEY && ev.value == 1 && ev.value <= 2)
{
keyType = 1; // key pressed
keyValue = (int)ev.code;
newKeyEvent = true;
}
// I'm filtering key repetitions
}
}
}