C#, best practices keyboard event integration


I’m building a plugin that will launch a WPF window with a keystroke and I would like to understand the best approach for doing this in Rhino. Questions include:

  1. Is there a recommended way to coordinate an event with existing user preferences and keystroke shortcuts so that the keystroke won’t conflict with a user’s preference, etc.?

  2. Is there a recommended way to prevent keystrokes that relate to an event from appearing on the command-line as command-line command suggestions?

  3. Am I missing anything else that’s important?

Thanks in advance.

Hi @tree,

The best practice is to avoid hooking the Windows keyboard when possible and just use Rhino’s keyboard handling features when possible.

For example, if you need a shortcut to run a command, you can do so programatically:

var macro = ShortcutKeySettings.GetMacro(ShortcutKey.CtrlF12);
if (string.IsNullOrEmpty(macro))
    ShortcutKeySettings.SetMacro(ShortcutKey.CtrlF12, "! _ShowMyWpfForm");

– Dale


Thanks Dale!


Two quick questions:

  1. Is it possible to programmatically define a new key sequence for the above suggestion? (ie. I don’t really like the options currently defined by ShortcutKey)

  2. If I understand the method correctly it binds the “EnglishName” of a command to a keystroke. Is this correct?

Hi @tree,

1.) No, not without hooking they keyboard yourself.

2.) Yes. More on scripting Rhino commands:

– Dale


Thanks. So I need to learn about keyboard hooks … super, I’m always happy to learn something new!

By chance are there any c# examples for defining and using a keyboard hook in Rhino?

Dale - a post reply note - I’ve hacked together something with a keyboard event that’s doing the trick and can live without an example but if one is ever made please let me know, I’d be really happy to take a look.

Just Google “C# Hook Windows Keyboard”.

– Dale


Thanks for the reply and extremely sorry for the long delay. Got it!