Undocumented Matlab
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT

GUI automation using a Robot

September 15, 2010 34 Comments

I would like to welcome guest writer Takeshi (Kesh) Ikuma. Kesh has posted several interesting utilities on the Matlab File Exchange, including the award-winning Enhanced Input Dialog Box. Today, Kesh will describe how we can automate GUI actions programmatically.

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.
Matlab’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’s java.awt.Robot class.
This article provides an overview of the Robot class and how it can be used to program mouse movement, button clicks and keyboard strikes.

java.awt.Robot class

The online Java documentation describes the purpose of the Robot class as follows:
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.
This class has three main functionalities: mouse control, keyboard control, and screen capture. Here are some of the important member functions:

Mouse control functions

void mouseMove(int x, int y)
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’s coordinate origin at the bottom-left corner).
void mousePress(int buttons)
void mouseRelease(int buttons)

This pair of functions performs the button click. Their input argument is an OR’ed combination of java.awt.event.InputEvents:

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

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

Keyboard control functions

Keyboard action is emulated by the following pair of functions. Their keycodes are defined in java.awt.event.KeyEvent:
void keyPress(int keycode)
void keyRelease(int keycode)

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.

Utility functions

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.
void delay(int ms)
void waitForIdle()

Readers interested in Robot’s screen-capture capability are invited to take a look at Yair’s ScreenCapture utility on the File Exchange.

Using java.awt.Robot in Matlab

To create the Robot object in Matlab, simply run

robot = java.awt.Robot;

robot = java.awt.Robot;

Moving the mouse cursor

With Matlab’s root (0) handle, the mouse cursor can be repositioned by

set(0,'PointerLocation',[x y]);

set(0,'PointerLocation',[x y]);

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.
Alternately, we could use the semi-documented moveptr function, which was described here last year.
The same operation can also be done using Robot as follows:

scrSize = get(0,'ScreenSize');
robot.mouseMove(x,scrSize(2)-y);

scrSize = get(0,'ScreenSize'); robot.mouseMove(x,scrSize(2)-y);

The extra step must be taken to convert from Matlab to Java screen coordinates.
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.

Clicking mouse buttons

Unfortunately, we have few alternatives for automating mouse-clicks – Robot is basically our only option. Matlab recognizes 4 different mouse click types as specified in the Figure’s SelectionType property:

  • Normal: Click left mouse button
  • Extend: SHIFT-click left mouse button
  • Alternate: CTRL-click left mouse button
  • Open: Double click left mouse button

To observe the mouse click, open a figure and set its WindowButtonDownFcn callback:

myCallback = @(hObj,event) disp(get(hObj,'SelectionType'));
set(gcf,'WindowButtonDownFcn', myCallback);

myCallback = @(hObj,event) disp(get(hObj,'SelectionType')); set(gcf,'WindowButtonDownFcn', myCallback);

To trigger the callback, the figure must have the focus and the mouse cursor must be on the figure. To ensure the callback invocation, “figure(gcf); drawnow;” commands are included in the code examples below. Make sure to place the mouse cursor on the figure before running these codes.
Here’s how the Robot can be used to perform each type of clicking. First, normal click:

figure(gcf); drawnow;
robot.mousePress  (java.awt.event.InputEvent.BUTTON1_MASK);
robot.mouseRelease(java.awt.event.InputEvent.BUTTON1_MASK);

figure(gcf); drawnow; robot.mousePress (java.awt.event.InputEvent.BUTTON1_MASK); robot.mouseRelease(java.awt.event.InputEvent.BUTTON1_MASK);

For “open” or double click, repeat it twice:

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);

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);

For the other two click types, we need to use the keyboard functions. First, “extend” or SHIFT-click:

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);

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);

Lastly, “alternate” or CTRL-click:

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);

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);

Clicking keyboard keys

Just as we have shown above how to press a modifier key (Ctrl or Alt key) to set the figure’s SelectionType property, keyPress and keyRelease functions can be used with character keys to type text. For example, with the focus on Matlab’s Command Prompt, running the following code executes the ver command:

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);

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);

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 (‘;’) can be typed by:

robot.keyPress    (java.awt.event.KeyEvent.VK_SEMICOLON);
robot.keyRelease  (java.awt.event.KeyEvent.VK_SEMICOLON);

