Navigation
Synopsis Define interactive behaviour when a key is pressed.
Function FProperty onKeyDown (bool (KeySym key, map[KeyModifier,bool] modifiers) cb)
Description This property allows you to install a handler for key presses. When the mouse is over the figure (i.e. the figure has focus) and a key is pressed then the callback cb will be called. This callback takes two arguments
  • KeySym key the key that is pressed, described by a KeySym.
  • map[KeyModifier,bool] a map that indicates which modifier keys (Control, Alt, Super) are currently held down. Modifier keys are described by KeyModifier.

    The callback returns a bool which indicates if the event is captured. Keyhandlers are executed deepest-figure first, if the key press is captured then the key press is not propagated further so figures below (less deep) than this figure will not receive a key press.
Examples
import vis::KeySym;
s = "";
s2 = "";
b = box(text(str () { return s; }),
	fillColor("red"),
	onKeyDown(bool (KeySym key, map[KeyModifier,bool] modifiers) {
		s = "<key>";
		return true;
	}));
b2 = box(vcat([
	text(str () { return s2; }),
	b],shrink(0.7)),
	fillColor("green"),
	onKeyDown(bool (KeySym key, map[KeyModifier,bool] modifiers) {
		s2 = "<key>";
		return true;
	}));
render(b2);
onkeydown
Pitfalls Unfortunately we cannot show an interactive version here, try it out in Rascal yourself.
Is this page unclear, or have you spotted an error? Please add a comment below and help us to improve it. For all other questions and remarks, visit ask.rascal-mpl.org.