Skip to content

Instantly share code, notes, and snippets.

@Happsson
Last active February 6, 2018 13:47
Show Gist options
  • Save Happsson/778dd86ba1747f12d025f32db54673d4 to your computer and use it in GitHub Desktop.
Save Happsson/778dd86ba1747f12d025f32db54673d4 to your computer and use it in GitHub Desktop.
Processing sketch that lets you use any Arduino as a keyboard emulator.
/*
Auth. Hannes Paulsson
Used to simulate keypresses on any Arduino board.
In arduino sketch, start serial at baudrate 9600 (or edit variable in this sketch)
Only 0-9 and a-z (not case sensitive). Enter can be sent by writing \n.
If you need other keys,
add check for those in serialEvent()-function. Look at: https://docs.oracle.com/javase/7/docs/api/constant-values.html
under java.awt.KeyEvent to see the values for all possible keys.
In Arduino sketch, simply write serial.println("a") to emulate letter a.
You can also print longer sentences, and each letter will be emulated seperately.
Note that you need to use println, not print.
////// DISCLAIMER /////
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions: The above copyright notice and this permission
notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
(MIT License)
*/
import processing.serial.*;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.Arrays;
import java.awt.*;
int baud = 9600;
Serial mPort;
boolean connected;
Robot robot;
String serialArray = "";
float[] incommingMessage = new float[10];
int index = 0;
int drawVal;
void setup() {
size(400, 400);
smooth();
fill(0);
colorMode(RGB, 1);
println("initializing robot");
try{
robot = new Robot();
}catch(AWTException e){
println("Could not start robot");
}
for(int i = 0; i < Serial.list().length; i++){
serialArray += "["+i+"]: " + Serial.list()[i] + "\n";
}
}
void serialEvent(Serial mPort){
String msg = mPort.readString();
println(msg);
msg = msg.toUpperCase();
for(int i = 0; i < msg.length(); i++){
boolean valid = false;
if(msg.charAt(i) >= 0x30 && msg.charAt(i) <= 0x39){
valid = true; //A number, 0-9.
}
if(msg.charAt(i) >= 0x41 && msg.charAt(i) <= 0x5A){
valid = true; //Letter A-Z
}
if(msg.charAt(i) == '\n') valid = true;
if(valid){
robot.keyPress((int) msg.charAt(i));
robot.keyRelease((int) msg.charAt(i));
}
}
}
void draw() {
background(0,0,0);
if(!connected){
textSize(30);
fill(1,1,1,1);
text("Please select port \n(enter on keyboard)", 10,50);
textSize(18);
text(serialArray, 10,150);
}else{
if(connected){
textSize(30);
fill(1,1,1,1);
text("Running\nConnected", 10,50);
}
}
}
void keyPressed(){
if(!connected){
if(((int) key >= 48 && (int) key <= 57)){
int val = key - 48;
if(val < Serial.list().length){
serialArray = "Connecting..";
tryConnect(val);
}else{
println("invalid choice");
}
}
}
}
void tryConnect(int port){
if(mPort != null){
mPort.stop();
}
try{
mPort = new Serial(this, Serial.list()[port], baud);
connected = true;
mPort.bufferUntil(10);
}catch(Exception e){
connected = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment