Problem
I want to know what letters correspond to the different row of keys in the user's keyboard, using JS. An array representation of what I want would be something like this:
// ANSI QWERTY layout for English - USA
const usaKeyboardLayout = [
['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\\'], // literal '\' escaped
['a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', "'"],
['z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/']
]
// ISO QWERTY layout for Spanish - Spain
const spanishKeyboardLayout = [
['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '`', '+'],
['a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'ñ', '´', 'ç'],
['<', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '-']
]
From this other SO question and the keyboard layout wiki it doesn't look like there's a standard that I could map to the locale or anything like that. I mean, even my keyboard configuration has around a hundred groups, each of them with 1 or more layout, with one of the groups called "others". Let alone keyboards for non-latin alphabets.
I don't think there's a standard way to find out this information, but I was wondering if there's something clever I could do to solve this problem.
I've already tried triggering synthetic events with a keyCode
to try and get the key
back, but it came empty.
Context
My end goal is to implement some kind of instrument controller with the keyboard. Programs that have MIDI input capabilities (e.g. ableton) allow the user to play a piano-like layout with the keyboard. With a QWERTY layout, for example, this is the equivalence between keyboard keys and piano keys:
A
: C / DoW
: C# / Do#S
: D / ReE
: D# / Re#D
: E / MiF
: F / Fa...And so on
I'd like to show the keyboard keys that the user has to press to play different instrument keys, but so far I can only hardcode them