We help IT Professionals succeed at work.

Form1_KeyDown Not Working with (e.Control && e.Shift && !e.Alt && e.KeyCode == Keys.D0)

I created a simple Windows Form in a C# project.

I created a KeyDown event:

          private void Form1_KeyDown(object sender, KeyEventArgs e)
          {
                  if (e.Control && e.Shift && !e.Alt && (e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9)
                        {Do Something};
          }

I set the KeyPreview property of the form to "true".

CNTL-SHIFT-1, CNTL-SHIFT-2, etc work just fine.

However, CNTL-SHIFT-0 does not.  Form1_KeyDown() event is never called.

I've never experienced this odd behavior before.

Thanks, in advance, for your help.
Comment
Watch Question

HainKurtSr. System Analyst
CERTIFIED EXPERT

Commented:
it is normal :)

use UP event

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Control && e.Shift && !e.Alt && (e.KeyValue >= 48 && e.KeyValue <= 57))
            {
                string n = e.KeyCode.ToString();
            };
        }

Open in new window

Sr. System Analyst
CERTIFIED EXPERT
Commented:
or your original code

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Control && e.Shift && !e.Alt && (e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9))
            {
                string n = e.KeyCode.ToString();
            };
        }

Open in new window

HainKurtSr. System Analyst
CERTIFIED EXPERT

Commented:
actually this one also works

        private void Form1_KeyDown_1(object sender, KeyEventArgs e)
        {
            if (e.Control && e.Shift && !e.Alt && (e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9))
            {
                richTextBox1.Text = DateTime.Now.ToString(); 
            };
        }

Open in new window


but dont put a break point and let it run...
CERTIFIED EXPERT

Commented:
It's a large pain, with not an easy way around if you don't control you're users' systems.
Something tied to shortcut keys for changing keyboard layout, which windows just intercepts.  I've never actually had any reason to have multiple keyboard layouts, so have never used it, and unsure WHY only the zero is the issue, but that is the cause.

If you dig into your control panel language settings, and change the Key Sequence for "Switch Keyboard Layout" to unassigned, Ctrl+Shift+0 starts working again.  
Of course the issue is you would have to do this on every machine running your application.

KeyboardSettings.PNG

Author

Commented:
Thanks