How to Grab Key Events Outside Java
In the Java programming language, the "keyDown" event detects each time a user presses a key. You can use this event handler in a Java application that runs in the background on a computer, allowing you to detect keystrokes outside of the Java program itself. You must set up the keyDown event and code to determine the computer’s response when the user presses a key.
Instructions
-
-
1
Open the Java editor that you use to write your code and load up the project you want to edit. Double-click on your Java source code file.
-
2
Add the event handler that detects when a user presses a key. The following Java code detects each key press:
public boolean mouseDown( Event evt, int x, int y)
{}
The Java code that you want to execute when the key is pressed is placed within the brackets.
-
-
3
Add code to the event handler. The following example echoes the keystroke back to the user:
System.out.println("You pressed the " + evt.key + " key.");
-
1