Inserting special characters (umlauts, etc)

I’m new to Atom, having used Sublime, and I’m trying to figure out if there’s a reasonable way to define keystrokes to enter non-ascii characters, such as all the letters with diacritics in ISO-8859-1. In Sublime, I could do it directly in the keymap:

[
{ “keys”: [“f1”, “`”, “a”], “command”: “insert”, “args”: {“characters”: “à”} },
{ “keys”: [“f1”, “’”, “a”], “command”: “insert”, “args”: {“characters”: “á”} },
{ “keys”: [“f1”, “^”, “a”], “command”: “insert”, “args”: {“characters”: “â”} },
{ “keys”: [“f1”, “~”, “a”], “command”: “insert”, “args”: {“characters”: “ã”} },
{ “keys”: [“f1”, “”", “a”], “command”: “insert”, “args”: {“characters”: “ä”} },
. . .
]

That works in Sublime because commands are Python functions that take arguments. In Atom, commands are events, and don’t appear to take arguments. I could define a command per character, but that seems inelegant, not to mention it would horrendously pollute the command palette. Am I missing something?

I’m not sure if this is right; Atom (and presumably Sublime) will react to keyboard events, and run a corresponding command if applicable (JS function in Atom’s case, Python in Sublime’s).

So if you set a keybinding, it will trigger a JS function. I believe you will be able to read what keys were pressed from the event that is passed as an argument.

You can use event.keyCode (or event.which) to detect which key has been pressed. To get the actual character from the returned keycode, you can use String.fromCharCode(event.keyCode).

But is there a way to get all the key presses that fired my command? Note that in the keymap above I’m defining 3-key sequences.

FYI, by setting a breakpoint in my command handler, I could discover that it’s actually event.originalEvent.keyCode that reveals the last key press. Unfortunately, I couldn’t find anything obvious in the environment to tell me the complete key sequence that got me there.

Why don’t you simply push the pressed key to an array and check the last x keys pressed?

Perhaps because I think I’m using an editor API, not programming a web page. Why should I listen to every key press when I’m rarely interested? That’s what a keymap is for. Atom is already listening to them, and has to keep track of the 3 keys pressed to invoke my command, so why can’t it just tell me what they are?

That said, thanks for the tip! I may end up having to do that.

I think there’s a more creative way to do it. You could create a command that marks the cursor’s spot in the editor and creates a decoration with type: overlay as a little bubble that just pops up over the spot in the text you’re typing in. You can then tell Atom to focus the overlay element and it should intercept all keypresses that would normally go to the TextEditor.

Wow, that does indeed seem creative. And more ambitious than I was looking for.

I did find one commenter suggest adding the feature I was kind of asking for up there, viz. to have some attribute on the command event that would give the keys that invoked it. But I don’t see that anything came of it.

But I also discovered the hiddenInCommandPalette attribute, and decided to use it to define a command per key sequence and hide them all from the palette. Seems to work okay.