locked
Dynamic Hot Keys WinForms RRS feed

  • Question

  • Is there a simple way to set hot/shortcut keys from file. Specifically the Function keys and some other keys in combination with the control key.

    I have a file of functions and there corresponding keys combinations, that I wish for my program to load.

    Cheers,

    Ben


    Ben Ringrose

    • Moved by CoolDadTx Monday, April 25, 2016 4:17 PM Winforms related
    Monday, April 25, 2016 6:28 AM

Answers

  • If you define your keys in a way that matches the standard keys enumeration you could use KeyDown on your window.

    You need to set

    KeyPreview = true;

    and handle keydown:

    KeyDown += new KeyEventHandler(form_KeyDown);

    In the handler you would usually just compare the e.KeyPressed to a fixed value, like:

    if (e.KeyCode == Keys.F && Control.ModifierKeys == Keys.Control)

    Obviously, you will have a variable string to compare instean of Keys.F.

    But you can convert the KeyCode to the Member Name you see in the table here:

    https://msdn.microsoft.com/en-us/library/system.windows.forms.keys.aspx

    Roughly:

    KeysConverter converter = new KeysConverter();
    string textRepresentation = converter.ConvertToString(e.KeyCode);
    Console.WriteLine(textRepresentation );

    Meaning you could have that membername as a string in your file, read those in and compare to your input key.

    It would of course be a lot easier if all these work with the control key pressed.

    I'm not clear on a number of things from your explanation though.

    For example, what you mean by a "file of functions".

    You're hopefully looking to map to bunch of methods you have compiled into your form already.

    You can get a method by name and invoke it:

    MethodInfo method = typeof(Sample).GetMethod("GenericMethod");
    MethodInfo generic = method.MakeGenericMethod(myType);
    generic.Invoke(this, null);
    http://stackoverflow.com/questions/232535/how-do-i-use-reflection-to-call-a-generic-method


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Monday, April 25, 2016 8:50 AM

All replies

  • If you define your keys in a way that matches the standard keys enumeration you could use KeyDown on your window.

    You need to set

    KeyPreview = true;

    and handle keydown:

    KeyDown += new KeyEventHandler(form_KeyDown);

    In the handler you would usually just compare the e.KeyPressed to a fixed value, like:

    if (e.KeyCode == Keys.F && Control.ModifierKeys == Keys.Control)

    Obviously, you will have a variable string to compare instean of Keys.F.

    But you can convert the KeyCode to the Member Name you see in the table here:

    https://msdn.microsoft.com/en-us/library/system.windows.forms.keys.aspx

    Roughly:

    KeysConverter converter = new KeysConverter();
    string textRepresentation = converter.ConvertToString(e.KeyCode);
    Console.WriteLine(textRepresentation );

    Meaning you could have that membername as a string in your file, read those in and compare to your input key.

    It would of course be a lot easier if all these work with the control key pressed.

    I'm not clear on a number of things from your explanation though.

    For example, what you mean by a "file of functions".

    You're hopefully looking to map to bunch of methods you have compiled into your form already.

    You can get a method by name and invoke it:

    MethodInfo method = typeof(Sample).GetMethod("GenericMethod");
    MethodInfo generic = method.MakeGenericMethod(myType);
    generic.Invoke(this, null);
    http://stackoverflow.com/questions/232535/how-do-i-use-reflection-to-call-a-generic-method


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Monday, April 25, 2016 8:50 AM
  • Hi Ben,
    There are a lot of ways to set the hot keys, perhaps you could refer to the following methods:
    1. Alt + key.
    Set the control's 'Text' property to '&'+ key. For example, button1.Text= "Enter(&A)", hot key = 'Alt + A'.
    2. Control + key.(KeyDown event)
    3. Override 'ProcessCmdKey' method.
    Sample:
    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
     if (keyData == (Keys.Escape))
     {
       this.Close();
     }
     return base.ProcessCmdKey(ref msg, keyData);
    }
    Regards,
    Moonlight

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    Tuesday, April 26, 2016 5:17 AM