Init Script that intercepts key bindings?


#1

Hey, I’d like to know how I can intercept a keybinding in an init script.

There’s a plugin which prevents typing a closing parenthesis in certain circumstances, and most of the time this is what I want. But sometimes, I need to type it in. I would like to make it so that if I press “Esc” it toggle off the plugin, and the next key I type is inserted into the editor, and then the plugin is activated again. So I need to know how I can from an init script listen for a key.

Thanks.


#2

I was able to insert a closing parenthesis with this code:

atom.commands.add 'atom-text-editor', 'user:insert-close-paren', ->
  return unless editor = atom.workspace.getActiveTextEditor()

  cursor = editor.getCursorBufferPosition()
  editor.buffer.insert(cursor, ")")

My keymap.cson:

'atom-text-editor':
  'escape )': 'user:insert-close-paren'

#3

That’s what I have now already, but I was hoping to find a more generic way to just have Escape be my escape of any following key I type. For now, I’ll just do that for every keys that I do need to escape.


#4

You could have escape pop up a modal dialog with a text box that takes text and inserts it exactly as typed. It could automatically enter after one character, or could take a whole string.