robot.keyPress (java.awt.event.KeyEvent.VK_SEMICOLON); robot.keyRelease (java.awt.event.KeyEvent.VK_SEMICOLON);

However, doing the same for a colon (‘:’ or SHIFT-‘;’) causes an error:

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)

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)

Instead of using the VK_COLON constant, VK_SEMICOLON constant must be used while SHIFT modifier key is pressed:

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);

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);

Although unconfirmed, I suspect VK_SEMICOLON constant would not work with keyboards in which semicolon is mapped to SHIFT-‘,’ (e.g., Spanish or Italian keyboard layouts), while the VK_COLON constant would work with a French keyboard.

Putting it all together: jMouseEmu and inputEmu

To simplify the execution of the mouse and keyboard actions outlined above, I (Kesh Ikuma) have created the jMouseEmu and inputEmu utilities, which are available on the Matlab File Exchange. These interface/wrapper functions will be described in next week’s article.
Do you have any favorite use for Java Robots in your workflow? If so, please tell us all about it in a comment below.

Related posts:

  1. GUI automation utilities – This article explains a couple of Matlab utilities that use Java's Robot class to programmatically control mouse and keyboard actions...
  2. Figure keypress modifiers – The figure's CurrentModifier property provides a simple and consistent way to retrieve keypress modifiers: alt-, control- and shift-clicks. ...
  3. Inactive Control Tooltips & Event Chaining – Inactive Matlab uicontrols cannot normally display their tooltips. This article shows how to do this with a combination of undocumented Matlab and Java hacks....
  4. Uicontrol callbacks – 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...
  5. JGraph in Matlab figures – JGraph is a powerful open-source Java library that can easily be integrated in Matlab figures. ...
  6. ScreenCapture utility – The ScreenCapture utility uses purely-documented Matlab for capturing a screen region as an image from within Matlab. ...
GUI Java Kesh Ikuma
Print Print
« Previous
Next »
34 Responses
  1. Martin Richard September 16, 2010 at 04:18 Reply

    Very interesting. I used this java.awt.Robot class recently to perform GUI testing in MATLAB and, with little effort, it became a very interesting and simple feature to use.
    Thanks for posting

  2. Sabrina Wendt February 1, 2011 at 08:22 Reply

    Response to:
    “Although unconfirmed, I suspect VK_SEMICOLON constant would not work with keyboards in which semicolon is mapped to SHIFT-’,’ ”

    I can confirm that this is true (writting from a danish laptop). At my computer ‘;’ is mapped to ‘,’ and so to generate a semicolon it works to write the following in MATLAB:

    robot.keyPress (java.awt.event.KeyEvent.VK_SHIFT);
    robot.keyPress (java.awt.event.KeyEvent.VK_COMMA);
    robot.keyRelease (java.awt.event.KeyEvent.VK_COMMA);
    robot.keyRelease (java.awt.event.KeyEvent.VK_SHIFT);

    Great article! Thanks for posting 🙂

  3. Héctor August 17, 2011 at 06:24 Reply

    I try this example some time ago from another source. Now I´m reading all your posts to learn new things (great job). When I reach newer posts I will ask you some things I want to do and don´t know how.

  4. Ankit January 15, 2012 at 02:14 Reply

    Hi,
    Just like you have given the commands for keyboard and mouse interface is there any command for joystick interface in java. Please help.

    • Yair Altman January 15, 2012 at 02:23 Reply

      @Ankit – different joysticks interface with the computer differently (for example, via USB or serial port). So there is nothing standard. However, there are numerous external Java joystick interfaces that you can integrate with Matlab. For example, http://sourceforge.net/projects/javajoystick/ and http://www.hardcode.de/jxinput/.

  5. appy February 29, 2012 at 23:06 Reply

    Hi this is very interesting i used it in matlab figure instead of overobj but i have problem i used mouse press
    and mouse release in windowmotionFcn in mat lab i.e when i move the mouse at that point mouse button will be pressed programmatically if i move mouse over the toolbar it is pressing the button automatically zoom save are working even to resize or close window will be activated if i just move the mouse over them but i donot want this to happen how to stop.

    Thank you.

  6. timmy July 6, 2012 at 08:56 Reply

    Has anyone experienced double clicking using Press/Release/Press/Release not working? Say for opening an image icon on desktop. The icon is correctly selected but the correct double-clicking results (application opening) is not performed. Everything else works, even dragging icons around, just not opening icons via double click. Thanks for the help.

  7. ScreenCapture utility | Undocumented Matlab August 16, 2012 at 00:21 Reply

    […] I have discussed the Java Robot in two past articles on this blog, where guest blogger Kesh Ikuma explained how it can be used to simulate mouse and keyboard actions programmatically. […]

  8. MEENAKSHI.S January 30, 2013 at 00:27 Reply

    I want to use the executable file in Matlab for optimisation methods. How can I use the run button of that exe file in matlab command window as a command and not by clicking the run button of exe file so that repeated process of clicking can be avoid in optimisation methods (exe file is visual c++ program-MFC Application).

    • Yair Altman January 30, 2013 at 04:53 Reply

      @Meenakshi – you would need to write an automation script for this, either using the Java Robot class (as described in the article), or using an external application (e.g., AutoIt) that you would call from Matlab using the system command. If you would like my help writing such an application for you as a consultant, please send me an email (altmany at gmail).

  9. MEENAKSHI.S February 12, 2013 at 03:49 Reply

    thank you sir…i have tried Java Robot class in matlab…it works well..i.e.to press F1 KEY of matlab suddenly help window opened…so really happy to see the response without clicking F1 KEY…it works well…but i want to press F5 KEY of EXECUTABLE FILE IN MATLAB COMMAND WINDOW…What can i do sir.. PLEASE SHARE YOUR IDEAS…Thank you Sir…

    • Yair Altman February 12, 2013 at 09:21 Reply

      @Meenakshi – I don’t understand your problem. Simply call the relevant function directly, you don’t need F5.

  10. George March 14, 2013 at 10:40 Reply

    thanks a lot for your post,and i have a question

    i want to type 1 to 3 with matlab
    of course i can write

    import java.awt.Robot;
    import java.awt.event.*;
    robot=Robot();
    robot.keyPress(KeyEvent.VK_1);
    robot.keyRelease(KeyEvent.VK_1);
    robot.keyPress(KeyEvent.VK_2);
    robot.keyRelease(KeyEvent.VK_2);
    robot.keyPress(KeyEvent.VK_3);
    robot.keyRelease(KeyEvent.VK_3);

    import java.awt.Robot; import java.awt.event.*; robot=Robot(); robot.keyPress(KeyEvent.VK_1); robot.keyRelease(KeyEvent.VK_1); robot.keyPress(KeyEvent.VK_2); robot.keyRelease(KeyEvent.VK_2); robot.keyPress(KeyEvent.VK_3); robot.keyRelease(KeyEvent.VK_3);

    i wanna ask how to do these with while or for loop

    thank you

    • Yair Altman March 14, 2013 at 16:39 Reply

      @George – in your specific case you can simply use the numeric equivalents of KeyEvent.VK_*: VK_0=48, VK_1=49 and so on until VK_9=57. You can see the full list using my checkClass utility. Turning these numbers into a loop is trivial. Note that using numbers is far less maintainable than using the enumerations, so add ample documentation to your code.

    • Mayank August 5, 2017 at 16:01 Reply

      create an int[] array for all the KeyEvent.Vk_1 to ..last then use the for loop to iterate over the same.

  11. Muthu Vicky October 12, 2013 at 06:23 Reply

    Thanks a lot for this post.. This is really awesome!!

    Can you please tell me how to press function keys (i.e.) f6 or f7 or some other function key?

    • Lucius Domitius Ahenobarbus November 19, 2013 at 02:47 Reply

      Just use:

      robot.keyPress(KeyEvent.VK_F6);
      robot.keyRelease(KeyEvent.VK_F6);

      robot.keyPress(KeyEvent.VK_F6); robot.keyRelease(KeyEvent.VK_F6);

      • Lucius Domitius Ahenobarbus November 19, 2013 at 02:49

        Btw: Thanks a lot – again – this blog was a big help for me!

  12. hema December 30, 2013 at 00:53 Reply

    how to right click on a window??

    • Yair Altman December 31, 2013 at 16:09 Reply

      @hema –

      jRobot = java.awt.Robot;
      jRobot.mouseMove(400,300); % move to absolute x,y pixel position
      jEvent = java.awt.event.InputEvent.BUTTON3_MASK;
      jRobot.mousePress(jEvent); jRobot.mouseRelease(jEvent);  %=right-click

      jRobot = java.awt.Robot; jRobot.mouseMove(400,300); % move to absolute x,y pixel position jEvent = java.awt.event.InputEvent.BUTTON3_MASK; jRobot.mousePress(jEvent); jRobot.mouseRelease(jEvent); %=right-click

  13. puja May 30, 2014 at 04:00 Reply

    i am working on dynamic typing project.i want to capture the time when key is pressed and released.can anyone please help me

    • Yair Altman May 30, 2014 at 07:42 Reply

      @Puja – take a look at the KeyPressedCallback and KeyReleasedCallback of uicontrols, explained here: https://undocumentedmatlab.com/blog/uicontrol-callbacks

  14. Ujjawal May 15, 2015 at 05:44 Reply

    Hi,

    Nice post.
    Is there a way to press right control(Ctrl) key on windows using Robot or if there is some other solution to press right control key programatically?

    Best Regards

  15. Ajay June 3, 2015 at 10:49 Reply

    I am working on a project in which … I am first detecting the color objects from the video and then to those detected colors ..we are assigning mouse functions …like red for mouse pointer movement …green for mouse scroll and one blue for left click …two blue for right click and three blue for double click ….mouse pointer movement …scrolling and left click …upto this we have done …but the right click and double click are the two functions we are facing problem with …we are not being able to fit the one blue object …two blue objects and 3 blue objects in a loop …so it will differentiate between them and excute those functions …so plz help …if u know something regarding this

    • Yair Altman June 3, 2015 at 11:16 Reply

      @Ajay – as the article explains you can use robot.mousePress and robot.mouseRelease to automate the mouse clicks at the current mouse pointer position.

      But ..why ..are ..you ..writing ..like ..this?! – ..it ..is ..so ..difficult ..to ..read! can’t you write text like a normal person ???

  16. sriram November 24, 2015 at 08:56 Reply

    We need to control a different Java application window (launched via JNLP). Need to enter username and password. Is it possible with Matlab.

  17. Xen March 23, 2016 at 23:44 Reply

    Very helpful indeed. Thank you.
    I am simulating the ESCAPE button press in my GUI to cancel drawing using imfreehand tool. After doing this for a while, the GUI becomes slow progressively until a point where it is no longer usable. I suspect that java.awt.Robot might have to do with it. Is there a way to cancel/stop/remove the robot? After creating the robot using

    removing it using

    does the job appropriately, or there is a more appropriate way? Or maybe something else might be causing the GUI to run slowly?

    • Yair Altman March 24, 2016 at 11:56 Reply

      Your code snippets did not come through, but in any case I suspect that the problem is not in Robot, which is a very simple class and probably not the source of the issues that you see.

  18. Apostolos Iliopoulos March 29, 2016 at 17:01 Reply

    Very nice work!!
    But i have a problem. I would like to write a code which runs an executable file and then enter to programs window to create a new case. The code that I’m using is this

    system('w0315v11-64bit');
    robot = java.awt.Robot;
    robot.keyPress  (java.awt.event.KeyEvent.VK_ENTER);
    robot.keyRelease(java.awt.event.KeyEvent.VK_ENTER);

    system('w0315v11-64bit'); robot = java.awt.Robot; robot.keyPress (java.awt.event.KeyEvent.VK_ENTER); robot.keyRelease(java.awt.event.KeyEvent.VK_ENTER);

    But instead of pressing enter on the programs window it presses it in the Matlab editor.
    Can you please tell me how to overcome this?
    Thank you in advance

    • Yair Altman March 30, 2016 at 10:55 Reply

      @Apostolos – the code as you wrote it just simulates pressing the <Enter> key so the key event will be sent to the currently-active window. If your editor is currently in focus then obviously the key event will be sent to it. If you want to send the key event to another window you must first ensure that the focus is on that window. There is no general way to do this, it is application-dependent.

  19. NSReddy September 24, 2016 at 13:46 Reply

    Can u help me how to do the ” Alt+S” using Robot Class

  20. Joshua November 14, 2017 at 05:47 Reply

    Hi, is there a way to simulate the keyboard input using the robot when the robot encounters user input request generated? For example,

    a = input('Type a word: ', 's');

    a = input('Type a word: ', 's');

    This is for the purpose of testing the interactive feature of a program. Is it possible?

    Thanks!

  21. maiky76 January 28, 2022 at 19:34 Reply

    Hi,

    The original post got me up and running in no time. Thank you so much!
    I have been trying to translate what I did in Octave to share the code with others but I can’t get it to work.

    Would anyone be gracious enough to give it a try?

    Here is the code that behaves as I’d like it to:
    Open external program, return to Octave script, Octave makes some keyboard inputs closes the program, then back to Octave to finish the remaining commands of the script

    clear all
    close all
    clc
     
    if exist('OCTAVE_VERSION', 'builtin') == 0
     % Matlab version   
            import java.awt.*;
            import java.awt.event.*;
     
            system('"calc.exe"&amp;');
     
            R2D2 = Robot;
            R2D2.delay(100) 
            R2D2.keyPress(KeyEvent.VK_8)
            R2D2.keyRelease(KeyEvent.VK_8)
            R2D2.delay(500)  
        %     R2D2.keyPress(KeyEvent.VK_PLUS) % does not work!
        %    R2D2.keyRelease(KeyEvent.VK_PLUS) 
            R2D2.keyPress(KeyEvent.VK_DIVIDE)
            R2D2.keyRelease(KeyEvent.VK_DIVIDE)
            R2D2.delay(500) 
            R2D2.keyPress(KeyEvent.VK_2)
            R2D2.keyRelease(KeyEvent.VK_2)
            R2D2.delay(500) 
            R2D2.keyPress(KeyEvent.VK_ENTER)
            R2D2.keyRelease(KeyEvent.VK_ENTER)
            R2D2.delay(1000) 
            R2D2.keyPress(KeyEvent.VK_ALT)
            R2D2.keyPress(KeyEvent.VK_F4)
            R2D2.keyRelease(KeyEvent.VK_ALT)
            R2D2.keyRelease(KeyEvent.VK_F4)
          else
    % Octave Version not working 
    % OBJ = java_set (OBJ, NAME, VAL)
    % obj = javaObject (classname, arg1, …)
    % Ret = javaMethod (methodname, obj, arg1, …)  
    % There is no import function so we need to express the full qualifier
    % https://savannah.gnu.org/task/?12601
     
            system("calc.exe", false, "async"); 
     
            R2D2 = javaObject("java.awt.Robot"); % works
            R2D2.delay(5000); % works
    %        R2D2.keyPress(KeyEvent.VK_8)
    %        javaMethod('KeyPress', R2D2, VK_ENTER)
    %        javaMethod("KeyRelease", R2D2, 'VK_8')  
    %        javaMethod('KeyPress', 'VK_ENTER')
    %        R2D2.keyRelease(KeyEvent.VK_8)
     
    %        R2D2.delay(500); 
    %       javaMethod('KeyPress', R2D2, VK_PLUS)
    %        javaMethod('KeyRelease', R2D2, VK_PLUS)
    %        R2D2.delay(500); 
    %        javaMethod('KeyPress', R2D2, VK_2)
    %        javaMethod('KeyRelease', R2D2, VK_2)
    %        R2D2.delay(500); 
    %       javaMethod('KeyPress', R2D2, VK_ENTER)
    %        javaMethod('KeyRelease', R2D2, VK_ENTER)
    %      1+2
     
    end

    clear all close all clc if exist('OCTAVE_VERSION', 'builtin') == 0 % Matlab version import java.awt.*; import java.awt.event.*; system('"calc.exe"&amp;'); R2D2 = Robot; R2D2.delay(100) R2D2.keyPress(KeyEvent.VK_8) R2D2.keyRelease(KeyEvent.VK_8) R2D2.delay(500) % R2D2.keyPress(KeyEvent.VK_PLUS) % does not work! % R2D2.keyRelease(KeyEvent.VK_PLUS) R2D2.keyPress(KeyEvent.VK_DIVIDE) R2D2.keyRelease(KeyEvent.VK_DIVIDE) R2D2.delay(500) R2D2.keyPress(KeyEvent.VK_2) R2D2.keyRelease(KeyEvent.VK_2) R2D2.delay(500) R2D2.keyPress(KeyEvent.VK_ENTER) R2D2.keyRelease(KeyEvent.VK_ENTER) R2D2.delay(1000) R2D2.keyPress(KeyEvent.VK_ALT) R2D2.keyPress(KeyEvent.VK_F4) R2D2.keyRelease(KeyEvent.VK_ALT) R2D2.keyRelease(KeyEvent.VK_F4) else % Octave Version not working % OBJ = java_set (OBJ, NAME, VAL) % obj = javaObject (classname, arg1, …) % Ret = javaMethod (methodname, obj, arg1, …) % There is no import function so we need to express the full qualifier % https://savannah.gnu.org/task/?12601 system("calc.exe", false, "async"); R2D2 = javaObject("java.awt.Robot"); % works R2D2.delay(5000); % works % R2D2.keyPress(KeyEvent.VK_8) % javaMethod('KeyPress', R2D2, VK_ENTER) % javaMethod("KeyRelease", R2D2, 'VK_8') % javaMethod('KeyPress', 'VK_ENTER') % R2D2.keyRelease(KeyEvent.VK_8) % R2D2.delay(500); % javaMethod('KeyPress', R2D2, VK_PLUS) % javaMethod('KeyRelease', R2D2, VK_PLUS) % R2D2.delay(500); % javaMethod('KeyPress', R2D2, VK_2) % javaMethod('KeyRelease', R2D2, VK_2) % R2D2.delay(500); % javaMethod('KeyPress', R2D2, VK_ENTER) % javaMethod('KeyRelease', R2D2, VK_ENTER) % 1+2 end

    The reason why I am interested can we found here: Acoustic Horn Design – The Easy Way (Ath4) | Page 459 | diyAudio)
    We are trying to perform some optimization on the design of an DIY acoustic waveguide.
    Any help would be much appreciated!

    • maiky76 January 30, 2022 at 03:06 Reply

      I figured it out eventually. To my knowledge this is the only public example: https://octave.discourse.group/t/help-on-java-keyevent/2163/14

      robot   = javaObject("java.awt.Robot");
      wait1   = robot.delay(500);
      VKeight = java_get ('java.awt.event.KeyEvent','VK_8');
      a1      = javaMethod ('keyPress', robot, VKeight);
      a2      = javaMethod ('keyRelease', robot, VKeight);

      robot = javaObject("java.awt.Robot"); wait1 = robot.delay(500); VKeight = java_get ('java.awt.event.KeyEvent','VK_8'); a1 = javaMethod ('keyPress', robot, VKeight); a2 = javaMethod ('keyRelease', robot, VKeight);

      etc…

