Click here to Skip to main content
15,914,452 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
Hi How to program shortcuts defined
For example, the control key and X to close program
I've worked with this function, but inside it can not be combined keys

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if(keyData==Keys.Control+Keys.X) //error
            return base.ProcessCmdKey(ref msg, keyData);
        }
Posted
Comments
Sergey Alexandrovich Kryukov 12-Jun-11 16:25pm    
Tag it! WPF, Forms, APS.NET?! Always tag.
--SA

1 solution

How about using the KeyDown event.

First set KeyPreview to true. Do it in constructor.

C#
this.KeyPreview = true;
this.KeyDown += new KeyEventHandler(KeyDownEvent);


void KeyDownEvent(object sender, KeyEventArgs e)
{
    if (e.Control && e.KeyCode == Keys.X)
    {
        this.Close();
    }
}


It should work now.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 12-Jun-11 16:26pm    
There is a problem here: it works only for a control having focus. OP needs a shortcut which should work everywhere. My 4.
--SA
Tarun.K.S 12-Jun-11 16:44pm    
I think it will close the window even if the focus is on some other control. Or am I missing something?
Sergey Alexandrovich Kryukov 12-Jun-11 23:52pm    
Ah... because of key preview..?
Probably I missed something. There was something which is not quite clear for me about it, I need to check up...
I'll re-vote by 5, thanks for your note.
--SA
Tarun.K.S 13-Jun-11 2:58am    
No problem, thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900