If you've ever wanted to programatically fire a keboard event then I have just the VI for you. This code makes a call to the Windows API calling a function in user32.dll available in all Windows versions. This function allows you to specify any key on the keyboard. When the VI is run, Windows will think the selected key has been pressed.
Looking at the function description available from MSDN we see the following:
keybd_event Function
--------------------------------------------------------------------------------
The keybd_event function synthesizes a keystroke. The system can use such a synthesized keystroke to generate a WM_KEYUP or WM_KEYDOWN message. The keyboard driver's interrupt handler calls the keybd_event function.
Syntax
VOID keybd_event(BYTE bVk,
BYTE bScan,
DWORD dwFlags,
PTR dwExtraInfo
);
Parameters
bVk
[in] Specifies a virtual-key code. The code must be a value in the range 1 to 254. For a complete list, see Virtual-Key Codes.
bScan
This parameter is not used.
dwFlags
[in] Specifies various aspects of function operation. This parameter can be one or more of the following values.
KEYEVENTF_EXTENDEDKEY
If specified, the scan code was preceded by a prefix byte having the value 0xE0 (224).
KEYEVENTF_KEYUP
If specified, the key is being released. If not specified, the key is being depressed.
dwExtraInfo
[in] Specifies an additional value associated with the key stroke.
Return Value
This function has no return value.
There are two inputs we need to be concerned about. The bScan and dwFlags. For the bScan we need to input the number which represents the key on your keyboard. This number is also available from MSDN. I won't display the complete list since you can retrieve it online from MSDN but I have included the complete list on the diagram of the VI.To make it easier during programming, I've created an enum control for all the possible keyboard values (0-254).
Another input of concern as mentioned above is the dwFlags. This must be toggled between 0 and 2 to indicate that a key is either depressed or released. This is especialy usefull with the ctrl or shift keys. I've created the following diagram and front panel for the above function.
The diagram:
As you can see i've used an enum to select between up and down key events. I do this so when this VI is called from another VI, the digram will be easier to read since the word "up" or "down" will be clearly visible instead of the number 0 or 2. This method should be used whenever multiple selection inputs are available. It allows for cleaner and more readable diagrams.
The DLL function call in LabVIEW is shown below:
One very important note about using this function. This function will only work on the VI that has it's front panel active. In other words, the keyboard events will only affect the active LabVIEW window.
LabVIEW 6.0 VI's:SimulateKeypress.zip
Support or questions on this code should be done via the forum.