Click here to Skip to main content
15,881,701 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a problem like this.

I have prepared a Winforms Application where I want to implement some short-cut keys functionality.

I have a generate function placed inside a user control. but I'm not able to implement the short cut key functionality. its confusing for me.

kindly help me out to fix this please.

Thanks in Advance
Posted
Comments
BillWoodruff 14-Nov-14 9:54am    
What is a "generate function;" what does it do ? In your UserControl, or Form, are you now implementing 'OnKeyPreview, 'ProcessCmdKey, or any key-handling ?
Velrats 14-Nov-14 10:00am    
Still now I'm not using any key press events. But I know how to do it by using the below code.

private void BtnGenerate_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.G && (e.Alt || e.Control || e.Shift))

{
Gen();
}
}
Now I want to use the Short cut key for generating function shown above which is used inside a usercontrol. i dont know howto implement that
BillWoodruff 14-Nov-14 10:08am    
What is that the implementation you show here does not do that you want done ?

Is your goal to handle short-cuts anywhere in your Application, or only in a certain ContainerControl, like a Panel, or only with a certain UI Control like a Button ?

Is your goal to have short-cuts whose function is defined once before your Application is run, or to have short-cuts whose definition can be changed while your Application runs ?

Do you wish to over-ride the standard short-cuts ?

Please be as specific as possible.
PIEBALDconsult 14-Nov-14 9:59am    
As far as I know shortcuts have to be applied to Buttons. So create a Button and set the shortcut on it.
My mistake, not Buttons, MenuItems.
http://msdn.microsoft.com/en-us/library/system.windows.forms.menuitem.shortcut(v=vs.110).aspx
Velrats 14-Nov-14 10:01am    
any demo project or links where in I can get these info please

1 solution

Short cuts are a PITA, much of the time.
You can set the ShortCutKeys property of menu controls, but if that doesn't cover what you want (and it rarely does) then you have to pretty much handle it yourself, and that is normally on a Form basis (or they only work when the custom control has the focus, which is difficult for users to handle).

To handle it by Form, override the ProcessCmdKey method:
C#
/// <summary>
/// Handle special keys that don't have shortcut operations.
/// </summary>
/// <param name="msg"></param>
/// <param name="keyData"></param>
/// <returns></returns>
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
    Keys keyOnly = keyData & ~Keys.Modifiers;
    Keys modifiersOnly = Control.ModifierKeys & (Keys.Shift | Keys.Control | Keys.Alt);
    if (modifiersOnly == 0)
        {
        // Key alone
        switch (keyOnly)
            {
            case Keys.Enter:
                tsbvPlaySelected.PerformClick();
                return true;
            }
        }
    else if ((modifiersOnly & Keys.Control) != 0)
        {
        // Control and key
        switch (keyOnly)
            {
            case Keys.D:
                // Toolstrip buttons don't have shortcut keys.
                tsbvRename.PerformClick();
                return true;
            case Keys.Add:
                // "Numeric +" can't be added as a shortcut key in the actual menu
                tsbvAddSelectedToLastList.PerformClick();
                return true;
            case Keys.Subtract:
                // "Numeric -" can't be added as a shortcut key in the actual menu
                tsbvRemoveSelectedFromLastList.PerformClick();
                return true;
            }
        }
    return base.ProcessCmdKey(ref msg, keyData);
    }
 
Share this answer
 
Comments
BillWoodruff 14-Nov-14 10:52am    
+5 ooh ... bitwise complement ... shiny ! I seem to recall using 'ProcessCmdKeys in a UserControl with success ... but it was a long time ago, and I'm not absolutely sure.
OriginalGriff 14-Nov-14 10:58am    
You can use it in a control yes - but IIRC it only gets the keypresses if it's got the focus. I could be wrong though.

And if it doesn't, what happens when you have two of the same control on a form?
Velrats 14-Nov-14 21:44pm    
Sowie... it worked...! :D :D :D nad as i expected the functionality was there :D thanks a lot +5 for you Griff :D

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