Skip to content

Use Key Press

Shortcuts of your own: an element round the canvas hears every key pressed on the flow, so the page binds keys with ordinary @onkeydown and @onkeyup and acts through the canvas's API - AddNodes, DeleteElementsAsync and MoveSelectionAsync. The keys light up while they are held.

ahjklDeleteShift Last key: -
Press 'a' to add a node
Select me, then h j k l
bit Blow

Try it

  • Click the canvas (or a node) so the keys reach it: the key chips above light up while you hold a key, and Last key names the latest one with its modifiers.
  • Press a to add a node at a random spot; the view eases out to fit it in. Holding the key adds only one.
  • Select a node and press h, j, k or l to move it left, down, up or right by 20; hold Shift for 100, or hold the key to keep it going. Several selected nodes move together.
  • Select nodes or edges and press Delete or Backspace: the page removes them, along with the edges of a removed node.
  • Press Ctrl+A (Cmd+A on a Mac): that one is the canvas's own select-all, and the page leaves combinations with Ctrl, Cmd or Alt alone. The arrow keys still nudge the selection too, as they always do.

How it works

  • @onkeydown and @onkeyup on the element round the canvas receive a KeyboardEventArgs for every key pressed while focus is in the flow; Key, ShiftKey, CtrlKey, AltKey, MetaKey and Repeat say which.
  • DeleteKeyEnabled="false" switches off the canvas's binding of DeleteKeys (Delete and Backspace) so the page's own handler is the only one acting on them.
  • AddNodes(node) appends and measures the new node, then FitViewAsync(300) eases the view to fit everything over 300 ms.
  • DeleteElementsAsync(nodes, edges) deletes the selected nodes and edges with the same rules as the Delete key: edges of a removed node go too, Deletable = false is respected, and OnBeforeDelete, OnDelete and NodesChanged run as usual.
  • MoveSelectionAsync(dx, dy) moves the selected nodes by a distance in flow units, as the arrow keys do: draggable nodes only, snapped to the grid when SnapToGrid is on, kept inside their extents, announced to a screen reader and recorded as one undo step.
  • The canvas claims only its own keys - the arrows, Escape, Enter and Space on a focused node, PanActivationKey (Space) held, Ctrl/Cmd+A while SelectAllKeyEnabled, DeleteKeys, and ConnectionKey (c) on a focused node - so other letters are free for shortcuts like these. See Key Bindings for changing them.
An unhandled error has occurred. Reload 🗙