Example Code

Programmatically Simulate Key Strokes in LabVIEW

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Software

  • LabVIEW

Code and Documents

Attachment

Overview:

Simulating key strokes can be a useful tool.  Many tasks can be automated this way ranging from automatically writing a string to an indicator to filling out web forms programmatically by tabbing through and filling in fields.  This example includes low level VIs called KeyDown, KeyUp and KeyPress that correspond to those events.

 

 

Description:

KeyDown and KeyUp are useful for "holding" a key down while others are typed, for example "Shift".  KeyPress simply uses KeyDown then KeyUp and is useful for simulating typing, for example alphabetical characters.  These three low level VIs all leverage the Windows keybd_event function in the user32.dll by calling a Shared Library Node with different configurations basd on the "keybd_event" function parameters.                  

Also included is a higher level VI called TypeCharacters.  This VI takes as input a cluster that allows the user to pass in an array of keys to press along with the desired action and a delay.  Using this VI, it is easy to design a series of key presses that accomplishes your goal.  

 

 

Requirements:

LabVIEW 2012  (or compatible)

Windows OS

 

 

Steps to Implement or Execute Code:

Key Strokes Simulation.vi should run without any further configuration.  By default, it types the string "Hello".  You can alter this by changing the control cluster labeled Key Array.

  1. Download the attached folder and Unzip it to your computer.
  2. Open "Key Strokes Simulation.vi"
  3. Enter the keys in the Key Array control. It is what defines which keys are pressed and for how long. Note that the shift key is pushed down and up in separate steps and that the alphabetical keys are pressed. This is equivalent to holding the shift key down while pressing 'H', the result being an uppercase 'H'.
  4. Run this VI and 'Hello' will be programmatically typed in the Output box.

 

 

Additional Information or References:

 

Front Panel of Key Strokes Simulate.vi

 FP.PNG

                                           

Block Diagram of Key Strokes Simulate.vi

BD.PNG

 

 

Block Diagram of Type Characters.vi                    

TypeCharacters.png

 

 

 

 

**This document has been updated to meet the current required format for the NI Code Exchange. **

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
Link Butler
Member
Member
on

I need to simulate the key strokes to input a path-to-file string automatically. For examle: "C:\LabVIEW\longhorn\key_strokes.vi".

I used your code above, but this code doesn't have ASCII code for the characters: ":" colon, "\" back slash, and "_" underscore. Those characters are very important to complete a path-to-file string. I also need to simulate the pressing of Alt button.

Can you modify your code and put those ASCII character into your code?

Thanks.

Vi-Shaman
Member
Member
on

Michael,

You Genius!... just what ive been trying to do..... A very big help!... I owe you a pint!

stubon
Member
Member
on
SULLutions
Member
Member
on

Just what I needed. Thanks.

And stubon's link was very helpful in customizing to my current application.

SULLutions.com
...when a single discipline is not enough
kdharanya
Member
Member
on

Hi Link,

How did to manage to simulate "\",":" in labview?

I am facing same problem. I am trying to generate keypresses for a file path.

Kindly help

Regards,

Dharannya

AshKum
Member
Member
on

Excellent !! This is exactly what i have been looking for.

InfiniteNothing
Active Participant
Active Participant
on

Any idea why acquire input data doesn't see the key presses?

Key pressed2.png

CLED (2016)
dew4theq
Member
Member
on

These keystroke VIs work really well.  However, they do not seem to work at all if the computer you are running them on has no one logged into it.  For example, I am trying to write something that starts, configures, and runs and external program.  This works, but then I have my labview program running in the back ground monitoring a data file to see if it is growing.  I used this tactic because once I see the file is not growing anymore, then I know the external program is done doing its thing.  At this point, I would like to re-configure the external program and start another run on the external program.  This does not work at all because nobody is logged into the computer when it tries to do this reconfiguring.  Does anybody know a way around this?