How to set mouse position

im working on an application with a trackball and i like to roll in every direction without screen limits. is there a way to set mouse coordinates ?


greetings ascorbin


are you looking to do this on a pc, mac or linux?

on a pc, you can use this:



take care!

ah thank you, later i will port this project to pc. actually im working on mac

on mac, one way would be:

CGPoint point;  
point.x = 500;  
point.y = 500;  

have fun!


works! thank you a lot!

Would be cool to wrap this up as an addon, say ofxVirtualMouse.

For windows I found this code:

#include <windows.h>  
void mouseMove(int dx, int dy)  
   INPUT input;  
   input.type = INPUT_MOUSE;  
   input.mi.dx = dx;  
   input.mi.dy = dy;  
   input.mi.mouseData = 0;  
   input.mi.dwFlags = MOUSEEVENTF_MOVE;  
   input.mi.time = 0;  
   input.mi.dwExtraInfo = 0;  
   SendInput(1, input, sizeof(INPUT));  

based on msdn: dx Absolute data is specified as the x coordinate of the mouse; relative data is specified as the number of pixels moved. But indeed, the fine print in the remarks explained the coordinates:

The mouse position is not specified in pixels in this record, it is specified in “mickeys”. That is a coordinate system that treats the screen (regardless of physical size and resolution) as a square of 65536 * 65536 units (0…65535 is the
value range for each axis). The position data is also only used if you specify MOUSEEVENTF_MOVE as part of the flags.

Code for linux using xlib:

void move_mouse_pointer(int delta_x, int delta_y) {  
    Display *display = XOpenDisplay(0);  
    XWarpPointer(display, None, None, 0, 0, 0, 0, delta_x, delta_y);  

i love this community :wink:

button presses in linux:

// g++ test.cpp -L/usr/X11R6/lib -lX11 -lXtst  
int main(int argc, char* argv[])  
Display *display;  
if ((display = XOpenDisplay(NULL)) == NULL) {  
fprintf(stderr, "Cannot open local X-display.\n");  
int ev, er, ma, mi;  
if(!XTestQueryExtension(display, &ev, &er, &ma, &mi))  
fprintf(stderr, "XTest extension not supported on server.\n");  
fprintf(stderr,"XTest for server \"%s\" is version %d.%d\n",  
DisplayString(display), ma, mi);  
XTestFakeButtonEvent(display, 1, True, CurrentTime);  
XTestFakeButtonEvent(display, 1, False, CurrentTime);  
return 0;  

mouse click in windows:
(This does not use SendInput but the older and easier mouse_event functions.)

#include <windows.h>  
#include <iostream.h>  
#include <stdlib.h>  
#include <stdio.h>  
int main(){  
cout<<"hit enter to click button";  
SetCursorPos(404, 459);  
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);  
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);  
return 0;  

button click in OSX:
(Only really the CGPostMouseEvent calls are relevant. CGPostMouseEvent is part of CGRemoteOperation.h)

// File:   
// click.m  
// Compile with:   
// gcc -o click click.m -framework ApplicationServices -framework Foundation  
// Usage:  
// ./click -x pixels -y pixels   
// At the given coordinates it will click and release.  
#import <Foundation/Foundation.h>  
#import <ApplicationServices/ApplicationServices.h>  
int main(int argc, char *argv[]) {  
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
  NSUserDefaults *args = [NSUserDefaults standardUserDefaults];  
  // grabs command line arguments -x and -y  
  int x = [args integerForKey:@"x"];  
  int y = [args integerForKey:@"y"];  
  // The data structure CGPoint represents a point in a two-dimensional  
  // coordinate system.  Here, X and Y distance from upper left, in pixels.  
  CGPoint pt;  
  pt.x = x;  
  pt.y = y;  
  // This is where the magic happens.  See CGRemoteOperation.h for details.  
  // CGPostMouseEvent( CGPoint        mouseCursorPosition,  
  //                   boolean_t      updateMouseCursorPosition,  
  //                   CGButtonCount  buttonCount,  
  //                   boolean_t      mouseButtonDown, ... )  
  // So, we feed coordinates to CGPostMouseEvent, put the mouse there,  
  // then click and release.  
  CGPostMouseEvent( pt, 1, 1, 1 );  
  CGPostMouseEvent( pt, 1, 1, 0 );  
  [pool release];  
  return 0;  

wow, soon we’ll have a java.awt.robot thing happening :slight_smile:

a good example is this one from the OF lab – theo (with a minor bit of keyboard press simulation code from me) did this awesome app:

that let’s OF control another app (NES simulator)

thanks for up-ing all those code sample, this is gonna be a valuable thread

take care!

problem is that CGWarpMouseCursorPosition() freezes mousemovementevents for a second or so. solution is setting CGSetLocalEventsSuppressionInterval to zero.

like this:

CGPoint point;
point.x = ofGetWidth() / 2;
point.y = ofGetHeight() / 2;

this is for mac only!

1 Like

I started creating an addon for this a while a go. It’s not finished yet but may be it’s useful to someone.…

[quote author=“companje”]I started creating an addon for this a while a go. It’s not finished yet but may be it’s useful to someone.…[/quote]

hi man. don’t know if you have any latest news about this addon. but i’d like to say, it works! Thanks man. With this i can control my PC in lots of funny ways!
thanks again!

The “ofxMouse” addon seems pretty nice. But link is broke now. Any one have it to post it?

Here it is:

But it’s quite old, and would need to be updated to OF007, not a huge deal. There’s also this one:

I don’t think either of these allow you to set the users mouse position though. To do that in OF you’d want to use the GLUT function to change mouse position:

glutWarpPointer(windowWidth / 2, windowHeight / 2); // set users mouse positioin to the center  

wow, important note: don’t use glutWarpPointer unconditionally in the main loop. it will hijack your cursor and keyboard and you’ll have to restart.

also, because OF does not include glut by default you have to add something like this on OSX:

#include "../../../libs/glut/lib/osx/GLUT.framework/Versions/A/Headers/glut.h"  

before you can use glutWarpPointer

Wow indeed! That’s not the friendliest behavior. Thanks for testing that out!

Hi All,

“mouse_event” works well to simulate virtual mouse press in XP, but I am having problems in Windows7. This sounds normal because I found some explanation about it:

The ‘mouse_event()’ function has been superseded by ‘SendInput()’ on Window NT/2000/XP. Thus, on these operating systems you should use ‘SendInput()’ (unless you need to provide backward compatibility with Windows 98 etc.). This FAQ is based on ‘SendInput()’.

Anyone have experience to made compile a simple SendInput() code like this into OF? I add link to window SDK v7.0A\Include and User32.lib . But there are still errors.

// add this in testApp  
#define _WIN32_WINNT 0x0400  
#include <windows.h>  
#include <winuser.h>  
void LeftClick ( )  
  INPUT    Input={0};  
  // left down   
  Input.type      = INPUT_MOUSE;  
  Input.mi.dwFlags  = MOUSEEVENTF_LEFTDOWN;  
  // left up  
  Input.type      = INPUT_MOUSE;  
  Input.mi.dwFlags  = MOUSEEVENTF_LEFTUP;