Customized Shortcuts

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sfreak
    New Member
    • Mar 2010
    • 64

    Customized Shortcuts

    Hi there,

    Im trying to implement a way to set the shortcuts defined by the user.

    Im using NHibernate and my model class returns me fields in Keys type (Keys is an Enum that contains the name of the keys. example: Keys.F1)

    I have implemented it in a static way and the code is the following:
    Code:
    internal void set_shortcuts (KeyEventArgs e)
    {
                switch (e.KeyCode)
                {
                    case Keys.F1: this.button_save.PerformClick(); break;
                    case Keys.F2: this.button_cancel.PerformClick(); break;
                }
    }
    Now I have a form and the user redefined these shortcuts and I dont know how to do to set them. To be more specific:

    Code:
    internal void set_shortcuts (KeyEventArgs e)
    {
                Keys save_shortcut = Shortcuts.load("save_button");
                Keys cancel_shortcut = Shortcuts.load("cancel_button");
    
                switch (e.KeyCode)
                {
                    case save_shortcut: this.button_save.PerformClick(); break;
                    case cancel_shortcut: this.button_cancel.PerformClick(); break;
                }
    }
    I know that the SWITCH statment only accepts constants but i didnt figure out a way to do this.

    I hope you can help me

    Fernando Mello
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    I know that the SWITCH statment only accepts constants but i didnt figure out a way to do this.
    Code:
    Keys Temp = e.Keycode; // Now you have a constant you can Switch
    switch (Temp)
    {
       // blah blah blah
    }
    Code:
    case Keys.F1: this.button_save.PerformClick(); break;
    Oh please don't get in the habit of putting your methods in the click handlers then calling them. As your programs grow this becomes really ugly and hard to manage.

    Code:
    this.button_save_Click(object sender, eventargs e)
    {
       DoSave();  // That's it. Have the save button call the save method
    }
    
    internal virtual void DoSave()
    {
       // Do your save function
       // Now that this can be virtual, your inherited classes can override this
    }
    
    void set_shortcuts(KeyEventArgs e)
    {
       Keys Temp = e.Keycode
       switch (Temp)
       {
        case Keys.F1:
               DoSave();
               break;
        case Keys.F2:
               DoCancel();
               break;
       }
    }
    
    void MoreComplexProcess()
    {
        DoValidation();
        WarnUser();
        DoSave();
        UpdateGUI();
        AddLogEntry("Complex operation complete");
    }
    
    void TheUglyButtonClickWay()
    { 
        this.button_validate.PerformClick();
        this.button_save.PerformClick();
        // etc.  See where this is going?
        // If you rename a button, you break your calls
        // Other classes would have to have access to the controls in order to call 
        //   Instead of calling the public method of "Save()", "Load()", "ResetScore()"
    }

    Comment

    • GaryTexmo
      Recognized Expert Top Contributor
      • Jul 2009
      • 1501

      #3
      Darnit, the reply button doesn't work off your post, T. Blah, oh well.

      I think the OP meant you have to use constants in the case part of the switch statement, which is correct. Per their second code block, they want to switch on a variable assigned to the key, which I'm pretty sure you can't do.

      That said, you can change the way the problem is approached. I've done key bind setups like this for various projects before. What I used was a hash table mapping a Key to an Action enum. For example...

      Code:
      enum Action
      {
        Unbound,
        Save,
        Cancel //,
        // etc...
      }
      Now you can create a hash table and associate a key with an action. For example...
      Code:
      myHashTable.Add(Keys.F1, Action.Save);
      When you look up your pressed keys, you can use that key as the hashtable key and find the action associated with it. Then you simply switch on the action and do what you're already doing.

      Hope that helps, good luck!

      Comment

      • Sfreak
        New Member
        • Mar 2010
        • 64

        #4
        Thanks both of you! GaryTexmo got what I really would like to know... works perfectly now. I didnt think of using a hashtable. Now it works fine :)

        tlhintoq thanks for the tips too! Im not calling the methods, i know it becomes a mess, I try to use in my sw several Design Patterns as MVC and others but my software is written in portuguese and that was a way to show you what i was trying to do. Thanks for the tip.

        Fernando

        Comment

        Working...