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.

Keyboard command bindings

The CommandHandler implements the following command bindings for keyboard input:

On a Macintosh the Command key is used as the modifier instead of the Control key.

At the current time there are no keyboard bindings for commands such as CommandHandler.collapseSubGraph, CommandHandler.collapseTree, CommandHandler.expandSubGraph, or CommandHandler.expandTree.

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.


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.

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 group selected parts together with the CommandHandler.groupSelection, you will need to set CommandHandler.archetypeGroupData to a group node data object:

  diagram.commandHandler.archetypeGroupData =
    { key: "Group", isGroup: true, color: "blue" };

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:

    myDiagram.commandHandler.doKeyDown = function() {
      var e = myDiagram.lastInput;
      var cmd = myDiagram.commandHandler;
      if (e.key === "T") {  // could also check for e.control or e.shift
        if (cmd.canCollapseSubGraph()) {
        } else if (cmd.canExpandSubGraph()) {
      } else {
        // call base method with no arguments;

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.

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.

  // allow the group command to execute
  diagram.commandHandler.archetypeGroupData =
    { key: "Group", isGroup: true, color: "blue" };
  // modify the default group template to allow ungrouping
  diagram.groupTemplate.ungroupable = true;

  var nodeDataArray = [
    { key: "Alpha" },
    { key: "Beta" },
    { key: "Delta", group: "Epsilon" },
    { key: "Gamma", group: "Epsilon" },
    { key: "Epsilon", isGroup: true }
  var linkDataArray = [
    { from: "Alpha", to: "Beta" },
    { from: "Beta", to: "Beta" },
    { from: "Gamma", to: "Delta" },
    { from: "Delta", to: "Alpha" }
  diagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);

  // enable or disable a particular button
  function enable(name, ok) {
    var button = document.getElementById(name);
    if (button) button.disabled = !ok;
  // enable or disable all command buttons
  function enableAll() {
    var cmdhnd = diagram.commandHandler;
    enable("SelectAll", cmdhnd.canSelectAll());
    enable("Cut", cmdhnd.canCutSelection());
    enable("Copy", cmdhnd.canCopySelection());
    enable("Paste", cmdhnd.canPasteSelection());
    enable("Delete", cmdhnd.canDeleteSelection());
    enable("Group", cmdhnd.canGroupSelection());
    enable("Ungroup", cmdhnd.canUngroupSelection());
    enable("Undo", cmdhnd.canUndo());
    enable("Redo", cmdhnd.canRedo());
  // notice whenever the selection may have changed
  diagram.addDiagramListener("ChangedSelection", function(e) {
  // notice when the Paste command may need to be reenabled
  diagram.addDiagramListener("ClipboardChanged", function(e) {
  // notice whenever a transaction or undo/redo has occurred
  diagram.addModelChangedListener(function(e) {
    if (e.isTransactionFinished) enableAll();
  // perform initial enablements after everything has settled down
  setTimeout(enableAll, 1);

  myDiagram = diagram;  // make the diagram accessible to button onclick handlers

Each button is implemented in the following fashion:

<input id="SelectAll" type="button"
       onclick="myDiagram.commandHandler.selectAll()" value="Select All" />

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


Since GoJS is based on the HTML Canvas element, making an app that is accessible to screen-readers or other accessibility devices is a matter of generating fallback content outside of GoJS, just as you would generate fallback content separate from any HTML Canvas application.

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. We recommend that you implement alternative mechanisms specific to your application for those tools that you want your users to access without a pointing device.

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 programatically:

  myDiagram.commandHandler.doKeyDown = function() {
    var e = myDiagram.lastInput;
    // The meta (Command) key substitutes for "control" for Mac commands
    var control = e.control || e.meta;
    var key = e.key;
    // Quit on any undo/redo key combination:
    if (control && (key === 'Z' || key === 'Y')) return;

    // call base method with no arguments (default functionality);