Skip to content

Commit 30bc274

Browse files
authored
Merge pull request #1033 from Rishab87/shift-page-keys
Fixed `SHIFT` and `PAGE_UP` keys using the same keyCode
2 parents 05c1292 + abc39e9 commit 30bc274

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

core/src/processing/opengl/PSurfaceJOGL.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1069,6 +1069,16 @@ protected void nativeMouseEvent(com.jogamp.newt.event.MouseEvent nativeEvent,
10691069
sketch.postEvent(me);
10701070
}
10711071

1072+
private short normalizeKeyCode(short newtKeyCode) {
1073+
switch (newtKeyCode) {
1074+
case com.jogamp.newt.event.KeyEvent.VK_PAGE_UP:
1075+
return java.awt.event.KeyEvent.VK_PAGE_UP;
1076+
case com.jogamp.newt.event.KeyEvent.VK_SHIFT:
1077+
return java.awt.event.KeyEvent.VK_SHIFT;
1078+
default:
1079+
return newtKeyCode;
1080+
}
1081+
}
10721082

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

1083-
short code = nativeEvent.getKeyCode();
1093+
short code = normalizeKeyCode(nativeEvent.getKeyCode());
1094+
10841095
char keyChar;
10851096
int keyCode;
10861097
if (isPCodedKey(code, nativeEvent.isPrintableKey())) {

core/test/processing/core/PAppletKeyEventTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
import org.junit.Test;
66
import processing.event.KeyEvent;
77

8+
import static org.junit.Assert.assertEquals;
9+
import static org.junit.Assert.assertFalse;
10+
import static org.junit.Assert.assertTrue;
11+
812
public class PAppletKeyEventTest {
913

1014
private static final int SHIFT_MASK = 1;
@@ -137,4 +141,37 @@ public void testKeyFocusLost() {
137141
Assert.assertFalse("keyPressed should be false after focus lost", applet.keyPressed);
138142
Assert.assertEquals("pressedKeys should be empty after focus lost", true, applet.pressedKeys.isEmpty());
139143
}
144+
145+
@Test
146+
public void testShiftAndPageUpKeyCodesAreDifferent() {
147+
final int VK_SHIFT = java.awt.event.KeyEvent.VK_SHIFT;
148+
final int VK_PAGE_UP = java.awt.event.KeyEvent.VK_PAGE_UP;
149+
150+
long shiftHash = ((long)VK_SHIFT << Character.SIZE);
151+
long pageUpHash = ((long)VK_PAGE_UP << Character.SIZE);
152+
153+
KeyEvent shiftPressEvent = new KeyEvent(null, 0L, KeyEvent.PRESS, SHIFT_MASK, '\0', VK_SHIFT, false);
154+
applet.handleKeyEvent(shiftPressEvent);
155+
156+
assertTrue("keyPressed must be true", applet.keyPressed);
157+
assertTrue("SHIFT should be in pressedKeys", applet.pressedKeys.contains(shiftHash));
158+
159+
KeyEvent pageUpPressEvent = new KeyEvent(null, 0L, KeyEvent.PRESS, 0, '\0', VK_PAGE_UP, false);
160+
applet.handleKeyEvent(pageUpPressEvent);
161+
162+
assertEquals("pressedKeys must contain exactly two keys", 2, applet.pressedKeys.size());
163+
assertTrue("PAGE_UP should be in pressedKeys", applet.pressedKeys.contains(pageUpHash));
164+
165+
KeyEvent shiftRelease = new KeyEvent(null, 0L, KeyEvent.RELEASE, 0, '\0', VK_SHIFT, false);
166+
applet.handleKeyEvent(shiftRelease);
167+
assertFalse("SHIFT should have been removed", applet.pressedKeys.contains(shiftHash));
168+
assertTrue ("PAGE_UP should still be down", applet.pressedKeys.contains(pageUpHash));
169+
assertTrue ("keyPressed must still be true", applet.keyPressed);
170+
assertEquals("pressedKeys must now have one key", 1, applet.pressedKeys.size());
171+
172+
KeyEvent pageUpRelease = new KeyEvent(null, 0L, KeyEvent.RELEASE, 0, '\0', VK_PAGE_UP, false);
173+
applet.handleKeyEvent(pageUpRelease);
174+
assertTrue ("pressedKeys must now be empty", applet.pressedKeys.isEmpty());
175+
assertFalse("keyPressed must be false", applet.keyPressed);
176+
}
140177
}

0 commit comments

Comments
 (0)