How to control devices on USB with keyboard interface.

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.

Comments (1)


Hi stone5150,

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.

Get access with a 7-day free trial.
You Belong in the World's Smartest IT Community