File Exchange

image thumbnail

KeyInject

version 1.1 (4.22 KB) by

Send key strokes to any window from within Matlab

12 Downloads

Updated

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

Example:
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':
http://www.kbdedit.com/manual/low_level_vk_list.html
The hex values there need to be converted to decimal:
http://www.kbdedit.com/manual/low_level_vk_list.html
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 'keyInject_setFocus.cc'
mex 'keyInject_sendKey.cc'

Comments and Ratings (14)

Malion

Malion (view profile)

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

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

Daniel

Daniel (view profile)

It's great!!
Works so fine!

Daniel

Daniel (view profile)

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

Todd Karin

Todd Karin (view profile)

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

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!

Hakan

Hakan (view profile)

Hi,

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.

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.

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?

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.

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?

Mario

Mario (view profile)

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

Mario

Mario (view profile)

Updates

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

MATLAB Release
MATLAB 8.0 (R2012b)
Acknowledgements

Inspired by: TextInject

Inspired: Interactive link between MATLAB and ANSYS

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

» Watch video