Components Interactions Examples Tutorials API FAQ

Key

A Key interaction allows the user to trigger an event when their mouse cursor is inside of a Component and a specified key is pressed.

var data = [];
for (var i = 0; i < 250; i++) { data.push({"x": i, "y": Math.random()}); }
var plot = new Plottable.Plots.Scatter()
  .addDataset(new Plottable.Dataset(data))
  .x(function(d) { return d.x; }, new Plottable.Scales.Linear())
  .y(function(d) { return d.y; }, new Plottable.Scales.Linear())
  .renderTo("svg#example");
var interaction = new Plottable.Interactions.Key();
var timeout;
for (var i = 48; i < 90; i++) {
  interaction.onKeyPress(i, function(keyCode) {
    if (timeout != null) {
      clearTimeout(timeout);
    }
    $("#key-feedback").html(String.fromCharCode(keyCode)).fadeIn(100);
    timeout = setTimeout(function() {
      $("#key-feedback").fadeOut(800);
    }, 1400);
  });
}
interaction.attachTo(plot);

Public Methods