mirror of
https://github.com/processing/processing4.git
synced 2026-06-16 04:26:26 +02:00
Merge pull request #5050 from JakubValtar/fix-keypressed
Fix keyPressed for multiple keys
This commit is contained in:
@@ -658,7 +658,7 @@ public class PApplet implements PConstants {
|
||||
* @see PApplet#keyReleased()
|
||||
*/
|
||||
public boolean keyPressed;
|
||||
int keyPressedCount;
|
||||
List<Long> pressedKeys = new ArrayList<>(6);
|
||||
|
||||
/**
|
||||
* The last KeyEvent object passed into a mouse function.
|
||||
@@ -2935,13 +2935,14 @@ public class PApplet implements PConstants {
|
||||
|
||||
switch (event.getAction()) {
|
||||
case KeyEvent.PRESS:
|
||||
keyPressedCount++;
|
||||
Long hash = ((long) keyCode << Character.SIZE) | key;
|
||||
if (!pressedKeys.contains(hash)) pressedKeys.add(hash);
|
||||
keyPressed = true;
|
||||
keyPressed(keyEvent);
|
||||
break;
|
||||
case KeyEvent.RELEASE:
|
||||
keyPressedCount--;
|
||||
keyPressed = (keyPressedCount > 0);
|
||||
pressedKeys.remove(((long) keyCode << Character.SIZE) | key);
|
||||
keyPressed = !pressedKeys.isEmpty();
|
||||
keyReleased(keyEvent);
|
||||
break;
|
||||
case KeyEvent.TYPE:
|
||||
@@ -3123,7 +3124,10 @@ public class PApplet implements PConstants {
|
||||
public void focusGained() { }
|
||||
|
||||
|
||||
public void focusLost() { }
|
||||
public void focusLost() {
|
||||
// TODO: if user overrides this without calling super it's not gonna work
|
||||
pressedKeys.clear();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user