Skip to content

Shortcuts fails for non-standard layouts #22

Description

@qurle

Shortcut reported to fail on Dvvorak keyboard layout.

Reason

Type uses event.code to resolve physical key pressed, ignoring language and case. E.g. KeyS used for downloading note is actually o charecter on Dvorak.

Possible solutions

  1. Use event.key to get resolved key. Need handling of languages and case ☹️
  2. Use keyboard layouts mapping and resolving.

Example

const LATIN_LAYOUTS = {
  qwerty: { 'KeyQ':'q', 'KeyW':'w', 'KeyE':'e', 'KeyR':'r', 'KeyT':'t', 'KeyY':'y' /* ... */ },
  dvorak: { 'KeyQ':'\'', 'KeyW':',', 'KeyE':'o', 'KeyR':'e', 'KeyT':'u', 'KeyY':'i' /* ... */ },
  colemak: { 'KeyQ':'q', 'KeyW':'w', 'KeyE':'f', 'KeyR':'p', 'KeyT':'g', 'KeyY':'j' /* ... */ }
};
function getLatinCharacter(event, userLayout = 'qwerty') {
  const currentKey = event.key;
  
  // If it's already a standard Latin letter/number, use it directly
  if (/^[a-z0-9]$/i.test(currentKey)) {
    return currentKey.toLowerCase();
  }

  // Fallback: The user is using a non-Latin language (e.g., Cyrillic, Hebrew)
  // Look up what the physical hardware key translates to in the selected Latin layout
  const layoutMap = LATIN_LAYOUTS[userLayout];
  if (layoutMap && layoutMap[event.code]) {
    return layoutMap[event.code];
  }

  // Return fallback if key is a modifier or special character
  return currentKey.toLowerCase();
}
window.addEventListener('keydown', (e) => {
  const resolvedChar = getLatinCharacter(e, 'dvorak'); 
  
  if ((e.ctrlKey || e.metaKey) && resolvedChar === 'o') {
    // This executes accurately across languages for a Dvorak user
    console.log("Dvorak hotkey triggered!"); 
  }
});

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions