// This code made for Keystroke Dynamic skeleton demo by Kun Qian import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import processing.core.*; public class KeystrokeCollector extends PApplet { /** * */ private static final long serialVersionUID = 1L; PFont myFont; public static boolean keypressed, train, loggedin, update; //update if user entered correct int trainitr; long start, last; float x, y; Typer tp; UInput login; String[] targets, users; ArrayList[] traindata; boolean mouseOver; int index, trainnum; String url = "http://kunqian.am.dhcp.ti.com/~kqian/"; // i.e. http://www.google.com/ (with a slash on the end public void setup() { size(500, 500, JAVA2D); keypressed = false; update = false; loggedin = false; train = false; trainitr = 0; trainnum = 3; start = 0; index = 0; myFont = loadFont("Courier-48.vlw"); textAlign(CENTER, CENTER); smooth(); noStroke(); tp = new Typer(this, 20); login = new UInput(this,width/2,height/2); //colorMode(HSB, 255); targets = loadStrings("objectives"); users = loadStrings("usernames.txt"); traindata = new ArrayList[trainnum]; if (!train) readData(); textFont(myFont, 20); } public void login() { login.display(); textAlign(RIGHT,CENTER); text("username: ",width/2-10,height/2); } public void draw() { background(color(255)); if(!loggedin) { login(); return; } if (index >= targets.length) { textAlign(CENTER, CENTER); text("DONE", width / 2, height / 2); return; } fill(color(24,59,183)); textFont(myFont, 30); textAlign(CENTER,CENTER); text(targets[index],width/2,height/3); textAlign(LEFT,CENTER); tp.setObj(targets[index]); fill(0); tp.display(); } public void readData() { String goal = targets[index]; boolean exists =(createInput("keys/" + goal+".txt")!= null); // println(goal); if (!exists) { train = true; // System.out.println("trainning"); trainitr = 0; return; } String[] tmp = loadStrings("keys/" + goal + ".txt"); // println(tmp); int[] times = new int[tmp.length]; String[] ups = new String[tmp.length]; for (int i = 0; i < tmp.length; i++) { String[] tmp2 = split(tmp[i], ","); times[i] = Integer.parseInt(tmp2[0]); ups[i] = tmp2[3]; } } public void analyze(Typer tper) { // background(255); ArrayList keys = tper.getKeys(); int[] times = new int[keys.size()]; // textFont(myFont,12); fill(0); for (int i = 0; i < keys.size(); i++) { Key thiskey = (Key) keys.get(i); times[i] = (int) thiskey.getTime(); } int maxtime = max(times); strokeWeight(3); for (int i = 0; i < times.length; i++) { ellipse(map(times[i], 0, maxtime, 20, width - 20), 350, 10, 10); } // noLoop(); } public void mousePressed() { if (mouseOver) { train = true; // System.out.println("trainning"); index = 0; trainitr = 0; return; } } public void keyPressed() { loop(); boolean ret = key == ENTER || key == RETURN; if(loggedin) { if (!keypressed) { start = millis(); last = start; keypressed = true; background(255); } // if(!update){ if (ret) { // keypressed = false; tp.addKey(key, keyCode, (millis() - start)); // println("\\n"); // analyze(tp); // index++; // tp.clear(); return; } else { tp.addKey(key, keyCode, (millis() - start)); } } else { if(key != BACKSPACE && !ret) { if(!loggedin) login.addKey(key); } else if(ret) { for(int i =0; i= trainnum) { System.out.println("Writing"); writeBack(); } } public int getNext(ArrayList list, char c) { for (int i = 0; i < list.size(); i++) { Key tmp = (Key) list.get(i); if (tmp.getInKey() == c) return i; } return -1; } public void dynamicPlot(Typer tper, PGraphics plot) { ArrayList keys = tper.getKeys(); // println(keys.size()); int[] times = new int[keys.size()]; String[] ups = new String[keys.size()]; // textFont(myFont,12); plot.fill(0); for (int i = 0; i < keys.size(); i++) { Key thiskey = (Key) keys.get(i); times[i] = (int) thiskey.getTime(); ups[i] = thiskey.getUp(); } int maxtime = max(times); plot.strokeWeight(1); for (int i = 0; i < times.length; i++) { if (ups[i].equals("down")) plot.fill(0); else plot.noFill(); plot.ellipse(map(times[i], 0, maxtime, 5, plot.width - 5), plot.height / 2, 3, 3); } } public void staticPlot(int[] times, String[] ups, PGraphics plot) { // textFont(myFont,12); plot.beginDraw(); plot.smooth(); plot.background(255); plot.fill(0); int maxtime = max(times); plot.strokeWeight(1); for (int i = 0; i < times.length; i++) { if (ups[i].equals("down")) plot.fill(0); else plot.noFill(); plot.ellipse(map(times[i], 0, maxtime, 5, plot.width - 5), plot.height / 2, 3, 3); } plot.endDraw(); } public void saveToWeb_saveFileString(String title, String ext, String folder, String[] data, boolean popup) { println("SAVING File START"); postData(title,ext,folder,join(data,"\n").getBytes(),popup); println("SAVING File STOP"); } public void saveToWeb_saveFile(String title, String ext, String folder, byte[] data, boolean popup) { println("SAVING File START"); postData(title,ext,folder,data,popup); println("SAVING File STOP"); } public void postData(String title, String ext, String folder, byte[] bytes, boolean popup) { try{ URL u = new URL(url+"savefile.php?title="+title+"&ext="+ext+"&folder="+folder); URLConnection c = u.openConnection(); // post multipart data c.setDoOutput(true); c.setDoInput(true); c.setUseCaches(false); // set request headers c.setRequestProperty("Content-Type", "multipart/form-data; boundary=AXi93A"); // open a stream which can write to the url DataOutputStream dstream = new DataOutputStream(c.getOutputStream()); // write content to the server, begin with the tag that says a content element is comming dstream.writeBytes("--AXi93A\r\n"); // discribe the content dstream.writeBytes("Content-Disposition: form-data; name=\"data\"; filename=\"whatever\" \r\nContent-Type: image/jpeg\r\nContent-Transfer-Encoding: binary\r\n\r\n"); dstream.write(bytes,0,bytes.length); // close the multipart form request dstream.writeBytes("\r\n--AXi93A--\r\n\r\n"); dstream.flush(); dstream.close(); // read the output from the URL try{ BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream())); String sIn = in.readLine(); boolean b = true; while(sIn!=null){ if(sIn!=null){ if(popup) if(sIn.substring(0,folder.length()).equals(folder)) link(url+sIn, "_blank"); System.out.println(sIn); } sIn = in.readLine(); } } catch(Exception e){ e.printStackTrace(); } } catch(Exception e){ e.printStackTrace(); } } }