[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More


How to control devices on USB with keyboard interface.

Published on
9,838 Points
Last Modified:
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR\Start" from 3 to 4. This procedure is also used by many system admins to disable USB ports within their purview to prevent corporate employees from coping sensitive corporate data. However keyboard interface devices cannot be disabled. The only way to disable them is through BIOS.

There are many devices like Barcode scanner, Magnetic card readers etc. which use the USB port to send data to the computer. Since these devices use the keyboard interface (act like a keyboard), it is by design that MS windows will not allow them to be disabled. However it sometimes is very crucial for an application developer to control these devices. Thus disabling the USB port from BIOS is not at all an agreeable solution. Windows uses messaging system to send events to applications. Messages like key down, key up etc are send by windows to the active window's application. If the application which is intended to receive data is not the active window, data from these kind of devices will fail to reach the application. Thus the application will fail to behave as intended.

The solution to the problem is to write a global keyboard hook. With a global keyboard hook immaterial of the application having the active window the keys or the data is first received by the hook. The hook has the ability either to let the data proceed to the active window's application or redirect to our own application. We now check the time difference between each WM_KEYDOWN message. When the time difference is very small say 30ms, credibility is that the operation is carried out by a device. If the time difference is high it is certainly from the keyboard. With this kind of approach our application can have full control over the USB device.
1 Comment

Author Comment

Hi stone5150,

Featured Post

Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Join & Write a Comment

This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Learn the basics of Skype For Business in office 365
Other articles by this author

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month