Programmatically move the cursor to a desired location on the screen

22

13

Is it possible to set/move the mouse pointer's location directly from Mathematica without using external tools like Run["xdotool mousemove x y"]?

my account_ram

Posted 2012-08-11T22:48:52.873

Reputation: 2 068

2If an interactive application like Mathematica did that, I would consider it a bug... Anyway, I don't think it's possible from within Mathematica. – Jens – 2012-08-12T02:50:54.767

You don't mean setting the current insertion point or selection? – Sjoerd C. de Vries – 2012-08-12T06:55:35.880

not the selection inside Mathematica. Just moving the mouse pointer anywhere inside the screen – my account_ram – 2012-08-15T06:34:20.247

Answers

39

Needs["JLink`"]
ReinstallJava[]
robotclass=JavaNew["java.awt.Robot"]
robotclass@mouseMove[#,300]&/@Range@900;

Andreas Lauschke

Posted 2012-08-11T22:48:52.873

Reputation: 3 600

Linking to Java is not considered using an external tool? Interesting.... – John – 2013-11-11T01:31:55.583

2@John: No, it's not. a) in the example of the o/p xdotool would be an external tool/application/program that you'd need to call. b) JLink is part of M, and WRI's official position is that this makes all of Java part of M (same with NETLink and .Net), as indicated by marketing materials. c) you ARE actually writing this in M. My code above is NOT Java code, it's M code. It just LOOKs like Java code (and that's a smart design choice of JLink/NETLink), but it isn't. These are all M symbols, it's M syntax, it runs from the M f/e, and it is executed/"scripted" through the kernel. – Andreas Lauschke – 2013-11-11T16:03:36.437

3Impressive .. :) – Dr. belisarius – 2012-08-21T00:50:28.940

1Second that, impressive indeed, +1. – Leonid Shifrin – 2012-08-21T00:53:47.707

5+1 Great ;-) Table[robotclass@mouseMove[Round[300 (Cos[x] + 1)], Round[100 (Sin[2 x] + 1)]], {x, 0, 2 Pi, .001}]; – Vitaliy Kaurov – 2012-08-21T01:25:51.830

Excellent - that seems like a prime candidate for causing some desktop mischief ;-) – Yves Klett – 2012-08-21T07:00:10.830

Very nice Andreas. +1. Java rules! – Rolf Mertig – 2012-08-21T09:55:27.473

2Impressive?? That's scary... – sebhofer – 2012-10-19T08:41:44.470

Impressive + Thanks ! – my account_ram – 2012-11-26T13:15:51.607