diff --git a/core/src/processing/opengl/PSurfaceJOGL.java b/core/src/processing/opengl/PSurfaceJOGL.java index 4979b6a75..064f9a8dd 100644 --- a/core/src/processing/opengl/PSurfaceJOGL.java +++ b/core/src/processing/opengl/PSurfaceJOGL.java @@ -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) { @@ -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())) { diff --git a/core/test/processing/core/PAppletKeyEventTest.java b/core/test/processing/core/PAppletKeyEventTest.java index 19770ebc3..e9c440a9c 100644 --- a/core/test/processing/core/PAppletKeyEventTest.java +++ b/core/test/processing/core/PAppletKeyEventTest.java @@ -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; @@ -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); + } }