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

2

5

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?

Citrate answered 15/7, 2016 at 17:29 Comment(0)
P
8

Editor component has onEscape property

<Editor 
    editorState={this.state.editorState} 
    onChange={this.onChange.bind(this)} 
    onEscape={keyEvent=>console.log('Escape just  pressed')}
    ref="editor"
/>
Pome answered 24/8, 2016 at 10:32 Comment(0)
C
0

We solved this with an onKeyDown handler in the editor's enclosing div. I'm not thrilled with the solution and hope someone has a better one, but this at least works.

Citrate answered 15/7, 2016 at 19:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.