Skip to content

Commit 591cc12

Browse files
authored
src: Refactor the logic about key (#210)
* src: Refactor the logic about key Signed-off-by: Ce Gao <[email protected]> * reference: Add keyPressed1 Signed-off-by: Ce Gao <[email protected]> * reference: Add keyPressedVar Signed-off-by: Ce Gao <[email protected]> * reference: Add keyReleased Signed-off-by: Ce Gao <[email protected]> * reference: Add keyTyped Signed-off-by: Ce Gao <[email protected]>
1 parent a18902a commit 591cc12

File tree

9 files changed

+121
-26
lines changed

9 files changed

+121
-26
lines changed

examples/reference/key/key1/key1.rpde

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Click on the window to give it focus, and press the 'B' key.
2+
3+
draw <- function() {
4+
if (keyPressedVar == TRUE) {
5+
if (key == "b" || key == "B") {
6+
fill(0)
7+
}
8+
} else {
9+
fill(255)
10+
}
11+
rect(25, 25, 50, 50)
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
fillVal <- 126
2+
3+
draw <- function() {
4+
fill(fillVal)
5+
rect(25, 25, 50, 50)
6+
}
7+
8+
keyPressed <- function() {
9+
# See https://github.com/gaocegege/Processing.R/issues/209
10+
if (key == CODED) {
11+
if (keyCode == UP) {
12+
fillVal = 255
13+
} else if (keyCode == DOWN) {
14+
fillVal = 0
15+
}
16+
} else {
17+
fillVal = 126
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Click on the image to give it focus, and then press any key.
2+
3+
value <- 0
4+
5+
draw <- function() {
6+
fill(value)
7+
rect(25, 25, 50, 50)
8+
}
9+
10+
keyPressed <- function() {
11+
if (value == 0) {
12+
value = 255
13+
} else {
14+
value = 0
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
category: Input
2+
subcategory: Keyboard
3+
description: "
4+
The Boolean system variable <b>keyPressed</b> is <b>TRUE</b> if any key is pressed and <b>FALSE</b> if no keys are pressed.
5+
"
6+
syntax: ""
7+
related:
8+
- key
9+
- keyCode
10+
- keyPressed
11+
- keyReleased
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Click on the image to give it focus, and then press any key.
2+
3+
# Note: The rectangle in this example may flicker as the operating system may
4+
# register a long key press as a repetition of key presses.
5+
6+
draw <- function() {
7+
if (keyPressedVar == TRUE) {
8+
fill(0)
9+
} else {
10+
fill(255)
11+
}
12+
rect(25, 25, 50, 50)
13+
}

examples/reference/keyPressed_var/.property.yml

-11
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Click on the image to give it focus, and then press any key.
2+
3+
value <- 0
4+
5+
draw <- function() {
6+
fill(value)
7+
rect(25, 25, 50, 50)
8+
}
9+
10+
keyReleased <- function() {
11+
if (value == 0) {
12+
value = 255
13+
} else {
14+
value = 0
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Run this program to learn how each of these functions relate to the others.
2+
3+
draw <- function() {
4+
} # Empty draw() needed to keep the program running
5+
6+
keyPressed <- function() {
7+
print("keyPressed")
8+
print(keyCode)
9+
print(key)
10+
}
11+
12+
keyTyped <- function() {
13+
print("keyTyped")
14+
print(keyCode)
15+
print(key)
16+
}
17+
18+
keyReleased <- function() {
19+
print("keyReleased")
20+
print(keyCode)
21+
print(key)
22+
}

src/rprocessing/RLangPApplet.java

+12-15
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import processing.core.PApplet;
2727
import processing.core.PConstants;
2828
import processing.core.PSurface;
29+
import processing.event.KeyEvent;
2930
import processing.event.MouseEvent;
3031
import processing.javafx.PSurfaceFX;
3132
import processing.opengl.PSurfaceJOGL;
@@ -370,16 +371,17 @@ private boolean isMixMode() {
370371
*/
371372
protected void wrapProcessingVariables() {
372373
log("Wrap Processing built-in variables into R top context.");
373-
// TODO: Find some ways to push constants into R.
374-
wrapMouseVariables();
374+
375+
this.wrapMouseVariables();
376+
this.wrapKeyVariables();
377+
375378
this.renjinEngine.put("width", width);
376379
this.renjinEngine.put("height", height);
377380
this.renjinEngine.put("displayWidth", displayWidth);
378381
this.renjinEngine.put("displayHeight", displayHeight);
379382
this.renjinEngine.put("focused", focused);
380383
this.renjinEngine.put("pixelWidth", pixelWidth);
381384
this.renjinEngine.put("pixelHeight", pixelHeight);
382-
// this.renjinEngine.put("keyPressed", keyPressed);
383385
}
384386

385387
@Override
@@ -454,6 +456,12 @@ private void applyFunction(String name) {
454456
}
455457
}
456458

459+
@Override
460+
protected void handleKeyEvent(KeyEvent event) {
461+
super.handleKeyEvent(event);
462+
wrapKeyVariables();
463+
}
464+
457465
@Override
458466
public void keyPressed() {
459467
wrapKeyVariables();
@@ -472,19 +480,8 @@ public void keyTyped() {
472480
applyFunction(Constant.KEYTYPED_NAME);
473481
}
474482

475-
private char lastKey = Character.MIN_VALUE;
476-
477483
protected void wrapKeyVariables() {
478-
if (lastKey != key) {
479-
lastKey = key;
480-
/*
481-
* If key is "CODED", i.e., an arrow key or other non-printable, pass that value through
482-
* as-is. If it's printable, convert it to a unicode string, so that the user can compare key
483-
* == 'x' instead of key == ord('x').
484-
*/
485-
final char pyKey = key == CODED ? parseChar(Integer.valueOf(key)) : parseChar(key);
486-
this.renjinEngine.put("key", pyKey);
487-
}
484+
this.renjinEngine.put("key", String.valueOf(key));
488485
this.renjinEngine.put("keyCode", keyCode);
489486
this.renjinEngine.put("keyPressedVar", keyPressed);
490487
}

0 commit comments

Comments
 (0)