Skip to content

Fixed SHIFT and PAGE_UP keys using the same keyCode #1033

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion core/src/processing/opengl/PSurfaceJOGL.java
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,16 @@ protected void nativeMouseEvent(com.jogamp.newt.event.MouseEvent nativeEvent,
sketch.postEvent(me);
}

private short normalizeKeyCode(short newtKeyCode) {
switch (newtKeyCode) {
case com.jogamp.newt.event.KeyEvent.VK_PAGE_UP:
return java.awt.event.KeyEvent.VK_PAGE_UP;
case com.jogamp.newt.event.KeyEvent.VK_SHIFT:
return java.awt.event.KeyEvent.VK_SHIFT;
default:
return newtKeyCode;
}
}

protected void nativeKeyEvent(com.jogamp.newt.event.KeyEvent nativeEvent,
int peAction) {
Expand All @@ -1080,7 +1090,8 @@ protected void nativeKeyEvent(com.jogamp.newt.event.KeyEvent nativeEvent,
// InputEvent.META_MASK |
// InputEvent.ALT_MASK);

short code = nativeEvent.getKeyCode();
short code = normalizeKeyCode(nativeEvent.getKeyCode());

char keyChar;
int keyCode;
if (isPCodedKey(code, nativeEvent.isPrintableKey())) {
Expand Down
37 changes: 37 additions & 0 deletions core/test/processing/core/PAppletKeyEventTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import org.junit.Test;
import processing.event.KeyEvent;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class PAppletKeyEventTest {

private static final int SHIFT_MASK = 1;
Expand Down Expand Up @@ -137,4 +141,37 @@ public void testKeyFocusLost() {
Assert.assertFalse("keyPressed should be false after focus lost", applet.keyPressed);
Assert.assertEquals("pressedKeys should be empty after focus lost", true, applet.pressedKeys.isEmpty());
}

@Test
public void testShiftAndPageUpKeyCodesAreDifferent() {
final int VK_SHIFT = java.awt.event.KeyEvent.VK_SHIFT;
final int VK_PAGE_UP = java.awt.event.KeyEvent.VK_PAGE_UP;

long shiftHash = ((long)VK_SHIFT << Character.SIZE);
long pageUpHash = ((long)VK_PAGE_UP << Character.SIZE);

KeyEvent shiftPressEvent = new KeyEvent(null, 0L, KeyEvent.PRESS, SHIFT_MASK, '\0', VK_SHIFT, false);
applet.handleKeyEvent(shiftPressEvent);

assertTrue("keyPressed must be true", applet.keyPressed);
assertTrue("SHIFT should be in pressedKeys", applet.pressedKeys.contains(shiftHash));

KeyEvent pageUpPressEvent = new KeyEvent(null, 0L, KeyEvent.PRESS, 0, '\0', VK_PAGE_UP, false);
applet.handleKeyEvent(pageUpPressEvent);

assertEquals("pressedKeys must contain exactly two keys", 2, applet.pressedKeys.size());
assertTrue("PAGE_UP should be in pressedKeys", applet.pressedKeys.contains(pageUpHash));

KeyEvent shiftRelease = new KeyEvent(null, 0L, KeyEvent.RELEASE, 0, '\0', VK_SHIFT, false);
applet.handleKeyEvent(shiftRelease);
assertFalse("SHIFT should have been removed", applet.pressedKeys.contains(shiftHash));
assertTrue ("PAGE_UP should still be down", applet.pressedKeys.contains(pageUpHash));
assertTrue ("keyPressed must still be true", applet.keyPressed);
assertEquals("pressedKeys must now have one key", 1, applet.pressedKeys.size());

KeyEvent pageUpRelease = new KeyEvent(null, 0L, KeyEvent.RELEASE, 0, '\0', VK_PAGE_UP, false);
applet.handleKeyEvent(pageUpRelease);
assertTrue ("pressedKeys must now be empty", applet.pressedKeys.isEmpty());
assertFalse("keyPressed must be false", applet.keyPressed);
}
}