Code covered by the BSD License  

Highlights from
jMouseEmu: Mouse Emulator (v2.3)

4.0 | 1 rating Rate this file 22 Downloads (last 30 days) File Size: 5.52 KB File ID: #28357 Version: 1.8

jMouseEmu: Mouse Emulator (v2.3)


Kesh Ikuma (view profile)


02 Aug 2010 (Updated )

A Java-based function to control mouse programmatically

| Watch this File

File Information

JMOOUSEEMU emulates the mouse cursor positioning and clicking. It supports either single- or multiple-command modes.

UPDATE (8/31/2010):
INPUTEMU utility (linked below) supersedes this function (without HG handle support). If your program does not use the HG handle (i.e., H=0), consider using INPUTEMU instead.

The function utilizes the java.Robot class to control mouse.

Single-Command Mode:

JMOUSEEMU(H,POS,CLICK) mouse cursor to POS = [X,Y] (in pixels) with respect to (the lower left hand corner of) the graphics object H. Mouse click option is one of 'none' | 'normal' | 'extend' | 'alternate' | 'open', according to Figure's SelectionType property. If only mouse click without cursor movement is desired, set H = 0 and leave POS empty.

The arguments H and CLICK may be omitted. See the help text for more details.

Example. Martin Weber's setfocus(H) function can be implemented using this function:

   mpos = get(0,'PointerLocation');

Multiple-Command Mode (new in v.2.0):

JMOUSEEMU(CMDS,T) performs the sequence of mouse commands using a MATLAB timer with interval T seconds. CMDS is an N-by-3 cell array specifying N mouse commands. The n-th row consists of {H_n, POS_n, CLICK_n} to define the mouse command at nT-th second. Alternatively, each interval between successive commands can be specified with size-N vector T.

In addition to the CLICK options for Single-Command Mode, two additional CLICK options are introduced to support mouse dragging: 'drag_on' and 'drag_off'. Also, there are 2 not-so-CLICK options: 'delay' and 'wheel'. The 'delay' option causes a momentary pause (duration in POS in seconds) and the 'wheel' option turns the mouse wheel (notches to turn in POS: positive-towards, negative-away).

If jMouseEmu is used to trigger multiple mouse callbacks within MATLAB, call jMouseEmu for each callback trigger. Moreover, set the last jMouseEmu command to trigger the callback. Not following this may cause undesirable behavior (i.e., delayed callback execution and erroneous figure's SelectionType property value during the callback).

Example. Circular mouse movement:

   Ts = 0.01; % update interval
   T = 2; % emulation duration

   % plot a circle
   t = (0:Ts:T)';
   mpos = [100*sin(2*pi*t/T)+pos(3)/2 100*cos(2*pi*t/T)+pos(4)/2];

   % show button down activity

   % adjust axis so its data = pixels
   pos = get(gcf,'Position');
   set(gca,'Units','pixels','Position',[0 0 pos([3 4])],'XLim',[0 pos(3)],'YLim',[0 pos(4)]);

   % generate commands to follow the cirle & make it to drag
   N = size(mpos,1);
   cmds = [cell(N,1) mat2cell(mpos,ones(N,1),2) cell(N,1)];
   cmds{1,3} = 'drag_on';
   cmds{N,3} = 'drag_off';

   % emulate!


Setfocus, Screen Capture Get A Screen Capture Of A Figure Frame Or Component, and Input Emu: Keyboard & Mouse Emulator (V1.0.1) inspired this file.

This file inspired Heightprofile From Google Earth To 3 D Surface and Input Emu: Keyboard & Mouse Emulator (V1.0.1).

MATLAB release MATLAB 7.10 (R2010a)
Other requirements Java.awt support
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (9)
22 Feb 2015 Matt H

Matt H (view profile)

I am also getting an error in R2014b now that figure handles are objects. I am not sure of any easy way to make it work since my figure does not have an integer handle value assigned under the 'Number' figure property.

Comment only
20 Nov 2014 Song

Song (view profile)

the function doesnt support 2014b since the object handle is no more returned

Comment only
05 Aug 2013 Kesh Ikuma

Kesh Ikuma (view profile)

@p kung - Thanks for reporting the bug. I've uploaded the fixed version. FYI, the fix is simply to change "cmds(1)" to cmds(2)" on Line 242.

Comment only
01 Aug 2013 p kung

p kung (view profile)

exactly what i was looking for (to emulate mouse movement)

but i got an error for


POS = [X,Y]

whether there is a figure or no figure.

if seems i have to use

jmouseemu(fig, POS)

fig => figure handle

06 May 2012 Kesh Ikuma

Kesh Ikuma (view profile)

@kalkin - In order to help you, I need to know what exactly cmds and Ts contain at the time of jmouseemu call.

Comment only
06 May 2012 kalkin powale

first of all your button clicking code is very use full to me as i was searching a lot. for my B.TECH project.
but as i am fresher here i don't know much about MATLAB. so i just copy above example (untitled.m) then copy jmouseemu(jmouseemu.m)also fuction parseInput (parseInput.m). and executed untitled.m

but i get error same ("asespecially line 122 column 5.") and
??? Attempted to access pos0(201,:); index out of bounds
because size(pos0)=[1,2].

Error in ==> jmouseemu at 152
ref = pos0(I(n),:);

Error in ==> Untitled4 at 23
>> "

please help me out if i am wrong some objective is just to click Left and Right click with regular interval of time....

Comment only
20 Sep 2010 Y Mehta

inputemu('alternate',[1000 1000]);

??? Undefined variable "KeyEvent" or class "KeyEvent.VK_CONTROL".

Error in ==> inputemu>convertToLowLevelActions at 347
params{I} = KeyEvent.VK_CONTROL;

Error in ==> inputemu at 119
[actions,params,dt,msg] = convertToLowLevelActions(cmds,deltaT,idx_mouse,oemChars);


I get similar error with extend option. Open and normal options work fine.

Comment only
05 Sep 2010 Kesh Ikuma

Kesh Ikuma (view profile)

@Coney - Not sure what your exact syntax is, but either

jmouseemu(0,[],'normal'); % or

should do what you want it to do. FYI, the second input parameter is the POS argument.

If you need further assistance, please post (or email me) your code. I'm curious to know how the error you described occurred.

Comment only
05 Sep 2010 Coney Woodcock

I am trying to use this code to just click without interfering with the mouse position (to measure speed of mouse movement). Is it possible with this code?
What do you mean by leave POS as empty? I have tried only entering 2 inputs, h = 0, and click = 'normal', as well as heaps of combinations similar, and it gives me errors, especially line 122 column 5.

Comment only
12 Aug 2010 1.1

v.2.0 - added multiple-command mode - added more click options

14 Aug 2010 1.2

* Fixed figure auto-focus bug
* Allowing T = 0
* Allowing no input argument

28 Aug 2010 1.3

* Using robot.delay() to queue interval-command instead of MATLAB timer
* Added 'delay' & 'wheel' CLICK options
* Added option to specify individual interval durations

31 Aug 2010 1.5

Info update

06 Aug 2013 1.8

Bug fix

Contact us