Prev | Current Page 333 | Next

Matthew MacDonald

"Pro WPF with VB 2008: Windows Presentation Foundation with .NET 3.5"

To hide or
disable a control, you set the Visibility and IsEnabled properties, respectively.
Getting Key State
When a key press occurs, you often need to know more than just what key was pressed. It??™s
also important to find out what other keys were held down at the same time. That means you
might want to investigate the state of other keys, particularly modifiers such as Shift, Ctrl, and
Alt.
The key events (PreviewKeyDown, KeyDown, PreviewKeyUp, and KeyUp) make this information
easy to get. First, the KeyEventArgs object includes a KeyStates property that reflects
the property of the key that triggered the event. More usefully, the KeyboardDevice property
provides the same information for any key on the keyboard.
Not surprisingly, the KeyboardDevice property provides an instance of the
KeyboardDevice class. Its properties include information about which element currently
CHAPTER 6 n DEPENDENCY PROPERTIES AND ROUTED EVENTS 174
has the focus (FocusedElement) and what modifier keys were pressed when the event
occurred (Modifiers). The modifier keys include Shift, Ctrl, and Alt, and you can check their
status using bitwise logic like this:
If (e.KeyboardDevice.Modifiers And ModifierKeys.Control) = _
ModifierKeys.Control Then
lblInfo.Text = "You held the Control key."
End If
The KeyboardDevice also provides a few handy methods, as listed in Table 6-6.


Pages:
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345