From 0edbae2b4e0c1da39cb77d8fd8c54e8fffb27e7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9C=9F=E4=BA=BA?= Date: Mon, 10 Jul 2017 23:47:22 +0800 Subject: [PATCH 1/2] feat: add keyboard event --- src/rprocessing/applet/BuiltinApplet.java | 63 +++++++++++++++++++++-- 1 file changed, 58 insertions(+), 5 deletions(-) diff --git a/src/rprocessing/applet/BuiltinApplet.java b/src/rprocessing/applet/BuiltinApplet.java index a2dd541..f9c64fe 100644 --- a/src/rprocessing/applet/BuiltinApplet.java +++ b/src/rprocessing/applet/BuiltinApplet.java @@ -32,6 +32,8 @@ public BuiltinApplet() { Session session = new SessionBuilder().withDefaultPackages().setPackageLoader(packageLoader).build(); this.renjinEngine = new RenjinScriptEngineFactory().getScriptEngine(session); + this.renjinEngine.put("key", "0"); + this.renjinEngine.put("keyCode", 0); } public void size(double width, double height) { @@ -54,6 +56,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 +101,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 +108,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); + } } From bf789d1ce4cec3c6b6fa32d626514cbd6ab8d17c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9C=9F=E4=BA=BA?= Date: Tue, 11 Jul 2017 10:54:25 +0800 Subject: [PATCH 2/2] style: move put statements to addPAppletToRContext --- src/rprocessing/RLangPApplet.java | 2 ++ src/rprocessing/applet/BuiltinApplet.java | 2 -- 2 files changed, 2 insertions(+), 2 deletions(-) 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 f9c64fe..c7747e4 100644 --- a/src/rprocessing/applet/BuiltinApplet.java +++ b/src/rprocessing/applet/BuiltinApplet.java @@ -32,8 +32,6 @@ public BuiltinApplet() { Session session = new SessionBuilder().withDefaultPackages().setPackageLoader(packageLoader).build(); this.renjinEngine = new RenjinScriptEngineFactory().getScriptEngine(session); - this.renjinEngine.put("key", "0"); - this.renjinEngine.put("keyCode", 0); } public void size(double width, double height) {