File Exchange

image thumbnail


version (4.22 KB) by Phillip
Send key strokes to any window from within Matlab


Updated 31 Jan 2013

View License

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 ''

Cite As

Phillip (2020). KeyInject (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (20)


Hi Zaid,

You will need to "hold down" the shift key, which is achieved with the string "SHIFT_+".

"SHIFT_+;" to get a colon
"SHIFT_-" to release the shift key (a brief pause between the commands can prevent glitches)

If still not working it could be due to some weird keyboard driver issue. But you could still potentially solve the problem by using my other submission "textInject".

Hope this helps.

Zaid Saiyed

I wanted to add the colon (:) key as a new key. I was able to add the key itself but every time I use it, it prints out the semicolon (;) which is also on the same physical key (US keyboard). I tried using the SHIFT__ command but still it only prints out the semicolon. Is there a workaround to getting the colon (:) to print?


Hi Dan,

You will have to play around with the physical buttons on your keyboard to figure out what will work, but I usually find that if programs are not looking for an Enter press they will respond to the Spacebar. It could also be a particular letter they have included as a keyboard shortcut, e.g. 'C' for Connect. And sometimes it will be some ridiculous shortcut only discoverable from the manual (e.g. some function key).

Let me know if you get stuck adding custom keys :)

Great program! One snag I ran into... when I am toggling through the menu for a camera, there is a button to "connect" to the camera... if I press the enter key, it skips to the next field, and does not connect to the camera. The program only seems to respond to mouse clicks... any idea how to use the program to select the button to connect to the camera? Thanks again!

Great program. Works like charm


Great there is an app for that!
Though I have problem installing it (on windows 10). Compiling the '' worked perfectly fine, but when executung mex '' I get the following result:
lcc preprocessor warning: No newline at end of file
Error 20 undeclared identifier `INPUT'
Warning 20 Statement has no effect
Error 20 syntax error; found `ip' expecting `;'
Error 20 undeclared identifier `ip'
Warning 20 Statement has no effect
Error 21 left operand of . has incompatible type `int'
Error 21 undeclared identifier `INPUT_KEYBOARD'
Error 22 left operand of . has incompatible type `int'
Error 22 left operand of . has incompatible type `int'
Error 23 left operand of . has incompatible type `int'
Error 23 left operand of . has incompatible type `int'
Error 24 left operand of . has incompatible type `int'
Error 24 left operand of . has incompatible type `int'
Error 27 left operand of . has incompatible type `int'
Error 27 left operand of . has incompatible type `int'
Error 32 left operand of . has incompatible type `int'
Error 32 left operand of . has incompatible type `int'
Error 35 left operand of . has incompatible type `int'
Error 35 left operand of . has incompatible type `int'
Error 41 left operand of . has incompatible type `int'
Error 41 left operand of . has incompatible type `int'
Error 47 left operand of . has incompatible type `int'
Error 47 too many errors

F:\MATLAB\MATLAB\BIN\MEX.PL: Error: Compile of '' failed.
Does anyone know how to get this to work?


It's great!!
Works so fine!



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...

Todd Karin

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


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!



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.


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.


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?


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.


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?


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



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++.

MATLAB Release Compatibility
Created with R2012b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired by: TextInject

Inspired: Interactive link between MATLAB and ANSYS