S).
There??™s one more wrinkle. Depending on your Windows keyboard settings, pressing a key
causes the keystroke to be repeated after a short delay. For example, holding down the S key
obviously puts a stream of S characters in the text box. Similarly, pressing the Shift key causes
multiple keystrokes and a series of KeyDown events. In a real-world test where you type
Shift+S, your text box will actually fire a series of KeyDown events for the Shift key, followed by
a KeyDown event for the S key, a TextInput event (or TextChanged event in the case of a text
CHAPTER 6 n DEPENDENCY PROPERTIES AND ROUTED EVENTS 171
box), and then a KeyUp event for the Shift and S keys. If you want to ignore these repeated
Shift keys, you can check if a keystroke is the result of a key that??™s being held down by examining
the KeyEventArgs.IsRepeat property, as shown here:
If CBool(chkIgnoreRepeat.IsChecked) AndAlso e.IsRepeat Then
Return
End If
nTip The PreviewKeyDown, KeyDown, PreviewKey, and KeyUp events are best for writing low-level keyboard
handling (which you??™ll rarely need outside of a custom control) and handling special keystrokes, such
as the function keys.
After the KeyDown event occurs, the PreviewTextInput event follows. (The TextInput
event doesn??™t occur, because the TextBox suppresses this event.
Pages:
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339