Unexpected program behavior: RhinoApp.SendKeystrokes order of operations

Hi,

We are developing a plugin that involves some context-switching. Sometimes we want to be able to cancel out a command that someone might be in the middle of issuing to Rhino if they decide to switch contexts before they are done. This is to prevent some errors in switching contexts.

We do this by issuing keystrokes to the Rhino console: “_EnterEnd”.

We recently discovered, when upgrading our plugin to Rhino 7, that there is an order of operations behavior that is unexpected. When we call RhinoApp.SendKeystrokes("_EnterEnd ", false);, then send another command, the second command issues first, followed by the keystrokes. These keystrokes cancel out the second command rather than whatever the user might have been entering before switching contexts.

I’ve put together a little c# script in grasshopper to illustrate what I mean. We are working with a RHP plugin but the GH script is concise and illustrates things in the console.

We’ve had some luck adding RhinoApp.Wait() after sending keystrokes, but this smells really bad from a best-practices standpoint. I’m worried it could introduce unexpected hangs to our plugin, for example. It also feels like something that is a workaround rather than solving the issue at the source.

Please see the attached script. Also, I found that this occurs in both Rhino 6 and 7, though we only recently discovered this apparent bug in our move to 7, for whatever reason, probably to do with something else within our plugin.

Regardless, this simple script seems to illustrate unexpected behavior, or perhaps I don’t grasp the correct way to work with “SendKeystrokes”.

Either way, your help is very much appreciated!

SendKeystrokes_BugReport_ToMcNeel.gh (4.4 KB)

Hi @gruedisueli,

You might try scripting the _EnterEnd command using RhinoApp.RunScript.

Also, when launching a command, precede the command name with a ! character, which will cancel any currently running command.

RhinoApp.RunScript("!_Line", true);

– Dale

Thanks @dale!

I will look into these suggestions.

Can you please provide a little more information about “RunScript”? I’ve reviewed the docs here. The docs would indicate that “RunScript” runs a “previously loaded” script, presumably from a .RVB file. Does this also work with built-in Rhino commands like “EnterEnd”? Are you suggesting that I can replace SendKeystrokes → EnterEnd with RunScript → EnterEnd and it should create the same intended operation in Rhino?

Not the RunScript command, but rather the RhinoApp.RunScript method.

– Dale

1 Like

Thank you @dale, I didn’t realize these were different things!