1

I need to make a shortcut key combination of three letters (Ctrl+L+I) I tried a lot to do so but no luck . I tried it in this way

private void MDIParent2HOME_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.Keycode==Keys.L && e.KeyCode == Keys.I)
            {//login
                    Form1 chilform = new Form1();
                    chilform.MdiParent = this;
                    chilform.Show();
            }
       }

but this didn't work.

then I changed my key combination (ctrl+ALt+L) and tried it in same way

private void MDIParent2HOME_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.Alt && e.KeyCode == Keys.L)
            {//login
                {
                    Form1 chilform = new Form1();
                    chilform.MdiParent = this;
                    chilform.Show();
                }
            }
        }

and I am wondering that worked perfect .I couldn't get the reason do anyone know about this behaviour of KeyDown event.Also help me if I can do the same with (ctrl+L+I) . Thanks

varsha
  • 1,620
  • 1
  • 16
  • 29

2 Answers2

1

You are checking for e.Keycode==Keys.L && e.KeyCode == Keys.I. I think e.Keycode contains only the value of a single key, L or I, but not both at the same time, so your check will always fail.

Note that Alt, Shift and Ctrl are modifiers and are handled a bit differently than other keys.

Thaoden
  • 3,460
  • 3
  • 33
  • 45
  • okay,thank you for fast response, is there any way if i need to use L and I both? – varsha Mar 12 '15 at 12:04
  • None that I am aware of. I think (but am not sure), that only a single KeyDown event is registered when multiple non-modifiers are pressed. After all, you cannot type `LILILILI` in your text editor by pressing `L` + `I`. – Thaoden Mar 12 '15 at 12:18
1

Hmmm...I think my solution works

private bool IfSeen;

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (IfSeen)
        {
            if (keyData == (Keys.Control | Keys.I))
            {
                MessageBox.Show("You pressed Ctrl+L+I");
            }
            IfSeen= false;
            return true;
        }
        if (keyData == (Keys.Control | Keys.L))
        {
            IfSeen= true;
            return true;
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }
Rohit
  • 10,056
  • 7
  • 50
  • 82
  • Are you sure ?? have you tried ?? because it does not work that way at my end – Rohit Mar 12 '15 at 12:23
  • My bad. Deleted original comment. – Thaoden Mar 12 '15 at 12:25
  • This is basically what the linked duplicate suggests. But [this comment](http://stackoverflow.com/questions/29009120/keydown-event-does-not-stroke-for-some-combination#comment46264269_29009120) by @Default suggests that it is not what OP wants. I'm quite confused. – Sriram Sakthivel Mar 12 '15 at 12:26
  • @thaoden no it is returning true only on ctrl+L+I – varsha Mar 12 '15 at 12:27