Reading Peripheral Devices With Inactive LabVIEW Window

Updated Jul 11, 2024

Environment

Software

  • LabVIEW

In this tutorial, you'll learn how to read the status of your peripheral devices, such as your keyboard or mouse, while interacting with another application on your computer (i.e., when the LabVIEW application window is inactive).

In LabVIEW, events (those defined in an Event Structure) are designed to respond to user interactions with the LabVIEW Front Panel. When you switch to another application or window, LabVIEW stops receiving these events because it is no longer the active application. For example, if you define an event in an Event Structure to detect when the mouse is clicked, LabVIEW will stop receiving this event when you interact with another application because it is no longer the current active window.

This is standard behavior for most windowed applications on operating systems like Windows. Simultaneously receiving low-level input events can cause conflicts and resource contention, so it is generally not recommended. However, it is possible to achieve this through DLLs.

Check the following example to learn how to implement this in LabVIEW.

You need to use a DLL that runs continuously to monitor the status of your peripheral device. In this example, you will learn how to monitor when the Up key is pressed on your keyboard.
Note: If you don't already have a DLL, you can generate a DLL from your LabVIEW Project where you read the status of your peripheral device.
 

  1. Identifying the DLL: The DLL that reads the status of the keyboard is user32.dll (a 32-bit DLL which is included in Windows Operating System, so it only works for the 32-bit version of LabVIEW). It is usually located at C:\Windows\SysWOW64\user32.dll.
  2. Configuring the DLL: In this case, we use the Call Library Function Node (refer to Related Links to see the available options to call DLLs in LabVIEW) to call user32.dll, where we need to specify the key whose status you want to get. For the Up key, you need to specify the number 38.
  3. Place the configured Call Library Function Node inside a While Loop to continuously run the DLL and retrieve the status.
kv.png
Note: This image is a LabVIEW snippet, which includes LabVIEW code that you can reuse in your project. To use a snippet, right-click the image, save it to your computer, and drag the file onto your LabVIEW diagram.
 

If you run the application, you will get the status of your peripheral device continuously because LabVIEW retrieves the information by calling the DLL continuously, not from an event defined in LabVIEW.

The LabVIEW program reads the status of your peripheral device every 100 milliseconds, regardless of whether the LabVIEW window is active. This is because the DLL operates independently of LabVIEW events, which only interact when the LabVIEW application is active.