0

I simply want to get the character 1...n inside my function when the user presses ctrl +1....n in the form. if i use keypress event for this:

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
    if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
        MessageBox.Show("KeyPress " + Keys.Shift);
    if ((Control.ModifierKeys & Keys.Control) == Keys.Control)
        MessageBox.Show("KeyPress " + Keys.Control);
}

If I press Shift+1 I see the message "Shift pressed" on the screen. If I press ctrl+1, no message appears on the screen. For example, if I press ctrl+b, I see the "control pressed" message on the screen again this time. so ctrl doesn't work for combinations of numbers.

it can be said, use the keydown method, in that case the event is running as long as I hold down ctrl, which bothers me. I want to know is there a way to solve this with "keypress"?

note: I know how to do this with "keydown" :

if (Char.IsDigit(((char)e.KeyCode)) &&  e.Modifiers == Keys.Control)
{
     MessageBox.Show("KeyDown :" + ((char)e.KeyCode));
}
Mykyta Halchenko
  • 720
  • 1
  • 3
  • 21
  • Typing Ctrl+1 does not produce a character, and therefore doesn't trigger the KeyPress event. You must use KeyDown to detect it. It isn't clear what "bothers me" might mean. – Hans Passant Jun 02 '22 at 13:04
  • @Hans Passant even if you press only ctrl key for KeyDown Event, Event fired repeatedly. until you hit a second key. Maybe this is something very normal. but it still bothers us, we'll use it anyway. thanks for answer. – poison pawn Jun 02 '22 at 13:33
  • Yes, keyboard keys repeat when you hold them down long enough. Just like, say, holding down the "A" key, which also repeatedly triggers the KeyPress event. This is normal and nothing to worry about. – Hans Passant Jun 02 '22 at 13:38
  • @HansPassant I totally agree with you. But if Ctrl does not produce a character. how much repetition is necessary. This is what's stuck in my head. but there must be a reason which we don't know, ofcourse. – poison pawn Jun 02 '22 at 13:46

0 Answers0