Search code examples
javascriptkey-bindingsdraftjs

How can I get draft.js to recognize the escape key?


I would like to cancel input and clear the field in my app when the user types the escape key. We tried testing for e.which === 27 in the keyBindingFn, but that function is never even invoked when the escape key is pressed (it is invoked just fine for normal keys, modifier keys, and arrow keys). How can I detect an Escape keypress in draft.js?


Solution

  • Editor component has onEscape property

    <Editor 
        editorState={this.state.editorState} 
        onChange={this.onChange.bind(this)} 
        onEscape={keyEvent=>console.log('Escape just  pressed')}
        ref="editor"
    />