Adding multiple event handlers
in
Programming Questions
•
1 month ago
Hello folks,
This currently resides in a tab (thus as a static (inner) class, if I understand the environment correctly). As you can see, I'm using various key input functions such as keyPressed(). The problem is that I cannot use these functions in any other tab, since it is considered a duplicate method.
I have a simple keyboard input class that allows me to handle multiple simultaneous key presses:
- import java.util.*;
- private HashSet<Integer> keySet = new HashSet<Integer>();
- public void keyPressed() {
- keySet.add(keyCode);
- }
- public void keyReleased() {
- keySet.remove(keyCode);
- }
- public boolean isKeyDown(int pKey) {
- return keySet.contains(pKey);
- }
Ultimately, I'd like to be able to register multiple event handlers (and I don't want to work outside of Processing's built-in event handling, since apparently it does some fancy things such as event caching between draw calls, etc).
One solution is to wrap the tabbed file into its own class, rename the keyPressed() etc functions so that they are not overriding PApplet's built-in functions (i.e. onKeyPressed), then call those renamed functions from within the main tab's keyPressed() method - i.e.:
- {inside main tab}
- KeyboardInput kb = new KeyboardInput();
- ...
- public void keyPressed(){
- kb.onKeyPressed();
- ..other stuff..
- }
Is there any built-in way to do this?
1