Mtok
Mtok

Reputation: 1630

Winforms disabling keyboard input on textbox but keeping textbox enabled

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 like barcode reader. I thought to hold a timer and take the timespan between two key strokes (not clear yet). But maybe there is a property or smarter algorithm for that ?

p.s. it is a windows forms application.

Upvotes: 3

Views: 1806

Answers (2)

Allon Guralnek
Allon Guralnek

Reputation: 16121

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.

Upvotes: 4

Vjatseslav Gedrovits
Vjatseslav Gedrovits

Reputation: 1191

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

Upvotes: 0

Related Questions