F1 key event checks

I recently wanted to add a check for pressing the F1 key to a Processing sketch, and the process left me with questions (below). A web search led me to a number of forum posts over the years; all these posts say that the correct form is keyCode == KeyEvent.VK_F1 -- as documented in Java 7 KeyEvent. The most recent Processing F1 example that I found was posted May 26 2016, ~~and worked on Processing 3.11. However I'm on Processing 3.2.1, and keyCode == KeyEvent.VK_F1 does not work for me.~~ Update: That example does work, just be sure to include the import statement, or it will raise the following error. In the Processing IDE it raises the error:

The global variable "VK_F1" does not exist.

Variations such as KeyEvent.F1 and VK_F1 are also not found.

What does work on Processing 3.2.1 is:

keyCode == java.awt.event.KeyEvent.VK_F1

This works even WITHOUT adding a separate import statement. For example, here it is in a small sketch:

/**
 * F1 Key: catch F1 in Processing 3.2.1
 */
color bgColor = color(255,0,0);
void setup(){ size(400, 400); }
void draw() { background( bgColor ); }
void keyPressed(){
  if (key == CODED) {
    if (keyCode == java.awt.event.KeyEvent.VK_F1){
      bgColor = color(0,255,0);
      println("f1");
    }
  }
}  

My questions:

Am I missing something about how to access the simple KeyEvent.VK_F1 form? Given that the function keys are undocumented on the keyCode() page, is this a bug, and should I submit a ticket? Should this be documented somewhere (e.g. other than here)?

Answers

  • edited September 2016 Answer ✓
    • Processing automatically imports some Java libraries.
    • Seems like they don't do that anymore for java.awt.event.KeyEvent.
    • But fret not, there's an even smarter kinda import: import static java.awt.event.KeyEvent.*;
    • Using that statement, you'll have direct access to all static members from class KeyEvent.
  • The KeyEvent class is from the java.awt.event package. You either need to import that class, or use the fully qualified name like your example code.

    If you found code online that just used KeyEvent without an import statement, that code is not complete. Many people will just leave out import statements in example code, and it's assumed that you'll add them yourself.

    Using KeyEvent.VK_F1 without an import statement will not work in any version of Processing.

    Notice that the link you posted does contain the import statement. You need that, otherwise Processing doesn't know where to find the KeyEvent class.

  • GoToLoop, KevinWorkman, thank you for these replies. Indeed, I missed that previous example I cited included the import statement I needed.

    To synthesize the three working approaches as I tested them:

    1. direct

       if (keyCode == java.awt.event.KeyEvent.VK_F1){ println("f1");}
      
    2. import KeyEvent

       import java.awt.event.KeyEvent;
       if (keyCode == KeyEvent.VK_F1){ println("f1");}
      
    3. import static KeyEvent members:

       import static java.awt.event.KeyEvent.*;
       if (keyCode == VK_F1){ println("f1");}
      

    Kevin, one point contra your final statement, I believe that Processing does in fact define the class KeyEvent by default without an import -- its version just does not include VK_F1 specifically. The Processing KeyEvent includes KeyEvent.SHIFT, for example. KeyEvent members appear in Processing IDE autocompletions and run without requiring additional Java import statements.

    I'm not sure if the Processing KeyEvent is actually useful for anything, but I wonder if the advantage of approaches #1 and #3 is that, unlike approach #2, they won't shadow the Processing built-in KeyEvent.

Sign In or Register to comment.