How to properly stop a python plugin by Esc


Hi, I need some help to understand how to properly terminate a Python script when it is converted to a plugin.

I made a dummy scipt that works when ran from the editor, but not as a plugin:

import rhinoscriptsyntax as rs
import scriptcontext
import Rhino

def CheckForESCpress():
    if scriptcontext.escape_test(False):
        rs.AddText("Command terminated by ESC", (-20,2,0), height=2, font="Century Gothic", font_style=1, justification=None)
        return True

textId=rs.AddText ( ".", (-18,0,0), height=2, font="Century Gothic", font_style=1, justification=None )
rs.RotateObject(textId,(0,0,0),rotation_angle=-20,axis=None, copy=False)
for i in range (n):
    rs.RotateObject(textId,(0,0,0),rotation_angle=0.2,axis=None, copy=False)
    rs.TextObjectText (textId, "Press ESC to terminate - "+str(n-1-i))

rs.DeleteObject (textId)
rs.AddText ( "ESC was not registered", (-18,0,0), height=2, font="Century Gothic", font_style=1, justification=None )

Here is the compiled script where pressing ESC doesn’t give me the desired result.