Code covered by the BSD License  

Highlights from
KeyInject

5.0

5.0 | 2 ratings Rate this file 15 Downloads (last 30 days) File Size: 4.22 KB File ID: #40001
image thumbnail

KeyInject

by

 

25 Jan 2013 (Updated )

Send key strokes to any window from within Matlab

| Watch this File

File Information
Description

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'

Acknowledgements

Text Inject inspired this file.

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 (6)
22 Jul 2014 Phillip

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.

22 Jul 2014 Laura

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?

21 Jul 2014 Phillip

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.

18 Jul 2014 Laura

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

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

12 Nov 2013 Mario  
Updates
31 Jan 2013

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