Skip to content

Instantly share code, notes, and snippets.

@brendan-w
Last active June 17, 2020 10:54
Show Gist options
  • Save brendan-w/e222e49cc87c0e7a19fa to your computer and use it in GitHub Desktop.
Save brendan-w/e222e49cc87c0e7a19fa to your computer and use it in GitHub Desktop.
Processing app for sending keystrokes with TouchOSC
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.Map;
import oscP5.*;
Robot robot;
OscP5 oscP5;
final int PORT = 8000;
HashMap<String,Integer> key_mappings = new HashMap<String,Integer>();
void setup()
{
/*
Adjust key mappings to taste. Currently, these are for the first two
buttons of TouchOSC's "Simple" layout.
Key constants can be found here:
http://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyEvent.html
*/
key_mappings.put("/2/push1", KeyEvent.VK_LEFT);
key_mappings.put("/2/push2", KeyEvent.VK_RIGHT);
run();
size(400, 150);
draw_docs();
}
void run()
{
try
{
robot = new Robot();
}
catch(java.awt.AWTException e)
{
println(e);
}
/* start oscP5, listening for incoming messages at port 8000 */
oscP5 = new OscP5(this, PORT);
}
void oscEvent(OscMessage theOscMessage)
{
String addr = theOscMessage.addrPattern();
float val = theOscMessage.get(0).floatValue();
if(key_mappings.containsKey(addr))
{
int key = key_mappings.get(addr);
handle_key(val, key);
}
}
void handle_key(float val, int key)
{
if(val == 1.0)
{
robot.keyPress(key);
}
else
{
robot.keyRelease(key);
}
}
void draw_docs()
{
background(0);
fill(255);
textAlign(LEFT, TOP);
text("This processing app will send keystrokes to the current window in focus.", 3, 3);
text("Set TouchOSC to transmit to " + oscP5.ip() + " port " + PORT, 3, 20);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment