Skip to content

Instantly share code, notes, and snippets.

@colinowens
Last active March 25, 2020 00:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colinowens/3816229 to your computer and use it in GitHub Desktop.
Save colinowens/3816229 to your computer and use it in GitHub Desktop.
Mouse tracking (outside of sketch window) in Processing
// trackScreen by Colin Owens
// Using JAVA mouseInfo for a bit of hackiness
// Use to your heart's content with attribution
// 2010
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import processing.pdf.*;
Point mouse;
float weight = .05;
int lastX, lastY;
int strokeColor = 0;
int clicked = 0;
void setup() {
background(255);
size(displayWidth, displayHeight);
mouse = MouseInfo.getPointerInfo().getLocation();
lastX = mouse.x;
lastY = mouse.y;
smooth();
beginRecord(PDF, "filename.pdf");
println("I'm running");
}
void draw() {
mouse = MouseInfo.getPointerInfo().getLocation();
noStroke();
fill(0, 10);
if (lastX == mouse.x && lastY == mouse.y) {
noFill();
stroke(strokeColor, 10);
strokeWeight(1);
ellipse(mouse.x, mouse.y, 20+weight, 20+weight);
noStroke();
fill(255, 03);
ellipse(mouse.x, mouse.y, 10+weight, 10+weight);
weight = weight+.05;
}
else {
stroke(strokeColor);
strokeWeight(.5);
line(lastX, lastY, mouse.x, mouse.y);
weight = .05;
}
lastX = mouse.x;
lastY = mouse.y;
}
void keyPressed() {
if (key == CODED) {
if (keyCode == UP) {
endRecord();
exit();
}
}
}
public void mousePressed(MouseEvent e) {
clicked++;
println(clicked);
}
@colinowens
Copy link
Author

Works in 2.x.

@ThanhMOOC
Copy link

Hello @colinowens, As your code, I see we can locate where the mouse is in the monitor. But If I want to get mouse's info in a the frame that created by processing, how can I modify the code? Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment