View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from

5.0 | 5 ratings Rate this file 17 Downloads (last 30 days) File Size: 4.22 KB File ID: #40001 Version: 1.1
image thumbnail



Phillip (view profile)


25 Jan 2013 (Updated )

Send key strokes to any window from within Matlab

| Watch this File

File Information

Send key strokes to a named Window. Alt, Ctrl, Shift, Enter and Tab are
all supported, allowing you to fully navigate the menu of other programs
from within Matlab. If you simply wish to type into another program (as
opposed to navigating a menu), see my other submission "textInject".

Send some text to Notepad, save it with a specified file name using
Notepad's menu, then return focus to Matlab:
keyInject('Untitled - Notepad','Here is some text I would like to save','Untitled - Notepad')
keyInject('Untitled - Notepad','ALT__FA','Untitled - Notepad')
keyInject('Save As','this is the file name i want.txt\r','Matlab R2012b')

Supported keystrokes:
alphanumeric (a-z,0-9), L Alt ('ALT__'), L Ctrl ('CTRL__'), L Shift
('SHIFT__'), Tab ('\t'), Enter ('\r'), Dash ('-') and Period ('.'). For
capital letters you can hold shift (see below).

Holding down Alt, Ctrl or Shift keys:
Replace the second underscore character with a '+' to hold the key down,
or a '-' to release it. For example to select all text in a window and
copy it to the clipboard: 'CTRL_+AC' (followed by 'CTRL_-' to release).
See the screenshot on the file exchange for an example of how to this in
action (selects text in Matlab and copy-pastes it into Notepad).

Adding new keys: This must be done manually, but it's easy if you copy
the approach I've laid out below. You will need a 'Virtual Key Code':
The hex values there need to be converted to decimal:
You don't need to recompile the mex files to add new keys.

Mex files: You must compile the included source code to use this utility. The Windows keyboard API uses C++, and so you will need a C++ compiler (the Matlab LCC compiler will not work). Once you have a supported compiler, type mex -setup to select it. Then put the C files on the Matlab path and just type:
mex ''
mex ''


Text Inject inspired this file.

This file inspired Interactive Link Between Matlab And Ansys.

Required Products MATLAB
MATLAB release MATLAB 8.0 (R2012b)
Other requirements Microsoft Windows
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (12)
03 Feb 2017 Daniel

Daniel (view profile)

It's great!!
Works so fine!

03 Feb 2017 Daniel

Daniel (view profile)

02 Dec 2015 Phillip

Phillip (view profile)

Hi Todd,

I don't think so, not without emulating Windows, because The C files call the Windows API. I'm sure there are similar function calls that exist within Mac OS, but I would have no idea what they are. My advice would be to run Bootcamp or similar...

Comment only
01 Dec 2015 Todd Karin

This looks like exactly what I need, but I run mac OS. Is there any way to get this working on mac?

Comment only
06 Jul 2015 Phillip

Phillip (view profile)

Hi Hakan,

To add F4 as a key, do the following:

1) Underneath all the variables named "...Positions", add:
f4Positions = strfind(sendString,'F4');

2) Add this term to the line defining numberOfKeyStrokes:
... - length(f4Positions)*2;

3) In the long chain of if/elseif statements, add the following option:

elseif find(f4Positions == nextPos)

sendKey = 115;
nextPos = nextPos + 1;


You can then run the function like this if you want to use Alt-F4 to close a window:
keyInject('Untitled - Notepad','ALT_+F4ALT_-','Matlab R2012b')

This can be a little screwy though as the window disappears while the Alt key is held down. Another option that you might find preferable in this instance could be:

keyInject('Untitled - Notepad','ALT_+FALT_-X','Matlab R2012b')

This allows the Alt key to be released before dissolving the window.

Hope this helps!

Comment only
03 Jul 2015 Hakan

Hakan (view profile)


This seems like a great piece of code which could help me a lot.

However, I was unable to figure out how to press "F4" (or any other "F.." button for that matter)

Can you tell me how I can do that. I don't quite understand how I can add new keys.

22 Jul 2014 Phillip

Phillip (view profile)

Oh yes, I meant hidden windows. It seems that some applications have all sorts of sub-windows or "Children" floating around behind the scenes.

I have another submission called "textInject" which you may want to look at. You specify the name of the main window for the application, and some (x,y) co-ordinates in pixels within that window. Then it uses a Windows API function called ChildWindowFromPoint to obtain a handle to the Child window at that location. The downside is that it cannot handle Alt/Ctrl keys etc, as I used a different function which makes it more practical to type text (SendMessage as opposed to SendKey). But I can probably revise this way of doing things if needed.

Comment only
22 Jul 2014 Laura

Laura (view profile)

hmm, I don't think it's got multiple windows, unless they're hidden, but the application does have several tabs in the main window, so maybe the tabs affect the naming?

Comment only
21 Jul 2014 Phillip

Phillip (view profile)

Hi Laura,

Thanks for the feedback :)

I suspect that maybe the application you are using has multiple windows, and that the named window you are referring to must relay keystrokes that it receives to some other window. And this other window would be the foreground window... So the "GetForegroundWindow()" check would always fail, but the text will be relayed to the desired window anyway.

If you cannot see what the other window could be called, if you would like I can probably make a separate utility to return the name of the current foreground window, so that you can use that name with KeyInject.

Comment only
18 Jul 2014 Laura

Laura (view profile)

Thanks, this was extremely useful! The only issue that I ran into was that I kept getting the error message 'Desired window needs priority'. Looking at the code, I think this means that the program I was writing to (a third-party .exe that won't take command line arguments) refused to stay as the Foreground Windows, even when I wasn't clicking away from it. The workaround that I found was just to comment out the if statement in the KeyInject-sendKey file that checks the Foreground window. I'm not really familiar with C++, so I was wondering if someone could suggest a better way to handle this?

12 Nov 2013 Mario

Mario (view profile)

Works just as described. Very useful script. Helped me tremendously. Thanks!

Comment only
12 Nov 2013 Mario

Mario (view profile)

31 Jan 2013 1.1

I updated the description of how to compile the source code, as I realised that the Matlab LCC compiler will not work because this submission uses C++.

Contact us