开发者

Is There a Better Way to Handle Key Presses?

开发者 https://www.devze.com 2023-03-20 09:21 出处:网络
M开发者_如何学Cost of the functions in my WinForms application have a key press alternative. There are a lot of them.At the moment I am handling this in a switch statement.My code analyzers tell me th

M开发者_如何学Cost of the functions in my WinForms application have a key press alternative. There are a lot of them. At the moment I am handling this in a switch statement. My code analyzers tell me that this code is un-maintainable to varying degrees of nasty. I have to agree. I would extract the handling code out to separate methods but in most cases this is only a line or two and it does not really help me get a handle on things.....

Is there are better way to handle this of am I stuck with a huge switch?

Thanks in advance


Somewhere in the code, soon or later, you will need to make swicth/case or if/else, or whatever. So pushing actual handling code for every Key or Key combination to separate function is already good step.

You may be can other "nice" stuff: make a dictionary where

Key: is keyboard key

Value: delegate to call on that key press

And basically what you will need to do is to say, pseudocode!!

dictionary[key].Invoke();

Hope this will give some hints.

Regards.


You could use a Dictionary to map different key presses to functions, and add to that Dictionary wherever it made sense structurally. For a very simple example:

public partial class Form1 : Form
{
    Dictionary<Keys, Action<object, KeyEventArgs>> KeyPressLookup;

    public Form1()
    {
        KeyPressLookup = new Dictionary<Keys, Action<object, KeyEventArgs>>();
        KeyPressLookup[Keys.F10] = (o, e) => MessageBox.Show("You pressed F10");

        InitializeComponent();
    }

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if(KeyPressLookup.ContainsKey(e.KeyCode) // Could use TryGetValue instead
            KeyPressLookup[e.KeyCode](sender, e);
    }
}

This code has the limitation that only one function can be assigned to a key, which may be good or bad. Of course you could make a Dictionary of Lists or "MultiDictionary" if you want multiple functions per key.

Alternatively, you could have multiple Dictionaries, and have them be used depending on the circumstances, such as user options or the current "mode" (depending on how your application is structured).

Using lambda functions the resulting code overall can be much more concise than defining a "regular" function for every key.

You might also want to come up with a concise way to define functions mapped to key combinations such as Ctrl-F10. How far you want to go would depend on the size of your application.


Implement a Command system (Like WPF).

If you seperate the logic of all your functions into individual Commands you can then attach these commands to your menu items etc

take a look at http://www.ageektrapped.com/blog/using-the-command-pattern-in-windows-forms-clients/

If you modify that code slightly so that you add the command.Key into the Dictionary examples given in the other answers you get your solution.

Alternatively each Command could contain a delegate method for the Form_keydown event that you attach when you attach the command and you can then analyse the keypress in the command and choose to process if it matches.

0

精彩评论

暂无评论...
验证码 换一张
取 消