Detect key press events in TextEditor



I’m looking for a way to detect key-down and key-up events in the TextEditor component.

Is this possible ?

Thank you!


If editor is a reference to the TextEditor instance:

editorView = atom.views.getView editor
editorView.addEventListener 'keydown', (event) ->
  # handle keydown event
editorView.addEventListener 'keyup', (event) ->
  # handle keyup event

You’ll want to keep a reference to the event handlers to be able to remove them when your package deactivates. Or maybe create a disposable:

{Disposable, CompositeDisposable} = require 'atom'

disposables = new CompositeDisposable()

addEventListener = (editor, eventName, handler) ->
  editorView = atom.views.getView editor
  editorView.addEventListener eventName, handler
  new Disposable ->
    editor.removeEventListener eventName, handler

# usage:
disposables.add addEventListener editor, 'keydown', (event) ->
  # handle keydown event
disposables.add addEventListener editor, 'keyup', (event) ->
  # handle keyup event


Thank you very much! It works! :smiley: