C# and timer components showing Grasshopper reading keys from the keyboard

Keyboard reader for Grasshopper

A prototype way of interacting with Grasshopper with the keyboard. Press any character key on the keyboard to send true/false values to Grasshopper.

It uses the Global Mouse and Keyboard Hook classes by George Mamaladze to intercept key up/down events. I have written two components that turns these key events into true/false commands – if a key is raised, ‘false’ is sent, and if pressed, ‘true’ is sent.

Keyboard reader with 6 pre-filled keys
Keyboard reader with 6 pre-filled keys

In the component above, 6 keys have been hard-coded. (I have been using this as part of video game-like WASD camera control in Rhino.) The component below accepts any character (and possibly any key on the keyboard if you’re lucky enough to find the correct character code for each key.)

Keyboard reader with any letter of the keyboard
Keyboard reader with any letter of the keyboard

Install

To install, download the file at the bottom of this page. Unzip and copy the two files (one GHA and one DLL) into your Grasshopper components folder. To use, set up as in the images above, and remember to attach a timer with a short interval. You may need to unblock the file within Windows if you are having problems. (Right click each of the two files in Windows Explorer, click Properties, and click Unblock.)


Source code

The source code of the Grasshopper components is available at GitHub.

The classes for interacting with the keyboard must (at least for the moment) be downloaded separately, and are available here.

7 thoughts on “Keyboard reader for Grasshopper”

    1. Ah, where did it go?? I have added a new copy – let me know how you get on 🙂

  1. James — this is awesome… I’m thinking about implementing this in concert with Human’s Clickable Mesh — since Clickable mesh intercepts all mouse clicks and can’t really be always on. That said, did you explore using an event-based model as opposed to a GH timer? I try to avoid timers at all costs…

    Cheers,
    Marc

    1. Incidentally — I was not able to get this to work… I used the provided components and all the gha + dlls are loading properly (no error messages). The sample file comes up with serialization errors and outputs appear to be mislabeled. Perhaps this is an issue with being an old file written for a different environment? I’m on Windows 10, Rhino 5, and GH 0.9.0076…

      1. I guess you unblocked the DLL? I have just tested, and got as far as clearing any errors, but I can’t get it to work either. I originally wrote/tested it on Win7, and I’m now on Win10, so it’s possible that’s the source of the issue.

    2. It was a while back, but I believe the reason was only my own time – adding a GH timer got the job done. It would certainly be much better with a proper timer built in.

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: