Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
windows mouse movement

Subject: windows mouse movement

From: Matthias Gschwendner

Date: 13 Mar, 2009 14:45:04

Message: 1 of 6

Hello!

I have a question about matlab / simulink and the windows mouse movement. Are there any functions or commands to move the windows mouse in realtime by using matlab and simulink.

We have EEG-Signals where there person was shown an arrow left or right ( 40 times ). Then I filtered the signals in alpha and beta waves. After that I made a LDA and decide if the person thought a right or left movement. According to that decision I want to move the windows mouse to the left or to the right. Are there any functions included into matlab or simulink to move the windows mouse to the left or to the right?

thanks for your answers!

Subject: windows mouse movement

From: Phil Goddard

Date: 13 Mar, 2009 18:48:01

Message: 2 of 6

"Matthias Gschwendner" <m.gschwendner@freenet.de> wrote in message <gpdrhf$f6t$1@fred.mathworks.com>...
> Hello!
>
> I have a question about matlab / simulink and the windows mouse movement. Are there any functions or commands to move the windows mouse in realtime by using matlab and simulink.
>
> We have EEG-Signals where there person was shown an arrow left or right ( 40 times ). Then I filtered the signals in alpha and beta waves. After that I made a LDA and decide if the person thought a right or left movement. According to that decision I want to move the windows mouse to the left or to the right. Are there any functions included into matlab or simulink to move the windows mouse to the left or to the right?
>
> thanks for your answers!

You can move the cursor by calling the appropriate windows API.

From MATLAB this can be done either as a c-mex file or using the generic dll functionality.
From Simulink you could either write a c-mex S-Function (arguably the prefered route) or by calling into MATLAB.

For the generic dll approach, put the following code into a file

%%%%%%%%%%%% Cut Here %%%%%%%%%%%%%%%%%%
function [methodinfo,structs,enuminfo,ThunkLibName]=customUser32Interface
% Function to use in place of a header file when doing
% >> loadlibrary('user32',@customUser32Interface);
% and only wanting access to the SetCursorPos function

% Generic stuff part one
ival={cell(1,0)}; % change 0 to the actual number of functions to preallocate the data.
structs=[];enuminfo=[];fcnNum=1;
fcns=struct('name',ival,'calltype',ival,'LHS',ival,'RHS',ival,'alias',ival);
ThunkLibName=[];

% Specific function definitions

% BOOL SetCursorPos (int, int);
fcns.name{fcnNum}='SetCursorPos'; fcns.calltype{fcnNum}='stdcall'; fcns.LHS{fcnNum}='int32'; fcns.RHS{fcnNum}={'int', 'int'};fcnNum=fcnNum+1;

% Generic stuff part two
structs.c_struct.members=struct('p1', 'double', 'p2', 'int16', 'p3', 'long');
enuminfo.Enum1=struct('en1',1,'en2',2,'en4',4);
methodinfo=fcns;
%%%%%%%%%%%% Cut Here %%%%%%%%%%%%%%%%%%

Then do
>> loadlibrary('user32',@customUser32Interface);
>> calllib('user32','SetCursorPos',120,120);

where the final 2 inputs to calllib specify the location where you'd like the cursor.

Phil.

Subject: windows mouse movement

From: Zhelyazko

Date: 14 Mar, 2009 00:10:19

Message: 3 of 6

The easiest solution is to use the Java Robot
So in matlab you can create the robot.
jRobot=java.awt.Robot;
x=100;
y=200;
jRobot.mouseMove(x,y);

Moves the mouse to position on the Screen the 0 0 is at the top of the screen and in Matlab it is at the bottom.

Subject: windows mouse movement

From: Yair Altman

Date: 14 Mar, 2009 17:54:02

Message: 4 of 6

The Windows DLL and Java solutions posted above are nice, but there is also an internal Matlab solution, using set(0,'PointerLocation',[newX,newY]).

Matlab also has an internal convenience function to move the mouse pointer relative to an axes, which could be helpful in your case - the moveptr function. moveptr is unsupported and only semi-documented (it has a help section but not a dedicated doc section, so "doc moveptr" simply displays the help section), but it works as long as your axes is a direct child of the figure. It accepts a handle of your axes (note: handle(hAxes), not the numeric hAxes), and a simple use is as follows:

moveptr(handle(gca), 'init');
moveptr(handle(gca), 'move',-5,7); % so easy

Yair Altman
http://Undocumented-Matlab.com

Subject: windows mouse movement

From: us

Date: 14 Mar, 2009 18:18:01

Message: 5 of 6

"Yair Altman"
> Matlab also has an internal convenience function to move the mouse pointer relative to an axes, which could be helpful in your case - the moveptr function. moveptr is unsupported and only semi-documented (it has a help section but not a dedicated doc section, so "doc moveptr" simply displays the help section...

it is a simple M-file living here

MLROOT\toolbox\shared\controllib\moveptr.m

% also,
type moveptr
% shows its engine...

us

Subject: windows mouse movement

From: Yair Altman

Date: 14 Mar, 2009 18:55:04

Message: 6 of 6

"us " <us@neurol.unizh.ch> wrote in message <gpgscp$sg3$1@fred.mathworks.com>...
> "Yair Altman"
> > Matlab also has an internal convenience function to move the mouse pointer relative to an axes, which could be helpful in your case - the moveptr function. moveptr is unsupported and only semi-documented (it has a help section but not a dedicated doc section, so "doc moveptr" simply displays the help section...
>
> it is a simple M-file living here
>
> MLROOT\toolbox\shared\controllib\moveptr.m
>
> % also,
> type moveptr
> % shows its engine...
>
> us

Yes, I believe it existed in this format, bugs/limitations and all, all the way back to Matlab 7. The fact that it's an m-file without the usual 'unsupported' warning does not mean that it is in fact supported. I discovered this when I tried to send TMW a report of the bugs and suggested solutions, 2 years ago. Here is the response I got:

"I am writing in reference to your Service Request # 1-4BBNBC regarding 'Bugs in moveptr function'. MOVEPTR function is an internal, undocumented function. If this function causes another, documented MATLAB function to break (or give results that are different then explained in the documentation for that function), please send me the exact reproduction steps. If you need further assistance regarding this issue, please reply to this email..."

Unfortunately, after so many years, TMW have still not seen fit to make this a fully supported function, and only fixed one of its bugs/limitations since then (in R2008a). The fixes I suggested remain in their inbox... life moves on... :-)

Yair Altman

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us