All versions of this documentation

Keyboard interactions

The keyboard module allows you to bind an action to a given combination of keys. In this example, we bind the addition of a node to a combination, and the removal of a node to another combination:

Open in a new window.
          <!DOCTYPE html>
  <meta charset="utf-8">
  <script src="../build/ogma.min.js"></script>
    #graph-container { top: 0; bottom: 0; left: 0; right: 0; position: absolute; margin: 0; overflow: hidden; }
<div id="graph-container"></div>

  'use strict';

  var ogma = new Ogma({
    container: 'graph-container'

  var addCounter = 0,
      removeCounter = 0;

  function addNode() {
    ogma.addNode({id: addCounter++, attributes: {x: Math.random() * 100, y: Math.random() * 100}});
    console.log('Node added');

  function removeNode() {
    if (removeCounter < addCounter) {
      console.log('Node removed');
  }'alt a', addNode);'alt r', removeNode);

  for (let i = 0; i < 5; ++i) {