Using keyboard-layout package

I’m fairly new to Atom and I’m looking into ways to fix keyboard layout issues I’m having (with AltG) on macOS, using custom keyboard layout. Basically Alt + any letter resolves into 'alt-undefined'(I don’t have many key mappings there in my keyboard layout)

I’d like to (at least for undefined letters), be able to map to original character. So Alt-W would actually fire Alt-W keybinding (if there is any). Right now I can’t use almost any keybinding that uses alt key.

I had some success by adding code to init.js

atom.keymaps.addKeystrokeResolver(({event}) => {
  if(event.code === 'KeyW' && event.altKey) {
    // ...
  }
})

but I’d like to use the functionality provided by keyboard-layout package. I don’t know how to install and require it properly.

var KeyboardLayout = require('keyboard-layout') doesn’t seem to work. “Cannot find module ‘keyboard-layout’” Do I need to install this module somehow?

EDIT: var KeyboardLayout = require('keyboard-layout') doesn’t work in init.js, but works if I put debugger; and then evaluate that line in the Console. :man_shrugging:

Hi :wave:

I don’t think you can require in the way you describe in your init script. So to use keyboard-layout like you describe you would need to make a package.

You can check out my package to get an idea on how to add this functionality as a package.

What is your keyboard layout? It would be interesting to fix this in Atom core instead of requiring you to write a custom keystroke resolver.

You’re right, this fix would probably fit better in Atom core.

I’m using custom layout on macOS, that I built using Ukelele app.

So far I haven’t had any issues in other editors, nor in any other app. Atom is the only app in which I noticed ALT+key doesn’t behave as expected.

In the Ukelele app, when I hold ALT, I can see that the keys are affected by this. However, seems like other apps decided to accommodate for this if possible and they therefore “pretend” the combination clicked is ALT+ instead of ALT+ (what I have “mapped” for most of the keys when ALT is pressed)

So ALT+H for example maps to ALT + H, no matter the fact that actually pressing ALT + H in any app, it doesn’t actually output the letter H.

Thanks @vfonic :+1: Just so we’re on the same page, can you share steps to reproduce? I’ve never used Ukelele but I’m assuming the installation is straightforward. But then after installation, is your custom layout saved in a file that you can share which I would then need to open in Ukelele? Step-by-step instructions would be super helpful.

Hey @rsese! Thanks for looking into this.

Here’s my keyboard layout: https://www.dropbox.com/s/it63n01j979ido9/viktor.keylayout?dl=1

I honestly don’t remember how I created it in the beginning. I’m pretty sure I took some existing keyboard layout and modified it.

To “load” this in Ukelele, you can simply “File => Open” this file. To use this keyboard layout so that you can test it in Atom, you can add it to ~/Library/Keyboard Layouts directory. Then you’ll need to “Log out” or restart. And finally choose it from System Preferences (Keyboard => Input sources => +)

Any modifications you make to the keyboard layout, you’ll have to Log-out (or restart)…at least as far as I know.

Hey :wave:

This is caused by your layout being undefined for these keys. We have a PR open to fix this in atom-keymaphttps://github.com/atom/atom-keymap/pull/240

In the meantime, you could add the same code to a custom package you have made to make it resolve correctly.

1 Like