0

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 / Do

  • W: C# / Do#

  • S: D / Re

  • E: D# / Re#

  • D: E / Mi

  • F: 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

Daniel Reina
  • 5,764
  • 1
  • 37
  • 50
  • You could let the user make inputs for every row, but this would be probably not the best user-experience. By the way think of mobile devices or parted keyboards. – Sascha Aug 24 '20 at 15:58
  • Good points. For touch devices we though of just using touch inputs, so keyboards should not be needed. For parted keyboards I guess the user would have to apply extra imagination to think they're playing an instrument with continuous keys. I thought about the user having to press all the keys, but it doesn't feel right from the UX perspective, as you mention. I was hoping there was a way to do something like that with JS instead of requesting user's actions – Daniel Reina Aug 24 '20 at 16:05
  • If it helps, here are standard windows keyboard layouts http://kbdlayout.info/ you can download each layout as xml. – Servus Aug 24 '20 at 19:45

0 Answers0