Esta función pasa por todas las matrices de un teclado, le pide al teclado el código clave en cada lugar y lo empuja a la matriz de códigos de teclas de capa. El problema es que device.read() es asincrónico y no pude encontrar una manera de hacerlo sincrónico sin tener un código súper desordenado. El problema es que el archivo console.log() al final (que se devolverá en producción) se activa antes de que el teclado responda y envíe los códigos clave. Sé que existe device.readSync() pero la documentación de nodehid es mala, no pude encontrar cómo usarlo.
const getLayerKeycodes = (keyboard, layer, rows, columns) => { //array that stores all the keycodes according to their order let layerKeycodes = []; //rows and columns start the count at 0 in low level, so we need to decrease one from the actual number. columns --; rows --; //loop that asks about all the keycodes in a layer const dataReceived = (err, data) => { if(err) { return err; } // push the current keycode to the array // The keycode is always returned as the fifth object. layerKeycodes.push(data[5]); console.log(layerKeycodes); }; for (let r = 0 , c = 0;c <= columns; r ++){ //callback to fire once data is receieved back from the keyboard. if(r > rows){ c++; //r will turn to 0 once the continue fires and the loop executes again r = -1; continue; } //Start listening and call dataReceived when data is received keyboard[0].read(dataReceived); //Ask keyboard for information about keycode // [always 0 (first byte is ignored),always 0x04 (get_keycode),layer requested,row being checked,column being checked] keyboard[0].write([0x01,0x04,layer,r,c]); } console.log(layerKeycodes); }