We are about to switch to a new forum software. Until then we have removed the registration on this forum.
This is how processing handles a keyEvent:
protected void handleKeyEvent(KeyEvent event) {
keyEvent = event;
key = event.getKey();
keyCode = event.getKeyCode();
switch (event.getAction()) {
case KeyEvent.PRESS:
keyPressed = true;
keyPressed(keyEvent);
break;
case KeyEvent.RELEASE:
keyPressed = false;
keyReleased(keyEvent);
break;
case KeyEvent.TYPE:
keyTyped(keyEvent);
break;
}
if (keyEventMethods != null) {
keyEventMethods.handle(new Object[] { event.getNative() });
}
handleMethods("keyEvent", new Object[] { event });
// if someone else wants to intercept the key, they should
// set key to zero (or something besides the ESC).
if (event.getAction() == KeyEvent.PRESS) {
//if (key == java.awt.event.KeyEvent.VK_ESCAPE) {
if (key == ESC) {
exit();
}
// When running tethered to the Processing application, respond to
// Ctrl-W (or Cmd-W) events by closing the sketch. Not enabled when
// running independently, because this sketch may be one component
// embedded inside an application that has its own close behavior.
if (external &&
event.getKeyCode() == 'W' &&
((event.isMetaDown() && platform == MACOSX) ||
(event.isControlDown() && platform != MACOSX))) {
// Can't use this native stuff b/c the native event might be NEWT
// if (external && event.getNative() instanceof java.awt.event.KeyEvent &&
// ((java.awt.event.KeyEvent) event.getNative()).getModifiers() ==
// Toolkit.getDefaultToolkit().getMenuShortcutKeyMask() &&
// event.getKeyCode() == 'W') {
exit();
}
}
}
This means that if you register a keyEvent it will always be called after keyPressed:
void setup() {
registerMethod("keyEvent", this);
}
void draw() {}
void keyEvent(KeyEvent evt) {
println("keyEvent");
}
void keyPressed() {
println("keyPressed");
}
I think it would be much better if the keyEvent get's called before keyPressed. This way with a library, the library creator can do things before the user.
So this:
protected void handleKeyEvent(KeyEvent event) {
keyEvent = event;
key = event.getKey();
keyCode = event.getKeyCode();
// set booleans so they are correct when a user checks for them in keyEvent
switch (event.getAction()) {
case KeyEvent.PRESS:
keyPressed = true;
break;
case KeyEvent.RELEASE:
keyPressed = false;
break;
}
if (keyEventMethods != null) {
keyEventMethods.handle(new Object[] { event.getNative() });
}
// handle the registered keyEvent's before calling keyPressed() etc.
handleMethods("keyEvent", new Object[] { event });
switch (event.getAction()) {
case KeyEvent.PRESS:
keyPressed(keyEvent);
break;
case KeyEvent.RELEASE:
keyReleased(keyEvent);
break;
case KeyEvent.TYPE:
keyTyped(keyEvent);
break;
}
// rest of code...
Do more people agree with me?
Comments
MouseEvent has the order like I think it should be, first registeredMethods, and as last the user.
I don't have a particularly strong opinion. What are the use cases for doing things before the user? Aren't there use cases where the event must be processed after the user?
In some libraries, we can hook before and after a user event, that allows more flexibility...
I made a method
public boolean keyPressed(char c) {
to check if a key is pressed. But if the user get's a keyPressed before the library then it doesn't work.I fixed it like this:
But this only works if the PApplet get extended by my library.
Your library behaves more like a 1-man PApplet fork!
You should have a general plan to focus on rather than trying to alter every detail on it.
Also, it's not smart to deviate too much from the original, since that's gonna make every re-emerge a daunting task!
I have a general plan :) And the remaining stuff I have to do has nothing to do with PApplet.
Some things might break in future but I don't mind :)
I would agree with @PhiLho's comments.
In my case the current order has not caused any problems with the G4P library when using GTextField and GTextArea objects.
The only reason I can see for wanting the library to process the keyEvent BEFORE the sketch is if you want to modify the keyEvent object before forwarding it to the sketch.
It might be helpful if you give us an idea what you are trying to achieve.
I want the user to be able to check for multiple keys being pressed at the same time. This already works:
But to made that work i extended PApplet and used:
Cause else I would always be one step behind. Makes sense for this to change the order right?
You're gonna need a
boolean[] keys = new boolean[1<<020];
in order to have a full Unicode-16 storage! :-?For the function key's or something? Only a few keys on my keyboard produce a high number.
And if processing would change the order of handling things, that wouldn't hurt right?
You shouldn't confine your "library" for US keyboards only!
Japanese, Chinese, Arabic, etc., gonna use full keyCode spectrum!
If you decide for a subset keyCode range, you gotta make a method which checks the index argument range!
More complete & easier solution is the full
1<<020
array's length for Unicode-16! :-BDamn, i wish i had a Chinese keyboard :)
1<<020 it is then.