I'm looking at a WPF example from a book where OnPreviewTextInput
is overridden in a class derived from Window
. The override checks the TextCompositionEventArgs.ControlText
string against the Unicode literal character '\u000F'
. This corresponds to the user pressing Ctrl+O.
The character '\u000F'
looks like a "magic" literal to me. How would I know what to check for if I wanted to check for other codes? Is there a more reader-friendly way to do this?
protected override void OnPreviewTextInput(TextCompositionEventArgs e)
{
// Ctrl+O
if (e.ControlText.Length > 0 && e.ControlText[0] == '\u000F')
{
// do stuff
}
}
As you have surmised, it is a "magic" literal. But there is some logic and history behind the magic. It corresponds to an old concept called Control characters (long read, only if you're interested).
For a quick reference of how 0x0F
corresponds to Ctrl-O
, see this table. Focus on the first column with the caret notation; you'll see that characters 1-26 (0x01
-0x1A
) maps Ctrl-A
to Ctrl-Z
. Char 15 (or 0x0F
) is your Ctrl-O
.
These old ASCII control codes were brought over to Unicode, retaining their mapping. Hence your '\u0000F'
.
If you've been on the internet long enough you'll see some dumb^H^H^H^Hawesome jokes that rely on the arcane knowledge that ^H
maps to the Backspace control character. You can actually try some of them. Fire up Notepad, and you'll see that Ctrl-I
and Ctrl-M
map to Tab and Enter correspondingly.