diff --git a/src/rprocessing/RLangPApplet.java b/src/rprocessing/RLangPApplet.java index 00a5776..e38732b 100644 --- a/src/rprocessing/RLangPApplet.java +++ b/src/rprocessing/RLangPApplet.java @@ -135,6 +135,8 @@ public void addPAppletToRContext() { // This is a trick to be deprecated. It is used to print // messages in Processing app console by stdout$print(msg). this.renjinEngine.put("stdout", stdout); + this.renjinEngine.put("key", "0"); + this.renjinEngine.put("keyCode", 0); } public void runBlock(final String[] arguments) throws RSketchError { diff --git a/src/rprocessing/applet/BuiltinApplet.java b/src/rprocessing/applet/BuiltinApplet.java index a2dd541..c7747e4 100644 --- a/src/rprocessing/applet/BuiltinApplet.java +++ b/src/rprocessing/applet/BuiltinApplet.java @@ -54,6 +54,31 @@ public double getPI() { return PI; } + @Override + public void mouseClicked() { + wrapMouseVariables(); + } + + @Override + public void mouseMoved() { + wrapMouseVariables(); + } + + @Override + public void mousePressed() { + wrapMouseVariables(); + } + + @Override + public void mouseReleased() { + wrapMouseVariables(); + } + + @Override + public void mouseDragged() { + wrapMouseVariables(); + } + /** * * @see processing.core.PApplet#focusGained() @@ -74,11 +99,6 @@ public void focusLost() { this.renjinEngine.put("focused", super.focused); } - @Override - public void mouseMoved() { - wrapMouseVariables(); - } - protected void wrapMouseVariables() { this.renjinEngine.put("mouseX", mouseX); this.renjinEngine.put("mouseY", mouseY); @@ -86,4 +106,35 @@ protected void wrapMouseVariables() { this.renjinEngine.put("pmouseY", pmouseY); // this.renjinEngine.put("mouseButton", mouseButton); } + + @Override + public void keyPressed() { + wrapKeyVariables(); + } + + @Override + public void keyReleased() { + wrapKeyVariables(); + } + + @Override + public void keyTyped() { + wrapKeyVariables(); + } + + private char lastKey = Character.MIN_VALUE; + + protected void wrapKeyVariables() { + if (lastKey != key) { + lastKey = key; + /* + * If key is "CODED", i.e., an arrow key or other non-printable, pass that + * value through as-is. If it's printable, convert it to a unicode string, + * so that the user can compare key == 'x' instead of key == ord('x'). + */ + final char pyKey = key == CODED ? parseChar(Integer.valueOf(key)) : parseChar(key); + this.renjinEngine.put("key", pyKey); + } + this.renjinEngine.put("keyCode", keyCode); + } }