Take the 2-minute tour ×

Is it possible to simulate mouse's move in PHP ? By that I mean to do something like :

$mouse->moveToCoordinate($x,$Y); // will move the screen to to the coordinate $X, $Y of the screen
$mouse->moveVector($x,$Y); // will move from the current point to the (current X + $X, current Y + $Y); 
$mouse->click(); // will simulate a mouse click on the screen.

This would be usable, even if no browser is open (so cannot use the classic javascript solution).

share|improve this question
PHP=Server, Mouse=Client so simply NO. i dont even think this is possible in Javascript –  Martin Holzhauer Oct 12 '10 at 11:17
even if no browser is open?! what are you trying to automate that you can't automate using programming? Sounds a bit dubious to me, why would you need to move somebody's mouse around the screen and click something without them having their browser open? –  Thomas Clayson Oct 12 '10 at 11:21
IMO valid question, even though the answer is "can't do that". What purpose are you trying to accomplish? Maybe it could be done in a different way... –  Piskvor Oct 12 '10 at 11:31
add comment

6 Answers

up vote 1 down vote accepted

Just as an exercise. It might be possible to write standalone desktop PHP app that has access to user pointer. For that you have to use bindings such as http://gtk.php.net/ (there were Qt bindings some time ago, but project seems to be dead). And even that it might be hard. PHP-GTK is not well documented at this moment.

share|improve this answer
Thanks for GTK suggestion, I haven't thought about that. –  Cedric Oct 12 '10 at 12:53
add comment

PHP is a server side scripting language and cannot do that. You should do that by Javascript. It's possible to do that from PHP (write needed Javascript in PHP and send to client). The most real-time solution is using AJAX but you still suffering round-trip lags depending on client speed.

share|improve this answer
I know that. PHP can be also used as a CLI, so not necessary from a server. And as I said, I'm searching for a solution that shouldn't use a browser in the first place. Some libraries might have been developed to extend PHP's capabilities. –  Cedric Oct 12 '10 at 11:45
The most confusing concepts for who coded standalone applications when they move to coding web-based applications is the "server-client connectionless round trip". How long do you have been working on web-based applications ? –  Xaqron Oct 12 '10 at 12:15
more than a year Xaqron. –  Cedric Oct 12 '10 at 16:05
When client sends a request to the server the response (in your case it's a page, not a file) is just a bunch of TEXTs which should be rendered by some application namely BROWSER. After that the PHP code has nothing to do until a new request is received. This new request is from a new client or from the same client (posting a form, requesting the same page, using AJAX on page ...). So it's not like a standalone application which has a real-time connection with the user. Moving the mouse has meaning when the client see the whole page (after it has been generated and transferred to the client). –  Xaqron Oct 12 '10 at 17:12
php -f foo.php bar foo2 . Check uk.php.net/manual/en/features.commandline.php . In the question "no browser is open" obviously implies it's not a web app. –  Cedric Oct 13 '10 at 9:46
add comment

1 - use exec() and : http://stackoverflow.com/questions/2986286/simulate-mouse-movement-in-ubuntu . Basically, use any other language, compile it if needed, and use the executable with argument throughout command line.

2 - PHP-QT might do the trick


People have suggested to use another language (javascript), but for this problem, it's not possible to use a browser. So other languages will do the trick.

Thanks for your message though, and if anybody have other solutions, I'd be interested to know them.

share|improve this answer
And shame on the guy who pressed the button 'this question is unclear or not useful' for my question :D (sorry for this useless comment -:) ) –  Cedric Oct 12 '10 at 12:48
@Cedric: Since you are using PHP in quite an unusual way, it would be worthwhile pointing that out in big letters in your question. If someone sees PHP in the title of a question, it's quite understandable that they would think that you are writing a web app, making the question seem unclear. –  Douglas Oct 12 '10 at 12:59
@Douglas: Even if I mention "no browser is open ", excluding all browser oriented based application/ pages, I thank you for your comment as I understand why people overlooked "This would be usable, even if no browser is open (so cannot use the classic javascript solution)." –  Cedric Oct 12 '10 at 13:13
What's the point of using PHP if it's just to launch another executable... ? (maybe just not to learn a much more appropriate client language) –  Shikiryu Oct 12 '10 at 13:29
@Cedric: Something like, "I am writing a PHP application which runs on the client (not on a web server). How do I set the location of the user's mouse pointer?" -- though I'd still expect "Why are you using PHP?" questions, so answering that up front would be a good idea. "I'm using PHP so that I can reuse XYZ library..." or something. –  Douglas Oct 12 '10 at 14:56
show 2 more comments

+1 To everything that was said before.

I'll add that more details on the goal is needed. Depending on what you really want (A click to do what ? On what ? etc...), you can still use cURL to reach a page, parsing it and following to the link you want (if that's a link you want to click...), entering a whole form and submiting it, etc... You can access to the html code and save it in a file on your server (if that's what you need.) etc... etc...

Anyway, as everyone said, PHP is server-side and, even as CLI, you need to have a server on your localhost and that will just execute a PHP script, PHP that don't have access to mouse/mouse movement etc without a client-side language like javascript.

share|improve this answer
Maybe I am wrong, but /usr/bin/php doesn't need a server to be run. It's a stand-alone application. php.net/manual/en/install.unix.commandline.php –  Cedric Oct 12 '10 at 12:36
add comment

IMHO I think your going about whatever it is your trying to do in the wrong way. There is no way to control the users mouse unless your using some sort of remote desktop app as that would be a security issue. That said I could take a guess as some possible things you could do

  1. set focus on an object using javascript
  2. click something using javascript 3 write and applescript (if on a mac) to click something in the finder or automate a process


EDIT is should also be noted that if you use applescript stuidio you have access to objective c which would let you write code to change the mouse position. but I don't recommend it the user should control the mouse and nothing else should

share|improve this answer
add comment

It's not so hard. Look example.

You can easily edit it and send AJAX HTTP request for x,y positions and return xstart->x , ystart->y.

Hard part is make object to avoid other objects.

share|improve this answer
add comment

Your Answer


