How to check Ctrl key combination?

I cannot get the status of the Ctrl key in the KeyUp event handler when I release the Ctrl key.

Do I need to check the key code of the event argument?

Is there another way?

+4
source share
2 answers

Working with the event for the KeyUp event handler will work.

The following code runs when you release the Ctrl key:

 private void Form1_KeyUp(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.ControlKey) { MessageBox.Show("Control key up"); } } 


If you want to check whether Ctrl was pressed in combination with another keystroke, for example: Ctrl + F1 , the following code fragment may be required:

 private void Form1_KeyUp(object sender, KeyEventArgs e) { if (e.Control && e.KeyCode == Keys.F1) { MessageBox.Show("Control + F1 key up"); } } 


Note: you may need to enable KeyPreview on the form to catch all KeyPreview control events in one place.

+13
source
 private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { if(e.Modifiers == Keys.Control) ... } 
0
source

All Articles