Caster Troy
Caster Troy

Reputation: 2866

What's a good design to handle multiple global hotkeys?

I'm struggling to think of a good design to handle multiple global hotkeys.

Say I have three different functions bound to three different global hotkeys...

  1. Play Song | Ctrl + P
  2. Skip Song | Ctrl + N
  3. Increase Volume | Ctrl + V

What's a good, effective way to check if the hotkey pressed conforms to a certain function? I'm using a class very similar to this: http://www.liensberger.it/web/blog/?p=207

Should I create a new instance of the hotkey class for each hotkey?

Hotkey hotkey = new Hotkey();
hotkey.RegisterHotkey(Shortcut.ModifierKeys.Control, Keys.F10);

hotkey.KeyPressed += ((s, args) =>
{
    //Do Something!
});

Or should I have an enum with different hotkey functions and manage it from within the hotkey class to prevent multiple instances (seems wasteful but easy). Thanks for any advice / help in advance.

Upvotes: 0

Views: 484

Answers (2)

Henrik Karlsson
Henrik Karlsson

Reputation: 5733

Why not make the hotkey class static?

 Hotkey.Register(modifier, key, callback)

and keep them in a list within the class?

Also, I would recommend adding more modifiers to your hotkeys. All of yours are assigned to a common task: Control+P = Print, Control+N = New and Control+V = Paste

Upvotes: 0

DeanOC
DeanOC

Reputation: 7282

I would opt for creating a separate instance of the class for each different function, as the Command Pattern seems like a good fit here. See this link for some more info.

Upvotes: 2

Related Questions