Skip to content

feat: add keyboard event #148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/rprocessing/RLangPApplet.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
61 changes: 56 additions & 5 deletions src/rprocessing/applet/BuiltinApplet.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -74,16 +99,42 @@ 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);
this.renjinEngine.put("pmouseX", pmouseX);
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);
}
}