how to detect type device scanner or keyboard ?

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  ?
Who is Participating?
Jacques Bourgeois (James Burger)Connect With a Mentor PresidentCommented:
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 }
				{ // 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.
BembiConnect With a Mentor CEOCommented:
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
mahmuodAuthor Commented:
ok but how to make it by c#
Jacques Bourgeois (James Burger)PresidentCommented:
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.
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.