Capturing Key Events in Silverlight

Background: I have an application where I want to be able to record keyboard events. On the main screen, the user clicks the Start button. During game play, the Start button disappears and the Reset button appears (changes visibility on two controls). These are the only two standard controls, as everything else is non-concentrating user controls.

Problem. When the user presses the Start button (thereby giving focus), then it hides and Reset appears, the Reset button automatically appears. This is not terrible as key press events are still coming up, but focus is a big problem. If the user presses Space, the button interprets it as a Click event.

If I add a handler for KeyUp and KeyDown to the button itself and set the event to Handle, it swallows the event if I hold the space and release it, but not if I just touch it.

Question: How can I prevent Space from activating a button while still being able to respond to space in my application?

+2


source to share


2 answers


Consider making a simple custom control or something that you can set to IsHitTestVisible="True"

on. Then place it somewhere within the same grid and connect to its key events and other input events you want to capture.

To make sure that the focus goes to your "invisible" control, you can call it (yourControlsNameHere).Focus()

after clicking the "Start" button.



You could work with a visual / tab order to place your hidden focus control before or after the Reset button. I don't think your users would mind that including an app in an app might lead them to focus on sloppy user controls with which their input is correlated.

+1


source


You can set the IsTabStop property of both buttons to false (for example, when you click the Start button). Thus, they will fail to gain focus and will never handle keyboard events.



http://msdn.microsoft.com/en-us/library/system.windows.controls.control.istabstop%28VS.95%29.aspx

0


source







All Articles