Winforms disabling keyboard input on textbox but keeping textbox enabled

后端 未结 2 1806
醉话见心
醉话见心 2021-01-21 08:27

The scenario is like that;

I need to disable textbox to take input from user by keyboard. But textbox should take inputs using kind of devices

相关标签:
2条回答
  • 2021-01-21 09:06

    You said you'll need to support any barcode reader, which usually emulate a keyboard, therefore there probably isn't an easy way to programmatically distinguish between input from a keyboard and a barcode reader.

    Your idea about using timing is a good one, although I'd do something slightly differently. I'd add a Timer to the Form and start it when the first character is entered into the TextBox. The timer should be set to a very short time span and should have auto-restart disabled. When the timer goes off, check if the TextBox has a valid barcode, and if does, process it. Either way, clear the text box afterwards.

    It would then appear to anyone attempting to use the keyboard that their typed text simply disappears, while a barcode scanner (which 'types' very fast) would still work.

    0 讨论(0)
  • 2021-01-21 09:06

    Maybe you need to track 'keyboard press' event? I don't think what automatically pasting something will trigger 'keyboard press' event.

    0 讨论(0)
提交回复
热议问题