How Can I Monitor a Joystick, Keyboard, or Mouse in LabVIEW?

Updated Oct 26, 2023

Environment

Software

  • LabVIEW

  • I need to acquire data from my joystick/mouse/keyboard in LabVIEW
  • I need to monitor my input devices in LabVIEW
  • How can I know which keys are pressed in my keyboard?
  • I want to poll for mouse clicks, keystrokes or joystick movements.

To monitor these devices in LabVIEW you can use the built in Input Device VIs. These VIs allow you to monitor attached keyboards, mice, and joysticks. These VIs are found on the Connectivity>>Input Device Control palette. The following example walks through how to read from a joystick input device, and the same process applies to mice and keyboards.
 
  1. Call Initialize Joystick.vi. In this VI, use the device index input to identify the device to be read from. This is a numeric value that corresponds to the device number given by the operating system. Use the corresponding Initialize VI if doing this for a different input device.
  2. Wire the device ID output from the Initialize Joystick.vi to the device ID input of Acquire Input Data.vi. For the joystick, this VI will give information such as the axis information, button status, and directional information that corresponds to the operations on the joystick or gamepad. This VI is polymorphic and will also return information for a mouse or keyboard if such a device ID is passed to it.
    1. Axis info: This cluster of numeric controls returns the joystick's rotating inputs.
    2. Button info: This cluster of boolean controls gives you the status of each one of the buttons in the joystick. Please note that they might not map the same way as they appear in your joystick.
    3. Direction info: Use this output for the joystick's Point of View (also called Point of View Hat) inouts.
  3. End with Close Input Device.vi. Wire the device ID output from the Acquire Input Data.vi to the device ID input of the Close Input Device.vi. This VI should be called at the completion of reading to close the reference to the device.
 

 
Note: 
This method will work with Windows and Linux operating systems, but are not supported on MAC OS X.

Additional Information

Examples

 

Embedded Targets

If you would like to use the joystick to control a CompactRIO (cRIO), you will need to run the Joystick VIs on a host computer due to using external code that would not run on a cRIO system. This data could then be moved to the cRIO using a shared variable or alternative communication method depending on the particular application.