From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Detect Keyboard Programmatically

Solved!
Go to solution

When my vi starts running, I would like to detect if a keyboard is connected to my computer.  I can't be the only one, but after a lot of searching I could find nothing.  Maybe I'm just dumb and everyone else knows how to do this...  If some smarter person would be kind enough to enlighten me, I would greatly appreciate it!

0 Kudos
Message 1 of 6
(3,138 Views)

There is a primitive that *may* work under Connectivity -> Input Device Control -> Query Input Devices. Unfortunately, it only seems to detect my laptop's built-in keyboard, not the external USB one. It may help or it may not. I did find some .NET stuff that seems like it'll work. I gave it a few minutes but couldn't get it working, but I think I'm close. I'm no .NET guy, so I'm stumbling a bit with syntax, which I think is my main problem. Some further reading- be sure to post a solution when you find it 🙂

 

How to do it with .net:

https://stackoverflow.com/questions/34514847/programmatically-detect-if-mouse-and-keyboard-are-plugg...

https://docs.microsoft.com/en-us/dotnet/api/system.management.managementobjectsearcher?view=netframe...

 

How to use .NET in LV:

https://forums.ni.com/t5/Developer-Center-Resources/Calling-NET-Assemblies-from-LabVIEW/ta-p/3523180

https://forums.ni.com/t5/LabVIEW/How-to-access-WMI-Classes-via-NET-in-LabVIEW/td-p/253845

 

Some combination of those links should get you where you need to go.

 

Message 2 of 6
(3,124 Views)
Solution
Accepted by topic author JohnEE

Thanks, Bert!  You pointed me right to the solution.  I'm not a .net guy either, but I've worked with it a little bit through LabView. 

I had tried the Connectivity -> Input Device Control -> Query Input Devices route; I have a Logitech mouse and keyboard with their Unifying receiver.  The Query Input Devices block tells me that I have one mouse and one keyboard, not matter if I add extra keyboards or remove the keyboard; so I deemed it not reliable.  The .NET function you identified did the trick.  I wrote a .vi to output the count of the keyboards (or mice) that I have attached.  The count is not necessarily correct; with just a USB keyboard it correctly reports "1," bit with my wireless keyboard it reports "3."  With both connected it reports "4."  But, with them both disconnected it correctly reports "0," which is what I need.

0 Kudos
Message 3 of 6
(3,083 Views)

This should detect USB keyboards.

"If you weren't supposed to push it, it wouldn't be a button."
0 Kudos
Message 4 of 6
(3,072 Views)

Hi Paul,

When I tried running it I got an error:

image.png

0 Kudos
Message 5 of 6
(3,059 Views)

@JohnEE wrote:

Hi Paul,

When I tried running it I got an error:

 


The error handling is a bit limited and the code simply enumerates all PNP devices and tries to read its Service property which not all PNP devices have. And if one of them doesn't have such a property, which is very common on not so new Windows installations, you get this error.

 

When I run this code I get 174 device records as result, which all are checked for that property and about half of them don't have it.

Rolf Kalbermatter
My Blog
0 Kudos
Message 6 of 6
(3,016 Views)