03-19-2024 02:49 PM
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:
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.
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".
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.
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.
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).
Click "OK" to close the "Call Library Node Properties" dialog.
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.
03-19-2024 03:03 PM - edited 03-19-2024 03:04 PM
03-19-2024 03:35 PM
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:
VI attached with 2018 version.
03-19-2024 04:10 PM
@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.
03-19-2024 04:26 PM
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.
03-19-2024 04:44 PM
Would you please give me LV2017 version ? Or show me the steps to config or select "Key" and "Controls" in .NET ?
03-19-2024 05:08 PM
"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".
To get "Control" you do the same thing but with an Invoke node, same assembly and namespace.
03-19-2024 06:32 PM
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 !
03-19-2024 07:35 PM - edited 03-19-2024 07:36 PM
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 !
03-20-2024 12:08 AM - edited 03-20-2024 01:08 AM
@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:
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: