How to control devices on USB with keyboard interface.

Published on
9,724 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

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

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…
Key to your CPU's ability to stay cool is to use the right amount of thermal paste and apply it correctly. In other words you want as much thermal conductivity between CPU and the cooling block. Use a quality thermal paste and apply it in a manner…
Other articles by this author

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month