G
G
Grande2014-09-08 12:33:30
WPF
Grande, 2014-09-08 12:33:30

How to pause event tunneling in WPF?

Good afternoon. In a WPF project, it became necessary to work with a barcode scanner, but the problem is that the scanner works through a keyboard break. You understand that it is problematic to distinguish input from a scanner from user input. I'm trying to solve this problem like this:
1) I hang a handler on the PreviewTextInput window event (you need to scan regardless of which element is in focus)
2) After entering the character, we look to see if the timer is running (Stopwatch), if not, remember the character and start it, if so, stop . We look at what interval it turned out (if it is less than 20ms, then we add a character to the buffer, and start the timer on a new one, and set the Handled property of the TextCompositionEventArgs object to true). Thus, the timer will serve as an indicator that the system is waiting for the next character to be entered.
There is a problem - the first entered character (before the timer counts down) how to count it? There was an idea to wait 20 ms and if the timer is still running, stop it and read the character entered from the keyboard and display it on the screen. The question is - how to suspend further execution of the event in order to wait for this interval? starting the second timer (which is the DispatcherTimer) doesn't help, as it appears to be started by a separate thread.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sumor, 2014-09-08
@Grande

In your case, it is better to use the scanner in com-port mode.
Or change the logic of the program so that the scanner simply replaces the keyboard, as is done, for example, in retail outlets - there you can enter data from the scanner, or you can fill it with your hands.
As for the entered characters, you can remember them all and not skip everything, and after you figure out what to do with them, you can emulate data input via SendInput. But this path is ugly and leads to errors, especially when using special characters or keyboard shortcuts.

A
Alexander Vishnyakov, 2014-12-27
@asvishnyakov

In general, of course, the solution to the problem is reminiscent of the Goldberg machine, and intuition suggests that there should be a more elegant solution. But if you are still interested in how to stop the tunneling of an event, then this is done using the Handled property .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question