Handle Keyboard Events

How do i handle keyboard events in GoJs?

I wish to intercept the key or other keys and call and event handler.

thank you

Override Diagram.commandHandler.doKeyDown, check this.diagram.lastInput.key, and otherwise call CommandHandler.doKeyDown for the regular behaviors. Search the samples for examples of how to override methods.

Thanks can you point me t o a demo which deals with key events?

I found this in the Text Editing demo

<span =“comment”>// Do a few different things when a user presses a key
customEditor.addEventListener(<span ="">“keydown”, <span =“keyword”>function(e) {
<span =“keyword”>var keynum = e.which;
<span =“keyword”>var tool = customEditor.textEditingTool;
<span =“keyword”>if (tool === <span =“literal”>null) <span =“keyword”>return;

Does the “keydown” event also work on diagram? If so, i think this is what i need. Can’t try it until later tonight.


has some example code for you.

thanks worked great!

Hi Walter,

Can you please elaborate how to catch ‘enter’ and ‘tab’ key pressed in the dokeydown event?

I gave you a “keydown” listener that does exactly that, in How to expand node's size dynamically when the text in its textBlock is being entered? - #3 by walter.

Or maybe you need to know that you can get the KeyboardEvent as the e.diagram.lastInput.event, if it’s a keyboard event.

Hi Walter,

Yes Walter, those events can be caught there but I want to catch those keyevents in the dokeydown listener.