#! python3
import rhinoscriptsyntax as rs
import System
import System.Collections.Generic
import Rhino
import scriptcontext as sc
for i in range(20):
rs.Sleep(100)
print(sc.escape_test())
if sc.escape_test(True):
print("True")
if sc.escape_test(False):
print("False")
And see that it won’t print to the command line nor does it react on ESC press.
As you can see, it is printing to the command line and pressing the escape key has been acknowledged. Note that as documented I had to press the escape key twice.
I suspect you may be under a misapprehension about escape_test.
Firstly, it takes two optional parameters:
so the True/False parameters you are supplying control whether an exception is thrown or not. The second parameter is passed to the host environment.
Secondly, it returns a boolean which indicates whether the escape key was pressed.
Your script causes escape_test to be run three times in each iteration. The second and third times are so close after the first that the chance of getting an escape press between them is negligible so their outcomes will be false, meaning that the associated print statements will not be executed.
Only the first test is likely to register a press. Up to that point the function returns false and that is what we see printed in the command line. When escape is detected the first print statement would print True, but the function is using the default throw_exception=true setting so an exception is thrown and, as there is no exception handling in your script, it stops before getting to the print statement.
(Thus, @Gijs, escape_test() is actually working correctly and RH-72484 is redundant.)
[EDIT: @Gijs the above relates to Iron Python - I missed Holo’s RhinoCode tag - and escape_test does not work in Python 3 in my alternative code anymore than in Holo’s, whereas Dale’s solution below does. Sorry for muddying the waters.]
If you want to halt the script without setting up an exception handling framework you could do this, turning off the exception throw and adding a break statement:
for i in range(20):
rs.Sleep(100)
if sc.escape_test(False):
print(i, "Escape pressed")
break
That’s strange. What version of Wip are you on?
I suddenly had issues with adding a point too, and had to restart Rhino for it to work.
Print isnt printing to command either, only to the RhinoCode terminal.
One proviso that should be made public: if you are running the code in the RhinoCode editor you need to click in the Rhino window to give it focus before pressing the escape key.
I found another strange thing.
If I use rs.GetObjects(“”,preselect=True) then the command I used took 30 seconds to complete IF objects were preselected. But only 2 seconds if I selected them after the command was started.