Skip to content

Commit 917bebc

Browse files
hawkingreigaocegege
authored andcommitted
feat: add keyboard event (#148)
* feat: add keyboard event * style: move put statements to addPAppletToRContext
1 parent 9ef02a2 commit 917bebc

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

src/rprocessing/RLangPApplet.java

+2
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ public void addPAppletToRContext() {
135135
// This is a trick to be deprecated. It is used to print
136136
// messages in Processing app console by stdout$print(msg).
137137
this.renjinEngine.put("stdout", stdout);
138+
this.renjinEngine.put("key", "0");
139+
this.renjinEngine.put("keyCode", 0);
138140
}
139141

140142
public void runBlock(final String[] arguments) throws RSketchError {

src/rprocessing/applet/BuiltinApplet.java

+56-5
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,31 @@ public double getPI() {
5454
return PI;
5555
}
5656

57+
@Override
58+
public void mouseClicked() {
59+
wrapMouseVariables();
60+
}
61+
62+
@Override
63+
public void mouseMoved() {
64+
wrapMouseVariables();
65+
}
66+
67+
@Override
68+
public void mousePressed() {
69+
wrapMouseVariables();
70+
}
71+
72+
@Override
73+
public void mouseReleased() {
74+
wrapMouseVariables();
75+
}
76+
77+
@Override
78+
public void mouseDragged() {
79+
wrapMouseVariables();
80+
}
81+
5782
/**
5883
*
5984
* @see processing.core.PApplet#focusGained()
@@ -74,16 +99,42 @@ public void focusLost() {
7499
this.renjinEngine.put("focused", super.focused);
75100
}
76101

77-
@Override
78-
public void mouseMoved() {
79-
wrapMouseVariables();
80-
}
81-
82102
protected void wrapMouseVariables() {
83103
this.renjinEngine.put("mouseX", mouseX);
84104
this.renjinEngine.put("mouseY", mouseY);
85105
this.renjinEngine.put("pmouseX", pmouseX);
86106
this.renjinEngine.put("pmouseY", pmouseY);
87107
// this.renjinEngine.put("mouseButton", mouseButton);
88108
}
109+
110+
@Override
111+
public void keyPressed() {
112+
wrapKeyVariables();
113+
}
114+
115+
@Override
116+
public void keyReleased() {
117+
wrapKeyVariables();
118+
}
119+
120+
@Override
121+
public void keyTyped() {
122+
wrapKeyVariables();
123+
}
124+
125+
private char lastKey = Character.MIN_VALUE;
126+
127+
protected void wrapKeyVariables() {
128+
if (lastKey != key) {
129+
lastKey = key;
130+
/*
131+
* If key is "CODED", i.e., an arrow key or other non-printable, pass that
132+
* value through as-is. If it's printable, convert it to a unicode string,
133+
* so that the user can compare key == 'x' instead of key == ord('x').
134+
*/
135+
final char pyKey = key == CODED ? parseChar(Integer.valueOf(key)) : parseChar(key);
136+
this.renjinEngine.put("key", pyKey);
137+
}
138+
this.renjinEngine.put("keyCode", keyCode);
139+
}
89140
}

0 commit comments

Comments
 (0)