<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Kesh Ikuma &#8211; Undocumented Matlab</title>
	<atom:link href="https://undocumentedmatlab.com/articles/tag/kesh-ikuma/feed" rel="self" type="application/rss+xml" />
	<link>https://undocumentedmatlab.com</link>
	<description>Professional Matlab consulting, development and training</description>
	<lastBuildDate>Wed, 15 Aug 2012 16:54:11 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.2</generator>
	<item>
		<title>ScreenCapture utility</title>
		<link>https://undocumentedmatlab.com/articles/screencapture-utility?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=screencapture-utility</link>
					<comments>https://undocumentedmatlab.com/articles/screencapture-utility#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 15 Aug 2012 16:54:11 +0000</pubDate>
				<category><![CDATA[GUI]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Low risk of breaking in future versions]]></category>
		<category><![CDATA[Kesh Ikuma]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Toolbar]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=3059</guid>

					<description><![CDATA[<p>The ScreenCapture utility uses purely-documented Matlab for capturing a screen region as an image from within Matlab. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/screencapture-utility">ScreenCapture utility</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/interesting-uitree-utility" rel="bookmark" title="An interesting uitree utility">An interesting uitree utility </a> <small>ExploreStruct is a utility that shows how custom uitrees can be integrated in Matlab GUI...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/savezip-utility" rel="bookmark" title="savezip utility">savezip utility </a> <small>Matlab data can be serialized and saved into a ZIP/GZIP file, and loaded back. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/plotedit-context-menu-customization" rel="bookmark" title="PlotEdit context-menu customization">PlotEdit context-menu customization </a> <small>A combination of Matlab and Java Robot commands to automate a certain animation can be used when we cannot access underlying GUI/graphics code. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/frameless-undecorated-figure-windows" rel="bookmark" title="Frameless (undecorated) figure windows">Frameless (undecorated) figure windows </a> <small>Matlab figure windows can be made undecorated (borderless, title-less). ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>A few days ago, my <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/24323-ScreenCapture">ScreenCapture utility</a> was <a target="_blank" rel="nofollow" href="http://blogs.mathworks.com/pick/2012/08/10/grab-a-screen-shot-from-within-matlab/">selected</a> as Matlab&#8217;s Pick of the Week (POTW). POTW selections are normally extremely useful, well-written and instructive utilities, which are both great to use in their own right, as well as a great source of knowledge about Matlab programming features and good practices. I follow the weekly POTW selections closely, and often learn new stuff from these utilities. I take pride in the fact that some of my utilities have been selected for inclusion in this unique set.<br />
ScreenCapture enables Matlab users to take automated (programmatic) as well as interactive screen-captures of any Matlab GUI component or sub-region. This includes figure windows, axes, images, controls and even the Matlab Desktop. If the target handle for the capture is not specified, then ScreenCapture prompts the user to interactively select the capture region using an <i><b>rbbox</b></i> limiting box. ScreenCapture also includes a feature that plants a camera icon in the figure toolbar, such that clicking this icon will immediately trigger the interactive region-selection screen-capture.<br />
Whichever manner the capture was made, the user then has the option of sending the output to Matlab (as a 3D RGB image matrix), or to one of the standard image file formats (e.g., JPG or PNG).<br />
ScreenCapture has extensive help and is well-documented and relatively easy to use. For example:</p>
<pre lang='matlab'>figure; surf(peaks); imgData=screencapture(gcf); imshow(imgData);</pre>
<p><center><figure style="width: 450px" class="wp-caption aligncenter"><img fetchpriority="high" decoding="async" alt="ScreenCapture in action" src="https://undocumentedmatlab.com/images/ScreenCapture.png" title="ScreenCapture in action" width="450" height="480" /><figcaption class="wp-caption-text">ScreenCapture in action</figcaption></figure></center><br />
<span id="more-3059"></span><br />
Some additional usage examples:</p>
<pre lang='matlab'>
imageData = screencapture;                     % interactively select screen-capture rectangle
imageData = screencapture(hListbox);           % capture image of a uicontrol
imageData = screencapture(0,  [20,30,40,50]);  % capture a small desktop sub-region
imageData = screencapture(gcf,[20,30,40,50]);  % capture a small figure sub-region
% capture a small sub-region of an axes
imageData = screencapture(gca,[10,20,30,40]);
imshow(imageData);  % display the captured image in a matlab figure
imwrite(imageData,'myImage.png');  % save the captured image to file
% capture a sub-region of an image
img = imread('cameraman.tif');
hImg = imshow(img);
screencapture(hImg,[60,35,140,80]);  % in data units, not pixel units
screencapture(gcf,[],'myFigure.jpg');                   % capture the entire figure into file
screencapture('handle',gcf,'filename','myFigure.jpg');  % same as previous
screencapture('toolbar',gcf);                           % adds a screen-capture button to gcf's toolbar
screencapture('toolbar',[],'file','sc.bmp');            % same, using a default output filename
</pre>
<h3 id="documented">Purely documented</h3>
<p>Over the course of the past few years I have submitted <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/index?term=authorid%3A27420">40 utilities</a> to the Matlab File Exchange, several of which have been selected for POTW. Unfortunately, since most of my utilities employ undocumented Matlab features to some extent (I can&#8217;t help myself&#8230;), they are ineligible for being selected as POTF, useful and deserving as they may be. In fact, my <a target="_blank" href="/articles/cprintf-display-formatted-color-text-in-command-window/">cprintf utility</a>, which was selected as POTW, was quickly deselected as POTW because of this very issue. MathWorks fears (and I can certainly understand the concern) that highlighting a utility that relies on some undocumented feature as POTW might be considered as an official endorsement of these features.<br />
ScreenCapture is different in this regard: it uses purely documented Matlab functionality to achieve its aims, and apparently still succeeds in providing useful functionality. This does not mean that ScreenCapture uses pure Matlab. In fact, it relies on the <a target="_blank" href="http://download.oracle.com/javase/6/docs/api/java/awt/Robot.html">Java <code>Robot</code> class</a>&#8216;s functionality of taking a screen-capture of a specified area of the screen. Using the Java <code>Robot</code> class in such a way is an entirely <a target="_blank" rel="nofollow" href="http://www.mathworks.com/help/techdoc/matlab_external/f44062.html">documented</a> Matlab feature. I have discussed the Java <code>Robot</code> in two past articles on this blog, where guest blogger Kesh Ikuma explained (<a target="_blank" href="/articles/gui-automation-robot/">here</a> and <a target="_blank" href="/articles/gui-automation-utilities/">here</a>) how it can be used to simulate mouse and keyboard actions programmatically.</p>
<h3 id="technical">Under the hood</h3>
<p>ScreenCapture calculates the requested screen-capture rectangle coordinates and then invokes the Java <code>Robot</code> to take the actual bitmap screen-capture. The output is then converted into a Matlab image matrix for output, or stored in an image file, based on the user&#8217;s choice of parameters.<br />
Some difficulties that I overcame when programming ScreenCapture:</p>
<ul>
<li>The screen position of docked windows cannot be computed reliably. Docked windows therefore need to be automatically temporarily undocked for screen-capture.</li>
<li>Undocking in Windows 7 with Aero transparency features causes the <code>Robot</code> to take its screen-shot before the window becomes fully opaque. Adding a short delay in undocking solved this issue.</li>
<li>Images use reversed Y-axis (Y=0 is at the axes top, not bottom). Also, when specifying a sub-region for capture, many users are used to handling images using data units (e.g., for <i><b>imcrop</b></i>) rather than ScreenCapture&#8217;s standard pixel units. Taking screen-captures of images proved to be a non-trivial challenge indeed.</li>
<li>Different Matlab objects (controls, axes, figures) have different external borders and internal margins. I had to take these into account in order to achieve tight-fitting image captures of these objects.</li>
<li>Performance was a problem, and it turned out that the bottleneck was trying to convert from the Java image data format to Matlab&#8217;s image data format. A couple of suggestions by Jan Simon and Urs (us) Schwartz significantly improved this performance hotspot. I&#8217;ve submitted the relevant code snippets to MathWorks for incorporation in a published <a target="_blank" rel="nofollow" href="http://www.mathworks.com/support/solutions/en/data/1-2WPAYR/">technical solution</a>, and I was happy to see that they have indeed incorporated them into that solution.</li>
<li>Finally, I thought that adding a custom toolbar image would be a nice touch. Since I&#8217;m not much of an artist, creating the camera icon programmatically proved to be a bit of a challenge&#8230;</li>
</ul>
<p>Please feel free to download ScreenCapture&#8217;s code and check how I chose to program around these issues.</p>
<h3 id="TODO">TODO list</h3>
<p>Some potentially-useful features have so far eluded me in ScreenCapture&#8217;s implementation. Perhaps one day I will find a way to do them:</p>
<ol>
<li>Enable output of the image data, as an image object, to the system clipboard. It is easy to serialize the data and store it as a string in the clipboard, using the built-in <i><b>clipboard</b></i> function. But we would not be able to paste this data as an image into an external editor or image-processing utility. I have not yet found an easy way to store the image data as an object, although it should not be very difficult to do (here&#8217;s a <a target="_blank" rel="nofollow" href="http://www.exampledepot.com/egs/java.awt.datatransfer/ToClipImg.html">starter</a>).</li>
<li>When interactively selecting a screen-capture region, <i><b>rbbox</b></i>&#8216;s starting point needs to be somewhere within the boundaries of a Matlab figure. The box can extend beyond the figure&#8217;s borders, but it has to start somewhere within the figure. I would like to be able to use <i><b>rbbox</b></i> without this limitation.</li>
</ol>
<p><b><u>Addendum Jan 28. 2013</u></b>: A new version of ScreenCapture was uploaded to the <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/24323-ScreenCapture">File Exchange</a> today which appears to solve both of the TODO issues above: The copy-to-clipboard feature relies on Jiro Doke&#8217;s <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/28708-imclipboard">imclipboard utility</a> as mentioned by Matt <a target="_blank" href="/articles/screencapture-utility/#comment-106188">below</a> &#8212; simply specify the &#8216;clipboard&#8217; string as the capture target (rather than a filename); the solution of the <i><b>rbbox</b></i>-anywhere feature relies on using a temporary <a target="_blank" href="/articles/transparent-matlab-figure-window/">transparent window</a> that <a target="_blank" href="/articles/minimize-maximize-figure-window/">spans the entire desktop area</a>, capturing the user&#8217;s <i><b>rbbox</b></i> clicks anywhere within the desktop area. Interested readers can easily adapt the code to fit multiple monitors (I didn&#8217;t bother).</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/screencapture-utility">ScreenCapture utility</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/interesting-uitree-utility" rel="bookmark" title="An interesting uitree utility">An interesting uitree utility </a> <small>ExploreStruct is a utility that shows how custom uitrees can be integrated in Matlab GUI...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/savezip-utility" rel="bookmark" title="savezip utility">savezip utility </a> <small>Matlab data can be serialized and saved into a ZIP/GZIP file, and loaded back. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/plotedit-context-menu-customization" rel="bookmark" title="PlotEdit context-menu customization">PlotEdit context-menu customization </a> <small>A combination of Matlab and Java Robot commands to automate a certain animation can be used when we cannot access underlying GUI/graphics code. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/frameless-undecorated-figure-windows" rel="bookmark" title="Frameless (undecorated) figure windows">Frameless (undecorated) figure windows </a> <small>Matlab figure windows can be made undecorated (borderless, title-less). ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/screencapture-utility/feed</wfw:commentRss>
			<slash:comments>35</slash:comments>
		
		
			</item>
		<item>
		<title>GUI automation utilities</title>
		<link>https://undocumentedmatlab.com/articles/gui-automation-utilities?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=gui-automation-utilities</link>
					<comments>https://undocumentedmatlab.com/articles/gui-automation-utilities#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 22 Sep 2010 18:00:28 +0000</pubDate>
				<category><![CDATA[Figure window]]></category>
		<category><![CDATA[Guest bloggers]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Low risk of breaking in future versions]]></category>
		<category><![CDATA[Kesh Ikuma]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=1877</guid>

					<description><![CDATA[<p>This article explains a couple of Matlab utilities that use Java's Robot class to programmatically control mouse and keyboard actions</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/gui-automation-utilities">GUI automation utilities</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/gui-automation-robot" rel="bookmark" title="GUI automation using a Robot">GUI automation using a Robot </a> <small>This article explains how Java's Robot class can be used to programmatically control mouse and keyboard actions...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/plotedit-context-menu-customization" rel="bookmark" title="PlotEdit context-menu customization">PlotEdit context-menu customization </a> <small>A combination of Matlab and Java Robot commands to automate a certain animation can be used when we cannot access underlying GUI/graphics code. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/figure-keypress-modifiers" rel="bookmark" title="Figure keypress modifiers">Figure keypress modifiers </a> <small>The figure's CurrentModifier property provides a simple and consistent way to retrieve keypress modifiers: alt-, control- and shift-clicks. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/uicontrol-callbacks" rel="bookmark" title="Uicontrol callbacks">Uicontrol callbacks </a> <small>This post details undocumented callbacks exposed by the underlying Java object of Matlab uicontrols, that can be used to modify the control's behavior in a multitude of different events...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p><em>Once again, I welcome guest writer <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/authors/36665">Takeshi (Kesh) Ikuma</a>. Last week, Kesh described how <a target="_blank" href="/articles/gui-automation-robot/">Java&#8217;s Robot class can be used to automate Matlab GUI</a>. Today, Kesh will describe two Matlab utilities that help this automation process.</em><br />
<center><img decoding="async" alt="" src="https://undocumentedmatlab.com/images/robot.gif" title="" width="96" height="156" /></center><br />
jMouseEmu and inputEmu are both available on the Matlab File Exchange. There is a large overlap between these utilities. As their name suggests, jMouseEmu is geared to control the mouse, while inputEmu emulates both mouse and keyboard inputs.</p>
<h3 id="jMouseEmu">jMouseEmu</h3>
<p>The <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/28357-jMouseEmu">jMouseEmu utility</a> is an interface function to simplify the programmatic execution of mouse actions (movements, clicks and wheel scrolls). jmouseemu features include accepting relative position with regards to a specified Handle Graphics control, supporting the Shift and Control click modifier keys, supporting double clicking and mouse dragging, controlling mouse wheel turns and allowing multiple command execution with interval timing specification.<br />
jmouseemu has two operating modes: single- or multi-command. In this respect, a single jmouseemu command consists of a cursor position and a click option &#8211; multiple such commands can be specified to jmouseemu in the multi-command mode.<br />
In the single-command mode, the function takes the cursor position information and click options. The click options can be any of the possible <strong>SelectionType</strong> property strings, or &#8220;none&#8221; if no clicking is desired. For example,</p>
<pre lang="matlab">jmouseemu([500,250],'normal');</pre>
<p>places the mouse cursor at the coordinate (500, 250) with respect to the current figure (in Matlab coordinates) and then clicks the left mouse button. If no figure is currently open, the cursor is placed at (500,250) with respect to the bottom-left corner of the screen.<br />
The reference graphics handle can be added as the first argument. For example, if hEdit is a <i><b>uicontrol</b></i> editbox handle, then</p>
<pre lang="matlab">
jmouseemu(hEdit,[],'open');
jmouseemu(hEdit,[],'normal');
</pre>
<p>provides a simple way to select the entire text in the edit box: the double-click <i><b>jmouseemu</b></i> call selects the first word, then the following single-click <i><b>jmouseemu</b></i> extends the selection to the entire text. If the position argument omitted, the cursor is placed on the upper-left hand corner of the object, unless hEdit=0, in which case the cursor stays at the current position.<br />
To turn the mouse wheel 600 notches down (towards you), run:</p>
<pre lang="matlab">jmouseemu(-600,'wheel');</pre>
<p>Multi-command mode extends the single-command mode by accepting a cell array of commands along with the update interval T in seconds. The command array must have three columns, and its k-th row specifies the mouse command at time (k–1)T second.<br />
Using the multi-command mode, the above example can be executed with a single <i><b>jmouseemu</b></i> call, placing the commands in a cell array:</p>
<pre lang="matlab">jmouseemu({h,[],'open'; [],[],'normal'},0);  % T=0</pre>
<p>Here, if you omit handles (i.e., the first column of command array) for the second or later commands, it defaults to the handle of the previous command.<br />
Multi-command mode enables mouse cursor to follow a predefined path. For example, to make the mouse cursor travel in a full circle:</p>
<pre lang="matlab">
T = 0.01; % update every 10 milliseconds
x = pi * (0:T:2)';
pos = 100*[cos(x),sin(x)] + 250;
N = numel(x);
cmds = cell(N,3); % empty click option -> no click
cmds{1,1} = gcf; % position w.r.t. current figure
cmds(:,2) = mat2cell(pos,ones(N,1),2);
jmouseemu(cmds,T);
</pre>
<p>The command interval is realized by calling robot.<em>delay</em>(T*1e3) between commands. This example updates the mouse position every 10 msecs and completes the circle in 2 seconds.<br />
In multi-command mode, there are two additional click options, &#8220;drag_on&#8221; and &#8220;drag_off&#8221;, available to enable mouse dragging. Dragging must be initiated and terminated within a single <i><b>jmouseemu</b></i> call, and &#8220;drag_off&#8221; and &#8220;none&#8221; are the only two acceptable click options during dragging.<br />
Lastly, to pause mouse movement for a moment (longer than the update interval) , a &#8220;delay&#8221; command may be used:</p>
<pre lang="matlab">jmouseemu({...; [],1.0,'delay'; ...}, T);  % pause for 1 second</pre>
<h3 id="warning">Using jMouseEmu to trigger multiple Matlab HG callbacks</h3>
<p><i><b>jmouseemu</b></i> (or more broadly the Matlab handling of Java Robot commands) has a major drawback: If <i><b>jmouseemu</b></i> is used to trigger a Matlab GUI callback during a sequence of mouse maneuver, the callback will not be executed until the entire maneuver is completed. Moreover, since <b>SelectionType</b> property is not logged for every callback event, the incorrect <b>SelectionType</b> property may be reported during callback execution.<br />
For example, add a double click at the beginning of the circular movement:</p>
<pre lang="matlab">
cmds{:,3} = 'open';
jmouseemu(cmds,T);
</pre>
<p>Running this code, you can observe the figure&#8217;s <b>WindowsButtonDownFcn</b> callback, which has been set to echo <b>SelectionType</b> property from earlier, does not kick in until the circle is completed. Now, introduce &#8220;extend&#8221; click at the end of the circle:</p>
<pre lang="matlab">
cmds{end,3} = 'extend';
jmouseemu(cmds,T);
</pre>
<p>Again, no callback displaying <b>SelectionType</b> echo occurs during the mouse movement. When the callbacks are finally executed, they return in the order – &#8220;normal&#8221;, &#8220;extend&#8221;, &#8220;open&#8221; – instead of expected &#8220;normal&#8221;, &#8220;open&#8221;, &#8220;extend&#8221;. To correct this behavior, <i><b>jmouseemu</b></i> must be called twice:</p>
<pre lang="matlab">
jmouseemu(cmds(1,:),T);
jmouseemu(cmds(2:end,:),T);
</pre>
<p>The key here is for each <i><b>jmouseemu</b></i> call to have only one callback triggering mouse click command and to place it at the end of the command sequence.</p>
<h3 id="inputEmu">inputEmu</h3>
<p>The <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/28603-inputEmu">inputEmu utility</a> is similar to jMouseEmu, with extended support for keyboard inputs. The support for relative mouse cursor position with respect to an HG object, however, has been turned off in the current version of inputEmu (it may get turned on in a future version). Position information in inputEmu is always given in pixels with respect to the lower-left corner of the main display screen. Also, the user must note that the input arguments are flipped between jMouseEmu and inputEmu.<br />
In addition to the left mouse click commands from jMouseEmu, inputEmu can emulate the right and middle mouse button clicks. For example, to perform the right click (e.g., to open a context menu):</p>
<pre lang="matlab">inputemu({'right_down' []; 'right_up' []}'); % execute right button click</pre>
<p>inputEmu&#8217;s main feature is keyboard support. There are 4 basic commands (the behavior may be somewhat different on non-windows platforms):</p>
<ul>
<li>key_normal &#8211; Normal key press</li>
<li>key_ctrl	&#8211; CONTROL + key press</li>
<li>key_alt &#8211; ALT + key press</li>
<li>key_win &#8211; WIN + key press</li>
</ul>
<p>One other obvious key press, SHIFT + key press, is conspicuously missing from this list. This is because the SHIFT key is automatically pressed when a SHIFT modified character (e.g., an upper-case alphabet) is entered. For example, executing</p>
<pre lang="matlab">inputemu('key_normal','Hello World'); % emulates typing "Hello World"</pre>
<p>directly in Matlab prompt displays &#8220;Hello World&#8221; in the next command line. The text is first decoded to a sequence of key press and release pairs, including SHIFT key presses and releases as needed, then the sequence of key commands are sent to the java Robot object.<br />
In addition to all the characters on the keyboard, non-character keys can be stroked by using escape characters:</p>
<ul>
<li>&#8216;\SHIFT&#8217;</li>
<li>&#8216;\CTRL&#8217;</li>
<li>&#8216;\ALT&#8217;</li>
<li>&#8216;\WINDOWS&#8217;</li>
<li>&#8216;\BACKSPACE&#8217;</li>
<li>&#8216;\TAB&#8217;</li>
<li>&#8216;\ENTER&#8217;</li>
<li>&#8216;\ESC&#8217;</li>
<li>&#8216;\PAGEUP&#8217;</li>
<li>&#8216;\PAGEDOWN&#8217;</li>
<li>&#8216;\END&#8217;</li>
<li>&#8216;\HOME&#8217;</li>
<li>&#8216;\LEFT&#8217;</li>
<li>&#8216;\UP&#8217;</li>
<li>&#8216;\RIGHT&#8217;</li>
<li>&#8216;\DOWN&#8217;</li>
<li>&#8216;\INSERT&#8217;</li>
<li>&#8216;\DELETE&#8217;</li>
<li>&#8216;\CAPSLOCK&#8217;</li>
<li>&#8216;\NUMLOCK&#8217;</li>
<li>&#8216;\SCROLLLOCK&#8217;</li>
<li>&#8216;\PRINTSCREEN&#8217;</li>
<li>&#8216;\PA&#8211; USE&#8217;</li>
</ul>
<p>Also, use &#8216;\\&#8217; for the forward slash character and &#8216;\F1&#8217; – &#8216;\F12&#8217; for the Function keys. For example, to execute a Matlab &#8220;<i><b>ver</b></i>&#8221; command automatically via command window, run</p>
<pre lang="matlab">inputemu('key_normal','ver\ENTER'); % runs the ver command</pre>
<p>Lastly, as mentioned in the previous article, java.awt.Robot is sensitive to the keyboard locale, and the inputEmu utility is programmed specifically for the US QWERTY keyboard. To use the inputEmu for another keyboard layout, the program has to be modified accordingly. If anyone&#8217;s interested using this utility for different keyboard layout, I&#8217;d be glad to provide assistance.<br />
Do you use Java&#8217;s Robot class or any of the utilities above in your work? Yair and I would love to hear about your experience in a short <a href="/articles/gui-automation-utilities/#respond">comment</a> below.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/gui-automation-utilities">GUI automation utilities</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/gui-automation-robot" rel="bookmark" title="GUI automation using a Robot">GUI automation using a Robot </a> <small>This article explains how Java's Robot class can be used to programmatically control mouse and keyboard actions...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/plotedit-context-menu-customization" rel="bookmark" title="PlotEdit context-menu customization">PlotEdit context-menu customization </a> <small>A combination of Matlab and Java Robot commands to automate a certain animation can be used when we cannot access underlying GUI/graphics code. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/figure-keypress-modifiers" rel="bookmark" title="Figure keypress modifiers">Figure keypress modifiers </a> <small>The figure's CurrentModifier property provides a simple and consistent way to retrieve keypress modifiers: alt-, control- and shift-clicks. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/uicontrol-callbacks" rel="bookmark" title="Uicontrol callbacks">Uicontrol callbacks </a> <small>This post details undocumented callbacks exposed by the underlying Java object of Matlab uicontrols, that can be used to modify the control's behavior in a multitude of different events...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/gui-automation-utilities/feed</wfw:commentRss>
			<slash:comments>7</slash:comments>
		
		
			</item>
		<item>
		<title>GUI automation using a Robot</title>
		<link>https://undocumentedmatlab.com/articles/gui-automation-robot?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=gui-automation-robot</link>
					<comments>https://undocumentedmatlab.com/articles/gui-automation-robot#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 15 Sep 2010 18:00:06 +0000</pubDate>
				<category><![CDATA[Figure window]]></category>
		<category><![CDATA[Guest bloggers]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Low risk of breaking in future versions]]></category>
		<category><![CDATA[Kesh Ikuma]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=1815</guid>

					<description><![CDATA[<p>This article explains how Java's Robot class can be used to programmatically control mouse and keyboard actions</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/gui-automation-robot">GUI automation using a Robot</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/gui-automation-utilities" rel="bookmark" title="GUI automation utilities">GUI automation utilities </a> <small>This article explains a couple of Matlab utilities that use Java's Robot class to programmatically control mouse and keyboard actions...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/figure-keypress-modifiers" rel="bookmark" title="Figure keypress modifiers">Figure keypress modifiers </a> <small>The figure's CurrentModifier property provides a simple and consistent way to retrieve keypress modifiers: alt-, control- and shift-clicks. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/inactive-control-tooltips-event-chaining" rel="bookmark" title="Inactive Control Tooltips &amp; Event Chaining">Inactive Control Tooltips &amp; Event Chaining </a> <small>Inactive Matlab uicontrols cannot normally display their tooltips. This article shows how to do this with a combination of undocumented Matlab and Java hacks....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/uicontrol-callbacks" rel="bookmark" title="Uicontrol callbacks">Uicontrol callbacks </a> <small>This post details undocumented callbacks exposed by the underlying Java object of Matlab uicontrols, that can be used to modify the control's behavior in a multitude of different events...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p><em>I would like to welcome guest writer <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/authors/36665">Takeshi (Kesh) Ikuma</a>. Kesh has posted several interesting utilities on the Matlab File Exchange, including the award-winning <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/25862-enhanced-input-dialog-box">Enhanced Input Dialog Box</a>. Today, Kesh will describe how we can automate GUI actions programmatically.</em><br />
<span class="alignright"><img decoding="async" alt="" src="https://undocumentedmatlab.com/images/robot.gif" title="" width="96" height="156" /></span><br />
Automating GUI actions, including controlling a mouse and keyboard programmatically, is often useful. This can be used, for example, to demonstrate GUI usage or to perform a recorded macro.<br />
Matlab&#8217;s Handle Graphics interface provides a simple way to manipulate the mouse cursor position, but not to emulate mouse or keyboard clicks. However, we can utilize Java&#8217;s <a target="_blank" rel="nofollow" href="http://download.oracle.com/javase/6/docs/api/java/awt/Robot.html">java.awt.Robot</a> class.<br />
This article provides an overview of the Robot class and how it can be used to program mouse movement, button clicks and keyboard strikes.</p>
<h3 id="Robot">java.awt.Robot class</h3>
<p>The online Java documentation describes the purpose of the Robot class as follows:<br />
<q>This class is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed.</q><br />
This class has three main functionalities: mouse control, keyboard control, and screen capture. Here are some of the important member functions:</p>
<h4 id="mouse-control">Mouse control functions</h4>
<p><i>void <a target="_blank" rel="nofollow" href="http://download.oracle.com/javase/6/docs/api/java/awt/Robot.html#mouseMove%28int,%20int%29">mouseMove</a>(int x, int y)</i><br />
This function moves the cursor to the coordinate (x, y) which is defined with respect to the top-left screen corner (in contrast to Matlab&#8217;s coordinate origin at the bottom-left corner).<br />
<i>void <a target="_blank" rel="nofollow" href="http://download.oracle.com/javase/6/docs/api/java/awt/Robot.html#mousePress%28int%29">mousePress</a>(int buttons)<br />
void <a target="_blank" rel="nofollow" href="http://download.oracle.com/javase/6/docs/api/java/awt/Robot.html#mouseRelease%28int%29">mouseRelease</a>(int buttons)</i><br />
This pair of functions performs the button click. Their input argument is an OR&#8217;ed combination of <a target="_blank" rel="nofollow" href="http://download.oracle.com/javase/6/docs/api/java/awt/event/InputEvent.html">java.awt.event.InputEvent</a>s:</p>
<pre lang="java">
java.awt.event.InputEvent.BUTTON1_MASK   // left mouse button
java.awt.event.InputEvent.BUTTON2_MASK   // middle mouse button
java.awt.event.InputEvent.BUTTON3_MASK   // right mouse button
</pre>
<h4 id="keyboard-control">Keyboard control functions</h4>
<p>Keyboard action is emulated by the following pair of functions. Their keycodes are defined in <a target="_blank" rel="nofollow" href="http://download.oracle.com/javase/6/docs/api/java/awt/event/KeyEvent.html">java.awt.event.KeyEvent</a>:<br />
<i>void <a target="_blank" rel="nofollow" href="http://download.oracle.com/javase/6/docs/api/java/awt/Robot.html#keyPress(int)">keyPress</a>(int keycode)<br />
void <a target="_blank" rel="nofollow" href="http://download.oracle.com/javase/6/docs/api/java/awt/Robot.html#keyRelease(int)">keyRelease</a>(int keycode)</i><br />
NOTE: Although java.awt.event.KeyEvent constants defines most of the US QWERTY keys, not all can actually be used with java.awt.Robot. Specifically, it appears that only the KeyEvent constants for unmodified keys can be used. See the following section for an example.</p>
<h4 id="utility">Utility functions</h4>
<p>The robot can be put to sleep for a desired duration (in milliseconds). Also, the calling routine can be blocked until the robot exhausts its command queue.<br />
<i>void <a target="_blank" rel="nofollow" href="http://download.oracle.com/javase/6/docs/api/java/awt/Robot.html#delay(int)">delay</a>(int ms)<br />
void <a target="_blank" rel="nofollow" href="http://download.oracle.com/javase/6/docs/api/java/awt/Robot.html#waitForIdle()">waitForIdle</a>()</i><br />
Readers interested in Robot&#8217;s screen-capture capability are invited to take a look at Yair&#8217;s <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/24323-screencapture-get-a-screen-capture-of-a-figure-frame-or-component">ScreenCapture utility</a> on the File Exchange.</p>
<h3 id="Robot-mouse">Using java.awt.Robot in Matlab</h3>
<p>To create the Robot object in Matlab, simply run</p>
<pre lang="matlab">robot = java.awt.Robot;</pre>
<h4 id="mouse-move">Moving the mouse cursor</h4>
<p>With Matlab&#8217;s root (0) handle, the mouse cursor can be repositioned by</p>
<pre lang="matlab">set(0,'PointerLocation',[x y]);</pre>
<p>The mouse cursor is placed on the x-th pixel from left edge and the y-th pixel from the bottom edge of the screen.<br />
Alternately, we could use the semi-documented <a target="_blank" href="/articles/undocumented-mouse-pointer-functions/#moveptr"><i><b>moveptr</b></i> function</a>, which was described here last year.<br />
The same operation can also be done using Robot as follows:</p>
<pre lang="matlab">
scrSize = get(0,'ScreenSize');
robot.mouseMove(x,scrSize(2)-y);
</pre>
<p>The extra step must be taken to convert from Matlab to Java screen coordinates.<br />
Depending on the specific case (for example, whether or not we know the absolute or only relative screen coordinates), we may prefer using any of these equivalent mouse-movement alternatives.</p>
<h4 id="mouse-click">Clicking mouse buttons</h4>
<p>Unfortunately, we have few alternatives for automating mouse-clicks &#8211; Robot is basically our only option. Matlab recognizes 4 different mouse click types as specified in the Figure&#8217;s <strong>SelectionType</strong> property:</p>
<ul>
<li><i>Normal</i>: Click left mouse button
</li>
<li><i>Extend</i>: SHIFT-click left mouse button
</li>
<li><i>Alternate</i>: CTRL-click left mouse button
</li>
<li><i>Open</i>: Double click left mouse button</li>
</ul>
<p>To observe the mouse click, open a figure and set its <strong>WindowButtonDownFcn</strong> callback:</p>
<pre lang="matlab">
myCallback = @(hObj,event) disp(get(hObj,'SelectionType'));
set(gcf,'WindowButtonDownFcn', myCallback);
</pre>
<p>To trigger the callback, the figure must have the focus and the mouse cursor must be on the figure. To ensure the callback invocation, &#8220;figure(gcf); drawnow;&#8221; commands are included in the code examples below.  Make sure to place the mouse cursor on the figure before running these codes.<br />
Here&#8217;s how the Robot can be used to perform each type of clicking.  First, normal click:</p>
<pre lang="matlab">
figure(gcf); drawnow;
robot.mousePress  (java.awt.event.InputEvent.BUTTON1_MASK);
robot.mouseRelease(java.awt.event.InputEvent.BUTTON1_MASK);
</pre>
<p>For &#8220;open&#8221; or double click, repeat it twice:</p>
<pre lang="matlab">
figure(gcf); drawnow;
robot.mousePress  (java.awt.event.InputEvent.BUTTON1_MASK);
robot.mouseRelease(java.awt.event.InputEvent.BUTTON1_MASK);
robot.mousePress  (java.awt.event.InputEvent.BUTTON1_MASK);
robot.mouseRelease(java.awt.event.InputEvent.BUTTON1_MASK);
</pre>
<p>For the other two click types, we need to use the keyboard functions. First, &#8220;extend&#8221; or SHIFT-click:</p>
<pre lang="matlab">
figure(gcf); drawnow;
robot.keyPress    (java.awt.event.KeyEvent.VK_SHIFT);
robot.mousePress  (java.awt.event.InputEvent.BUTTON1_MASK);
robot.mouseRelease(java.awt.event.InputEvent.BUTTON1_MASK);
robot.keyRelease  (java.awt.event.KeyEvent.VK_SHIFT);
</pre>
<p>Lastly, &#8220;alternate&#8221; or CTRL-click:</p>
<pre lang="matlab">
figure(gcf); drawnow;
robot.keyPress    (java.awt.event.KeyEvent.VK_CONTROL);
robot.mousePress  (java.awt.event.InputEvent.BUTTON1_MASK);
robot.mouseRelease(java.awt.event.InputEvent.BUTTON1_MASK);
robot.keyRelease  (java.awt.event.KeyEvent.VK_CONTROL);
</pre>
<h4 id="key-click">Clicking keyboard keys</h4>
<p>Just as we have shown above how to press a modifier key (Ctrl or Alt key) to set the figure&#8217;s <strong>SelectionType</strong> property, <em>keyPress</em> and <em>keyRelease</em> functions can be used with character keys to type text. For example, with the focus on Matlab&#8217;s Command Prompt, running the following code executes the <i><b>ver</b></i> command:</p>
<pre lang="matlab">
robot.keyPress    (java.awt.event.KeyEvent.VK_V);
robot.keyRelease  (java.awt.event.KeyEvent.VK_V);
robot.keyPress    (java.awt.event.KeyEvent.VK_E);
robot.keyRelease  (java.awt.event.KeyEvent.VK_E);
robot.keyPress    (java.awt.event.KeyEvent.VK_R);
robot.keyRelease  (java.awt.event.KeyEvent.VK_R);
robot.keyPress    (java.awt.event.KeyEvent.VK_ENTER);
robot.keyRelease  (java.awt.event.KeyEvent.VK_ENTER);
</pre>
<p>In the previous section, I mentioned that not all KeyEvent constants are supported by java.awt.Robot, and keys must be defined with their unmodified characters. For example, a semicolon (&#8216;;&#8217;) can be typed by:</p>
<pre lang="matlab">
robot.keyPress    (java.awt.event.KeyEvent.VK_SEMICOLON);
robot.keyRelease  (java.awt.event.KeyEvent.VK_SEMICOLON);
</pre>
<p>However, doing the same for a colon (&#8216;:&#8217; or SHIFT-&#8216;;&#8217;) causes an error:</p>
<pre lang="matlab">
robot.keyPress    (java.awt.event.KeyEvent.VK_COLON);
robot.keyRelease  (java.awt.event.KeyEvent.VK_COLON);
??? Java exception occurred:
java.lang.IllegalArgumentException: Invalid key code
    at sun.awt.windows.WRobotPeer.keyPress(Native Method)
    at java.awt.Robot.keyPress(Unknown Source)
</pre>
<p>Instead of using the VK_COLON constant, VK_SEMICOLON constant must be used while SHIFT modifier key is pressed:</p>
<pre lang="matlab">
robot.keyPress    (java.awt.event.KeyEvent.VK_SHIFT);
robot.keyPress    (java.awt.event.KeyEvent.VK_SEMICOLON);
robot.keyRelease  (java.awt.event.KeyEvent.VK_SEMICOLON);
robot.keyRelease  (java.awt.event.KeyEvent.VK_SHIFT);
</pre>
<p>Although unconfirmed, I suspect VK_SEMICOLON constant would not work with keyboards in which semicolon is mapped to SHIFT-&#8216;,&#8217; (e.g., Spanish or Italian keyboard layouts), while the VK_COLON constant would work with a French keyboard.</p>
<h3 id="jMouseEmu">Putting it all together: jMouseEmu and inputEmu</h3>
<p>To simplify the execution of the mouse and keyboard actions outlined above, I (Kesh Ikuma) have created the <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/28357-jMouseEmu">jMouseEmu</a> and <a target="_blank" rel="nofollow" href="http://www.mathworks.com/matlabcentral/fileexchange/28603-inputEmu">inputEmu</a> utilities, which are available on the Matlab File Exchange. These interface/wrapper functions will be described in next week&#8217;s article.<br />
Do you have any favorite use for Java Robots in your workflow? If so, please tell us all about it in a comment <a href="/articles/gui-automation-robot/#respond">below</a>.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/gui-automation-robot">GUI automation using a Robot</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/gui-automation-utilities" rel="bookmark" title="GUI automation utilities">GUI automation utilities </a> <small>This article explains a couple of Matlab utilities that use Java's Robot class to programmatically control mouse and keyboard actions...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/figure-keypress-modifiers" rel="bookmark" title="Figure keypress modifiers">Figure keypress modifiers </a> <small>The figure's CurrentModifier property provides a simple and consistent way to retrieve keypress modifiers: alt-, control- and shift-clicks. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/inactive-control-tooltips-event-chaining" rel="bookmark" title="Inactive Control Tooltips &amp; Event Chaining">Inactive Control Tooltips &amp; Event Chaining </a> <small>Inactive Matlab uicontrols cannot normally display their tooltips. This article shows how to do this with a combination of undocumented Matlab and Java hacks....</small></li>
<li><a href="https://undocumentedmatlab.com/articles/uicontrol-callbacks" rel="bookmark" title="Uicontrol callbacks">Uicontrol callbacks </a> <small>This post details undocumented callbacks exposed by the underlying Java object of Matlab uicontrols, that can be used to modify the control's behavior in a multitude of different events...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/gui-automation-robot/feed</wfw:commentRss>
			<slash:comments>37</slash:comments>
		
		
			</item>
	</channel>
</rss>
