Key Detection in KeyUp Event

I have a text box in the form where I try to find the keys that the user enters. TextBox has multi-line text with wordwrap. I donโ€™t want the user to press the enter key (since I want all the text to be entered on ONE line, wrapped up), so I used the following code:

private void txtPlain_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == (char)13) { MessageBox.Show("Enter keys are not allowed"); e.KeyChar = (char)0; } } 

This worked fine in my tests, but when I tested CTRL + ENTER, it did not work, as I am not sure how to determine the control key. From my googling, I found that I need to use KeyUp / Down events, so now I have the following code:

 private void txtPlain_KeyUp(object sender, KeyEventArgs e) { //if (e.KeyData == (Keys.Control | Keys.Enter)) { if (e.KeyCode == Keys.Enter || (e.KeyCode == Keys.Enter && e.Control)) { MessageBox.Show("Enter keys are not allowed:"); //e.KeyValue = Keys.None; } } 

The first commented line did not work for some reason, so if someone could explain why it would be useful.

The problem with the KeyUp / Down event is that I donโ€™t know how to DELETE an input key from text - unlike the KeyPress event, when I can set KeyChar to zero. The event captures the Enter and Ctrl + Enter keys, but the cursor still moves to the next line in the TextBox.

Thanks for any help with this.

+4
source share
3 answers

Hmm, there is no reason to deny the Enter key when handling KeyDown or KeyUp events. You can simply set the AcceptsReturn property of the text field control to False. This will prevent the multiline text field from responding to the Enter key.

Of course, this does not solve the Ctrl + Enter problem. Actually, the expected way to create a new line is when the AcceptsReturn property AcceptsReturn set to False. To solve this problem, you will need to handle one of the keyboard events and not allow the control to receive this input.

KeyDown is a good place to start. What you want to do is filter out any keyboard events that include the Keys.Enter flag. This will catch them no matter what other modifier key they can be combined. Then, as soon as you find Enter, you want to set the e.Handled property to True so that it is not passed to the control.

But, unfortunately, we have not quite done it yet. The text box control tries to process certain keys inside, and you cannot override this in the key event handler method. You also need to tell the control not to interpret this particular key as an input key. There are two main ways to do this. The first (and recommended way) is to inherit the TextBox base class to create your own custom control, and then override the protected IsInputKey method. The second (somewhat simpler) way is to handle the PreviewKeyDown event and set the IsInputKey property to False.

Code example:

 private void txtPlain_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) { // Check if the KeyCode value has the Keys.Enter flag set if ((e.KeyCode & Keys.Enter) == Keys.Enter) { // Set the IsInputKey property to False e.IsInputKey = false; } } private void txtPlain_KeyDown(object sender, KeyEventArgs e) { // Check if the KeyCode value has the Keys.Enter flag set if ((e.KeyCode & Keys.Enter) == Keys.Enter) { // Show the user a message MessageBox.Show("Enter keys are not allowed in this textbox."); // Prevent the key event from being passed on to the control e.Handled = true; } } 

And, although I assume this is for testing purposes only, you definitely want to accept the MessageBox call from here for production code. Find another way to warn the user that their input was not allowed, for example, a short beep and the ErrorProvider component located next to the text box. The display of the message box is very sharp and not very user friendly. See my answer here for other tips and tricks.

+4
source
 private void txtPlain_KeyUp(object sender, KeyEventArgs e) { //if (e.KeyData == (Keys.Control | Keys.Enter)) { if (e.KeyCode == Keys.Enter || (e.KeyCode == Keys.Enter && e.Control)) { MessageBox.Show("Enter keys are not allowed:"); //e.KeyValue = Keys.None; // mark event as handled e.Handled = true; } } 

from msdn link

change

I think you need a key down event, not a key

EDIT2
here is some tested code and it works the way you like:

  bool invalid=false; private void textBox1_KeyDown(object sender, KeyEventArgs e) { if ((e.KeyCode & Keys.Enter) == Keys.Enter) { invalid = true; } } private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { if (invalid) { e.Handled = true; } invalid = false; } 
+1
source

The first commented line did not work for some reason, so if someone could explain why it would be useful.

You wanted to detect Ctrl + Enter. if (e.KeyData == (Keys.Control | Keys.Enter)) {..

Keys.Control and Key.Enter are none other than some values see . Now executing a logical or not necessarily lead to a keystroke. Completely illogical article.

Now we turn to your actual problem that you want to detect. Enter a stroke and Ctrl + Enter to handle as the same. In addition, you want to cancel the newline character that was entered. Try

PreviewKeyDown or Preview key up event handler with the following condition

if (e.KeyCode == Keys.Enter)

Let me know if this works.

0
source

All Articles