Leave a Reply
HTML tags such as <b> or <i> are accepted.
Wrap code fragments inside <pre lang="matlab"> tags, like this:
<pre lang="matlab">
a = magic(3);
disp(sum(a))
</pre>
I reserve the right to edit/delete comments (read the site policies).
Not all comments will be answered. You can always email me (altmany at gmail) for private consulting.

Click here to cancel reply.

Useful links
  •  Email Yair Altman
  •  Subscribe to new posts (feed)
  •  Subscribe to new posts (reader)
  •  Subscribe to comments (feed)
 
Accelerating MATLAB Performance book
Recent Posts

Speeding-up builtin Matlab functions – part 3

Improving graphics interactivity

Interesting Matlab puzzle – analysis

Interesting Matlab puzzle

Undocumented plot marker types

Matlab toolstrip – part 9 (popup figures)

Matlab toolstrip – part 8 (galleries)

Matlab toolstrip – part 7 (selection controls)

Matlab toolstrip – part 6 (complex controls)

Matlab toolstrip – part 5 (icons)

Matlab toolstrip – part 4 (control customization)

Reverting axes controls in figure toolbar

Matlab toolstrip – part 3 (basic customization)

Matlab toolstrip – part 2 (ToolGroup App)

Matlab toolstrip – part 1

