3

Is there any way to proceed into a method if the key that is being pressed does not result in any typing. i.e. the shift key, control key etc without having to specify all of them. Ideally, to detect combinations of keys, for example Control+V = Paste.

Code similar to below is what I am working with;

    if( (e.KeyData == Keys.Left) 
        || (e.KeyData == Keys.Right) 
        || (e.KeyData == Keys.Home) 
        || (e.KeyData == Keys.End) 
        || (e.KeyData == Keys.Up) 
        || (e.KeyData == Keys.Down)
        || (e.KeyData == Keys.ShiftKey)
        || (e.KeyData == Keys.ControlKey)
        ) return;

But don't want to add every single combination of keypresses.

Any ideas?

Josata
  • 173
  • 2
  • 8

4 Answers4

4
protected override bool ProcessCmdKey( ref Message msg, Keys keyData )
{
  if ( keyData == (Keys.Control | Keys.V) )
    return true;
  else
    return base.ProcessCmdKey( ref msg, keyData );
}

That takes care of copy+paste. You can override OnKeyPress as well and use the Char.IsDigit and/or Char.IsLetter (or Char.IsLetterOrDigit) if needed. You get the idea, I don't think that a regular expression is warranted here as some others have suggested.

Ed S.
  • 122,712
  • 22
  • 185
  • 265
2

The Keys value contains bit combinations for all keys pressed... This means it can have more than one value at a given time. Try this:

if ( (keyData & Keys.Control) == Keys.Control && (keyData & Keys.V) == Keys.V) 
{ 
// Ctrl+V was pressed! 
} 

So, that'll detect combinations of keys... BUT you are going to have to specify them :s

Check this out: http://www.go4expert.com/forums/showthread.php?t=713

Faizan Kazi
  • 545
  • 1
  • 10
  • 18
  • How about if( keyData == (Keys.Control | Keys.V). Less verbose at least. – Ed S. Nov 16 '10 at 04:30
  • @Ed even though it may serve, that's not exactly the same. Faizan's passes on Ctrl+V and Ctrl+Alt+V, while yours only passes on Ctrl+V. – R. Martinho Fernandes Nov 16 '10 at 04:39
  • Not sure what code you're looking at, but I see no check for ALT. I see individual checks for Control and V. – Ed S. Nov 16 '10 at 04:44
  • @Ed: but if Ctrl and Alt and V are pressed Faizan's checks pass because both Ctrl and V are pressed. Yours don't, because it's not exactly Ctrl and V that are pressed. Faizan's check for "at least", you check for "exactly". Not sure what the OP would prefer, though. Just pointing out the differences. – R. Martinho Fernandes Nov 16 '10 at 04:48
  • Good one Ed... quite useful for checking for exact values :) – Faizan Kazi Nov 16 '10 at 14:53
0

This answers the inverse of your question--"proceed into a method if the key that is being pressed does not result in any typing"--but might get at what you're actually trying to accomplish, rather than the method.

I would suggest using the TextChanged event handler on the TextBox/RichTextBox control. This is only fired when the text actually changes (whether from the user typing, or from code).

private void richTextBox1_TextChanged(object sender, EventArgs e) {
    // Handle the event.
}

If you want to know when it was the user that modified the content, you can use the Modified property of the text box.

if (richTextBox1.Modified) {
    // The user modified the contents of the text box.
}

This will likely be much more reliable than trying to handle the KeyDown, KeyUp, KeyPress, etc. events and figuring out from the keys that are pressed whether or not the text changed.

tgray
  • 8,826
  • 5
  • 36
  • 41
-1

Perhaps it will serve your purpose to use a regular expression, with a "character class" that indicates non-printable characters.

For example, the unicode "general category" for control characters might work ('Cc').

Brent Arias
  • 29,277
  • 40
  • 133
  • 234
  • Some events don't provide characters, but keycodes or "keystrokes". Ctrl+V happens to be a control character (aka ^V), but Ctrl+Alt+V is not. – R. Martinho Fernandes Nov 16 '10 at 04:26
  • 1
    Yeah, so many people jump to regular expressions when the problem is actually quite simple to solve in a more traditional way. – Ed S. Nov 16 '10 at 04:28