Question

This code shows hashcodes of pressed button, but when i press on Control button i does not do anything. Can help me someone?

 private void treeView1_KeyPress(object sender, KeyPressEventArgs e)
    {
       MessageBox.Show(e.KeyChar.GetHashCode().ToString());
    }

C# WinForms

Was it helpful?

Solution

Ctrl is a modifier key, so it doesn't generate keypress events in its own right.

Think of a keypress as an actual character that has been typed (e.g. "A"). In contrast, the modifier keys (Ctrl, Alt and Shift) don't usually "type" anything, they tend to work in conjunction with other keys to modifiy their effect (e.g. shift changes an "a" int an "A"; ctrl often changes "a" into the hotkey for "Select All", Alt may try to open the main menu (if any) that starts with the letter "A")

To use ctrl you would usually either check its state at the point where a normal key is pressed (e.g. when "A" is pressed, to determine whether the keypress means "A" or "ctrl+A"), or you might check the key state asynchronously (e.g. while dragging, checking if ctrl is held down to apply a modification to the drag such as constraining it to drag only in one axis, or to copy the files being dragged rather than moving them)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top