Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Esc and Fn Key Events

Final provides EventEscFn so you can receive an event when the user presses Esc or a Fn key. Handy for development, demos, and a clean shutdown path. Event model: Events.

Several of the Pi examples use the Esc key to exit the program. Here is the typical handler, taken from examples/pi/led-btn-ctrl.fnl The actual key pressed is in the Event.params[0] parameter. Esc is 0, the Fn keys the Fn key number 1-12.

A typical EventEscFn handler code:

/*
Handle an Esc or Fn key press.
We look specifically for Esc.
( ev:Event -- )  o( inst:LedBtnCtrl )
*/
: LedBtnCtrl#handleEscFn
  // get key: 0=Esc, else Fn#
  >ts 0 t:.params@() tdrop
  ==0 if
    LedBtnCtrl_stop
  then
;

Here is the initialization code. Note: you have to call the .start method after creating it.

    o:.handleEscFn~ 0 EventEscFn.new^ o:.escFn!
    o:.escFn.start^

When your program stops, you should close the EventEscFn object, so it doesn’t continue to send events to zombie code.

: LedBtnCtrl_stop
  // stop receiving EscFn events
  o:.escFn.close^

  // ... more code