Categories
  • Desktop (45)
  • Figure window (59)
  • Guest bloggers (65)
  • GUI (165)
  • Handle graphics (84)
  • Hidden property (42)
  • Icons (15)
  • Java (174)
  • Listeners (22)
  • Memory (16)
  • Mex (13)
  • Presumed future risk (394)
    • High risk of breaking in future versions (100)
    • Low risk of breaking in future versions (160)
    • Medium risk of breaking in future versions (136)
  • Public presentation (6)
  • Semi-documented feature (10)
  • Semi-documented function (35)
  • Stock Matlab function (140)
  • Toolbox (10)
  • UI controls (52)
  • Uncategorized (13)
  • Undocumented feature (217)
  • Undocumented function (37)
Tags
ActiveX (6) AppDesigner (9) Callbacks (31) Compiler (10) Desktop (38) Donn Shull (10) Editor (8) Figure (19) FindJObj (27) GUI (141) GUIDE (8) Handle graphics (78) HG2 (34) Hidden property (51) HTML (26) Icons (9) Internal component (39) Java (178) JavaFrame (20) JIDE (19) JMI (8) Listener (17) Malcolm Lidierth (8) MCOS (11) Memory (13) Menubar (9) Mex (14) Optical illusion (11) Performance (78) Profiler (9) Pure Matlab (187) schema (7) schema.class (8) schema.prop (18) Semi-documented feature (6) Semi-documented function (33) Toolbar (14) Toolstrip (13) uicontrol (37) uifigure (8) UIInspect (12) uitools (20) Undocumented feature (187) Undocumented function (37) Undocumented property (20)
Recent Comments
  • Nicholas (6 days 14 hours ago): Hi Yair, Thanks for the reply. I am on Windows 10. I also forgot to mention that this all works wonderfully out of the editor. It only fails once compiled. So, yes, I have tried a...
  • Nicholas (6 days 14 hours ago): Hi Yair, Thanks for the reply. I am on Windows 10. I also forgot to mention that this all works wonderfully out of the editor. It only fails once compiled. So, yes, I have tried a...
  • Yair Altman (6 days 21 hours ago): Nicholas – yes, I used it in a compiled Windows app using R2022b (no update). You didn’t specify the Matlab code location that threw the error so I can’t help...
  • Nicholas (7 days 17 hours ago): Hi Yair, Have you attempted your displayWebPage utility (or the LightweightHelpPanel in general) within a compiled application? It appears to fail in apps derived from both R2022b...
  • João Neves (10 days 22 hours ago): I am on matlab 2021a, this still works: url = struct(struct(struct(struct(hF ig).Controller).PlatformHost). CEF).URL; but the html document is empty. Is there still a way to do...
  • Yair Altman (13 days 21 hours ago): Perhaps the class() function could assist you. Or maybe just wrap different access methods in a try-catch so that if one method fails you could access the data using another...
  • Jeroen Boschma (13 days 23 hours ago): Never mind, the new UI components have an HTML panel available. Works for me…
  • Alexandre (14 days 0 hours ago): Hi, Is there a way to test if data dictionnatry entry are signal, simulink parameters, variables … I need to access their value, but the access method depends on the data...
  • Nicholas (14 days 14 hours ago): In case anyone is looking for more info on the toolbar: I ran into some problems creating a toolbar with the lightweight panel. Previously, the Browser Panel had an addToolbar...
  • Jeroen Boschma (17 days 21 hours ago): I do not seem to get the scrollbars (horizontal…) working in Matlab 2020b. Snippets of init-code (all based on Yair’s snippets on this site) handles.text_explorer...
  • Yair Altman (46 days 0 hours ago): m_map is a mapping tool, not even created by MathWorks and not part of the basic Matlab system. I have no idea why you think that the customizations to the builtin bar function...
  • chengji chen (46 days 6 hours ago): Hi, I have tried the method, but it didn’t work. I plot figure by m_map toolbox, the xticklabel will add to the yticklabel at the left-down corner, so I want to move down...
  • Yair Altman (53 days 23 hours ago): @Alexander – this is correct. Matlab stopped including sqlite4java in R2021b (it was still included in 21a). You can download the open-source sqlite4java project from...
  • Alexander Eder (59 days 19 hours ago): Unfortunately Matlab stopped shipping sqlite4java starting with R2021(b?)
  • K (66 days 5 hours ago): Is there a way to programmatically manage which figure gets placed where? Let’s say I have 5 figures docked, and I split it into 2 x 1, I want to place 3 specific figures on the...
Contact us
Captcha image for Custom Contact Forms plugin. You must type the numbers shown in the image
Undocumented Matlab © 2009 - Yair Altman
This website and Octahedron Ltd. are not affiliated with The MathWorks Inc.; MATLAB® is a registered trademark of The MathWorks Inc.
Scroll to top