LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to detect the state of a key (Caps Lock) on keyboard?

I tried attached code but LabVIEW either crash or hanging for ever. I did try following method but no luck.

 

In LabVIEW, you can call functions from the User32.dll (or any other Windows DLL) using the Call Library Node. Here are the steps to call the GetKeyState function from the User32.dll to detect the Caps Lock state:

  1. Create a Call Library Node

Right-click on the block diagram and select "Functions>>Select a VI..." from the pop-up menu. In the "Select a VI" dialog, navigate to "Programming>>Libraries & ExecutableNodes" and select the "Call Library Node". Click on the "Open" button to place the Call Library Node on the block diagram.

  1. Configure the Call Library Node

Double-click on the Call Library Node to open its configuration dialog. In the "Call Library Node Properties" dialog, click on the "Browse..." button next to the "Library file or path" field and navigate to the location of the User32.dll (typically C:\Windows\System32\User32.dll). Select the User32.dll file and click "Open".

  1. Specify the Function to Call

In the "Call Library Node Properties" dialog, click on the "Browse..." button next to the "Function name" field. This will open the "Browse Functions" dialog. In this dialog, scroll down and select the "GetKeyState" function. Click "OK" to close the "Browse Functions" dialog.

  1. Configure the Function Parameters

The GetKeyState function takes an integer parameter representing the virtual key code for the key you want to check. You need to pass the virtual key code for Caps Lock, which is VK_CAPITAL (0x14).

In the "Call Library Node Properties" dialog, expand the "Parameters" section. For the first parameter (which should be named "nVirtKey" or similar), enter the hexadecimal value 0x14 (or the decimal value 20) in the "Value" field.

  1. Configure the Return Value

The GetKeyState function returns a short integer value indicating the state of the specified key. In the "Call Library Node Properties" dialog, expand the "Return Value" section and set the "Return Type" to "Int16" (signed 16-bit integer).

  1. Close the Configuration Dialog

Click "OK" to close the "Call Library Node Properties" dialog.

  1. Check the Caps Lock State

The Call Library Node will now execute the GetKeyState function and return the state of the Caps Lock key as a signed 16-bit integer value.

To check if Caps Lock is on, you can use a Case Structure or other conditional logic to check if the least significant bit (bit 0) of the returned value is set (non-zero). If bit 0 is set, Caps Lock is on; otherwise, it's off.

Download All
0 Kudos
Message 1 of 10
(1,730 Views)

Hi Drum,

 

did you try to use the "stdcall / WINAPI" calling convention to call functions from WIN API?

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 2 of 10
(1,724 Views)

I created a version of this that uses .NET instead of the much more irritating to configure "Call library function" nodes.  This is it for all "lock" keys, not just Caps Lock:

 

 

Check lock key statuses.png

 

VI attached with 2018 version.

Message 3 of 10
(1,710 Views)

@Kyle97330 wrote:

I created a version of this that uses .NET instead of the much more irritating to configure "Call library function" nodes. 


Don't mean to hijack the thread, but do these .NET functions not need to be closed? Is there a reference leak if you don't close?

 

If they don't need to be closed, how do you tell? Sorry, I use quite a bit of the .NET functions posted on this site and am not a .NET guru like yourself.

Message 4 of 10
(1,681 Views)

The property node and the invoke node there are both "static" nodes.  That means they are not referenced to a specific object when they run (they are members of a class, yes, but don't operate on specific members of that class).  They also don't output any objects (just primitive data types) so nothing they output needs to close either.

 

If you wired up something to the top object outputs of those it would output a bad (null) reference so it couldn't close it.

 

You generally need to run a "close" on anything you create with a constructor node, or on any output of the bottom portion of a property node or invoke node that is an "object" of some kind instead of just primitive data.  

 

Note that the "Close reference" function in LabVIEW just tells LabVIEW that it is done with it and might not necessarily be a complete operation.  Many objects in .NET will have their own "Close" or "Destroy" or "Dispose" method that you should run first, then follow that up with the LabVIEW "Close reference" function.

Message 5 of 10
(1,662 Views)

Would you please give me LV2017 version ? Or show me the steps to config or select "Key" and "Controls" in .NET ?

0 Kudos
Message 6 of 10
(1,652 Views)

"Keys" is created by making a property node, then right-clicking to Select Class -> .NET -> Browse and choose "System.Windows.Forms", then expand the System.Windows.Forms namespace down to "Keys".

 

Kyle97330_0-1710885918962.png

To get "Control" you do the same thing but with an Invoke node, same assembly and namespace.

Message 7 of 10
(1,628 Views)

Here is my final version and I tested it works for me. If you see anything else I missed please let me know, thank you so much !

0 Kudos
Message 8 of 10
(1,603 Views)

Actually the goal is turn off  CapsLock if it is on. I cannot find a way to turn it off in property node. Please advise, thank you so much !

 

BigDrum_0-1710894975942.png

 

0 Kudos
Message 9 of 10
(1,581 Views)

@BigDrum wrote:

 I cannot find a way to turn it off in property node. Please advise, 

 

 


Just press it programmatically and then depress, something like this:

CapsLock.png

 

Andrey.

PS

In your initial VI (if you prefer to call GetKeyState) was two troubles — calling convention and returned value, which is not input param, of course.

This is how it should be, then it works:

GetKeyState.png

Message 10 of 10
(1,542 Views)