Is there a way to automatically rerun a dynamo script, used as a pushbutton, until I hit the escape key?
I know that on the pyRevit ribbon, there’s the match function where you can do a similar concept but I’m not as fluent in python yet.
Is there a way to automatically rerun a dynamo script, used as a pushbutton, until I hit the escape key?
I know that on the pyRevit ribbon, there’s the match function where you can do a similar concept but I’m not as fluent in python yet.
Hi @bschneiderhan, maybe you already figured out, but this is my take.
What you need is a never ending loop with while True
and a condition to break
out of it.
To read the key presses, you should be able to do it with the msvcrt
module.
Since the msvcrt.getch
function is blocking (it waits until you press a key), you first need to check if a key has been pressed with the msvcrt.kbhit
function.
So your code could be like this:
import msvcrt
def esc_pressed():
return msvcrt.kbhit() and ord(msvcrt.getch()) == 27
while True:
if esc_pressed():
break
# place your repeating code here...
Note: this is what I found on stack overflow after 5 minutes of google search.
I also didn’t test it, it might be that the msvcrt
module won’t work in revit/pyrevit… give it a try and let us know if that worked!
Hi Brandon, did this question be solved? I am now trying to run dynamo in pyrivit so that I can rerun the dynamo model automatecally and obtain all the result revit files automatecally. But my dynamo model is a littel complex with many different input. May I ask if you successfully make it?
I haven’t had good luck incorporating msvcrt into my scripts.
It runs into logic errors, pyRevit seeming to input keystrokes for me. I haven’t had the time to try and figure out why and my application was more complex than just rerun the same thing.
Im not sure how equivalent there is available, but I cracked key holding in C# recently. I assume somewhere in pyrevits inner workings there is some code for this as well to support alt click and shift click:
regarding the pyRevit keyPressed magic, this is happening here pyRevit/dev/pyRevitLabs.PyRevit.Runtime/ScriptCommands.cs at f5a20a42010d27f12353e392e6ddfd7a745391f4 · pyrevitlabs/pyRevit · GitHub