Texbox input, disable for keyboard, keep for barco

2020-05-09 03:54发布

问题:

I have made a Windows Form application with a textbox. I will force users to use the barcode scanner, so the keyboard input should be disabled. The KeyPressed event does not work, because is also disabled input from the barcode scanner.

I thought maybe I can set a timer on the TextChanged event, but I do not really know how it works.

Have someone a good idea?

回答1:

Based on your description, I assume that your barcode scanner is an HID barcode scanner. If so, there is no easy answer here because the barcode scanner functions exactly like a keyboard. Disabling keyboard input will disable the barcode scanner.

I have done this before, and there are a few solutions I know of:

  1. Use a barcode scanner that is not an HID barcode scanner. Ex: Serial, or something like that.
  2. Configure the barcode scanner to send a special character at the start and end of the barcode scan. (This is something you can do if you have the manual for the particular model of barcode scanner.) Write your KeyPressed event to listen for these special characters as a signal that it is getting data from the barcode scanner. When it sees them, enable and disable the text box.
  3. Use Windows API calls to communicate directly with the barcode scanner. This requires knowing the VID and PID of the barcode scanner, and some detailed knowledge of APIs like GetRawInputDeviceInfo, GetRawInputData, and RegisterRawInputDevices. This is very powerful, since you can receive barcode input from anywhere in the application, but it is very complex.

However, one caveat: You probably don't want to disable the text box at all. What if the barcode is damaged and the scanner cannot read it? You always want the user to be able to manually enter the barcode. Next time you go through the cash register at a store, notice that there is always a way to manually enter the barcode.



回答2:

You can find the way to check with keysUp and Keysdown event.

Check here for detailed answer.

Check here for answer

Check here for github code