• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1207
  • Last Modified:

how to detect type device scanner or keyboard ?

hi
i want design form these form accept number for symbols scanners barcode only   if any one   type from normal keyboard  or copy past number not accept
how to detect type device scanner or keyboard  ?
  form
0
mahmuod
Asked:
mahmuod
  • 2
  • 2
2 Solutions
 
BembiCEOCommented:
Every device has a HardwareID, if you search your registry for HardwareID you will find all of the installed devices. The HardwareID contains the vendor, a device type as well as a unique identification of the device itself. So You form has to indentify the HardwareID of the input device to decide, if it is allowed or not.

In the internet your find databases to identify a device by its hardwareID
0
 
mahmuodAuthor Commented:
ok but how to make it by c#
0
 
Jacques Bourgeois (James Burger)Commented:
Check the events that are triggered when the scanner sends it information to the application. This can change depending on the scanner.

If the scanner sends only a KeyPress or a TextChanged and no KeyDown, then simply set e.SuppressKeyPress=True in the KeyDown event of the control. This will prevent a key from the keyboard from going to the control.

If this does not work with you scanner, you might want to use a timer to calculate the time it takes to type fill the control. Manual entry is usually a lot slower than the scanner, so with some tests, you should be able to come with a duration that will tell you if the scanner or the keyboard is sending the data. If the timing between the first character and the second one is too long, delete the first character and display a warning message.
0
 
mahmuodAuthor Commented:
The scanner action same keyboard . Can not select  type keyboard or scanner
But if the code only one deget can not calculate time
0
 
Jacques Bourgeois (James Burger)Commented:
Yes, you can calculate time with one digit.

Define a DateTime variable called lastTime at the Form level and use the following code in your KeyPress event:

		private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (lastTime>new DateTime()) // Do not react to the first character
			{
				if (DateTime.Now.Subtract(lastTime).Milliseconds>50)
				{ // This is the user typing }
				else
				{ // This is the scanner }	
			}
			lastTime = DateTime.Now; // Initialize the timer for the first character
		}

Open in new window


I have set the trigger delay to 50 milliseconds because this is the fastest speed at which I can type when go on like crazy on the keyboard. If your scanner is faster than that, that should be OK. Otherwise, you may try to increment the value, since a normal person that types as it reads the code will be a lot slower. 100 milliseconds per character seems to be proper for a fast typist.

If the form is used to make repeated reads, be sure to reset lastTime to new DateTime() when a reading is complete, to reinitialize the whole thing for the next reading.
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now