Skip to main content
  1. Index

Commands

Commands such as Delete or Paste or Undo are implemented by the CommandHandler class.

Keyboard events, like mouse and touch events, always go to the Diagram.currentTool. The current tool, when the user is not performing some gesture, is the same as the Diagram.defaultTool, which normally is the Diagram.toolManager. The ToolManager handles keyboard events by delegating them to the Diagram.commandHandler.

Basically, the diagram handles a keyboard event, creates an InputEvent describing it, and then calls ToolManager.doKeyDown. That in turn just calls CommandHandler.doKeyDown. The same sequence happens for key-up events.

Please note that the handling of keyboard commands depends on the diagram getting focus and then getting keyboard events. Do not apply any styling such as

css

Keyboard command bindings

The CommandHandler implements the following commands for keyboard input.

For all of these commands, you can freely interchange Meta ( on macOS) with Ctrl.

Keys Effect
Del Backspace invoke CommandHandler.deleteSelection
Ctrl + X Shift + Del invoke CommandHandler.cutSelection
Ctrl + C Ctrl + Insert invoke CommandHandler.copySelection
Ctrl + V Shift + Insert invoke CommandHandler.pasteSelection
Ctrl + A invoke CommandHandler.selectAll
Ctrl + Z Alt + Backspace invoke CommandHandler.undo
Ctrl + Y Ctrl + Shift + Z Alt + Shift + Backspace invoke CommandHandler.redo
call Diagram.scroll
PgUp PgDn call Diagram.scroll
Home End call Diagram.scroll
Space invoke CommandHandler.scrollToPart
Ctrl + - Numpad- (minus) invoke CommandHandler.decreaseZoom
Ctrl + + Numpad+ (plus) invoke CommandHandler.increaseZoom
Ctrl + 0 invoke CommandHandler.resetZoom
Shift + Z invoke CommandHandler.zoomToFit; repeat to return to the original scale and position if CommandHandler.isZoomToFitRestoreEnabled is true
Ctrl + G invoke CommandHandler.groupSelection
Ctrl + Shift + G invoke CommandHandler.ungroupSelection
F2 invoke CommandHandler.editTextBlock
Menu Shift + F10 Ctrl + Shift + \ invoke CommandHandler.showContextMenu
Escape invoke CommandHandler.stopCommand
Ctrl + Alt + Enter switch to keyboard control mode

For the keyboard command bindings when in keyboard control mode, see Keyboard commands.

At the current time there are no keyboard bindings for commands such as CommandHandler.collapseSubGraph, CommandHandler.collapseTree, CommandHandler.expandSubGraph, or CommandHandler.expandTree. Such Nodes or Groups usually have buttons such as "TreeExpanderButton" or "SubGraphExpanderButton" in them or provide expand/collapse ability via other means.

If you want to have a different behavior for the arrow keys, consider using the sample class extended from CommandHandler: DrawCommandHandler, which implements options for having the arrow keys move the selection or change the selection.

That DrawCommandHandler extension also demonstrates a customization of the Copy and Paste commands to automatically shift the location of pasted copies.

CommandHandler

The CommandHandler class implements pairs of methods: a method to execute a command and a predicate that is true when the command may be executed. For example, for the Copy command, there is a CommandHandler.copySelection method and a CommandHandler.canCopySelection method.

Keyboard event handling always calls the "can..." predicate first. Only if that returns true does it actually call the method to execute the command.

Updating command UI

It is common to have HTML elements outside of the diagram that invoke commands. You can use the CommandHandler's "can..." predicates to enable or disable UI that would invoke the command.

The following demo depicts a file system working on a C program. Add files with the input at the top and use the buttons to edit the diagram. Files can be grouped into directories by multiple-selecting them and pressing "Create Directory". Press T to collapse directories and drag from file border to border to draw links showing if a file #includes another.

Each button is implemented in the following fashion:

html

Whenever the selection changes or whenever a transaction or undo or redo occurs, the updateButtons function is called to update the disabled property of each of the buttons.

Custom behavior

There are a number of properties that you can set to affect the CommandHandler's standard behavior. For example, if you want to allow the user to create a Group of selected parts with the CommandHandler.groupSelection, you will need to set CommandHandler.archetypeGroupData to the desired group node data object. Use Ctrl + G to create groups and Ctrl + Shift + G to ungroup:

That data object is copied and added to the model as the new group data object by CommandHandler.groupSelection.

If you want to add your own keyboard bindings, you can override the CommandHandler.doKeyDown method. For example, to support using the T key to collapse or expand the currently selected Group:

Do not forget to call the base method in order to handle all of the keys that your method does not handle.

Note that calling the base method involves getting the base class's prototype's method. If the base method takes arguments, be sure to pass arguments to the call to the base method.

Accessibility

Although much of the predefined functionality of the CommandHandler is accessible with keyboard commands or the default context menu, not all of it is, and the functionality of the Tools mostly depends on mouse or touch events. Your users can use keyboard control mode if they cannot use a mouse or finger or if they prefer to use a keyboard.

That mode also supports updating through screen readers. Since for efficiency and flexibility reasons, GoJS is designed not to use regular HTML DOM elements, making an app that is accessible to screen readers or other accessibility devices is a matter of producing the appropriate application-specific information in an HTML element and implementing a CommandHandler.focusChanged event handler that updates that element. Read more at Customizing for screen readers.

More CommandHandler override examples

Stop Ctrl + Z/Ctrl + Y from doing an undo/redo, but still allow CommandHandler.undo and CommandHandler.redo to be called programmatically:

js

Override CommandHandler.copySelection to prevent copying any selected Groups so users cannot duplicate whole groups but can still copy individual nodes:

js