Reputation: 1232
I have an application with a toggle button that, when toggled, will listen for text input from a bar code scanner. When the bar code scanner starts sending text input to the application, as soon as it sends a space character, the toggle button (which has keyboard focus) ends up becoming deselected.
I need to find a way to prevent my toggle button from being affected by keyboard input while still allowing the PreviewTextInput or TextInput events to fire.
If I handle the PreviewKeyDown event on the toggle button, I can stop the button from receiving the spacebar input by setting the e.Handled property to true, but that stops the PreviewTextInput and TextInput events from firing.
I've thought about trying to remove focus from the toggle button immediately after it's clicked, but that seems like too much of a hack. I'd also like to avoid sub-classing the toggle button. I've also considered setting the button to disabled and allowing it to be re-enabled with a mouse click, but that's not an elegant solution. I'm thinking there must be some mechanism that associates the space bar key with a button press, and if I could figure out how to disassociate those, I'd have a working solution, but I don't know how that works.
Any thoughts or ideas would be appreciated. Thanks!
Upvotes: 2
Views: 1982
Reputation: 1232
As suggested by Mannimarco, a good solution is to set the Focusable property on the toggle button to False.
Upvotes: 1
Reputation: 189
When the button is clicked set focus to a TextBox
control to capture the text.
Make the control's background and font the same color to hide the input.
You will have to catch LostFocus
event and set e.Handled = false;
while the toggle button is active.
Upvotes: 0