Book Image

OpenFrameworks Essentials

Book Image

OpenFrameworks Essentials

Overview of this book

Table of Contents (19 chapters)
openFrameworks Essentials
About the Authors
About the Reviewers

Handling keyboard events

The keyboard events are handled by the keyPressed(int key) and keyReleased(int key) functions, which are called by openFrameworks when a key is pressed and released respectively. The key input argument holds the numerical code of the key. Let's implement handling key pressings in our project and use it to add some desirable features: hiding GUI, saving screenshots, and saving/loading presets.


We consider here only key pressing events. Handling key releasing is implemented in a similar way.

Hiding the GUI

In order to capture the image generated with our project, we need to be able to hide the GUI from the screen. To achieve this, let's define the Boolean variable showGui, implement its toggling by pressing Z, and use its value to decide whether we should draw gui on the screen:

  1. Add the variable definition to the ofApp class:

    bool showGui;
  2. Next, set up its starting value in setup():

    showGui = true;
  3. Now, implement its toggling by inserting the following command in the ofApp...