# Don't exit loop when ESC button press

**URL:** <https://discourse.mcneel.com/t/dont-exit-loop-when-esc-button-press/90955>\
**Category:** Scripting\
**Tags:** python\
**Created:** [October 24, 2019, 5:16pm UTC](https://discourse.mcneel.com/t/dont-exit-loop-when-esc-button-press/90955 "2019-10-24T17:16:26Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![xiix.xoox](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/xiix.xoox/32/638532_2.png) [@xiix.xoox](https://discourse.mcneel.com/u/xiix.xoox)\
**Post date:** [October 24, 2019, 5:16pm UTC](https://discourse.mcneel.com/t/dont-exit-loop-when-esc-button-press/90955/1 "2019-10-24T17:16:26Z")

</div>

```
import rhinoscriptsyntax as rs
import scriptcontext as sc

while True:
    Value=rs.GetString("Input:")
    if sc.escape_test(False):
        print "ESC pressed "
        break
    print Value

```

I run this script in Version 6 SR20 but don’t exit when ESC button press.

---

<div class="post-metadata">

**Author:** ![ivelin.peychev](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/ivelin.peychev/32/676038_2.png) [@ivelin.peychev](https://discourse.mcneel.com/u/ivelin.peychev)\
**Post date:** [October 24, 2019, 5:50pm UTC](https://discourse.mcneel.com/t/dont-exit-loop-when-esc-button-press/90955/2 "2019-10-24T17:50:25Z")

</div>

Do not use ‘while’ in Rhino, ever!  
Using ‘while’ will shorten your life. 😜

---

<div class="post-metadata">

**Author:** ![clement](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/clement/32/772979_2.png) [@clement](https://discourse.mcneel.com/u/clement)\
**Post date:** [October 24, 2019, 8:03pm UTC](https://discourse.mcneel.com/t/dont-exit-loop-when-esc-button-press/90955/3 "2019-10-24T20:03:17Z")

</div>

@xiix.xoox, there is nothing wrong using while loops but you should always put your code into a function so you can use `return` statement in case you want to exit at a certain point and continue the script at another point.

For your example you might just check if a value has been entered, if not break the loop. This also works if you press ESC when it prompts for a value:

```python
import scriptcontext
import rhinoscriptsyntax as rs

def DoSomething():
    while True:
        value = rs.GetString("Enter something:")
        if not value: break
        print "You entered: {}".format(value)

    print "End of loop"

DoSomething()

```

\_  
c.

---

<div class="post-metadata">

**Author:** ![xiix.xoox](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/xiix.xoox/32/638532_2.png) [@xiix.xoox](https://discourse.mcneel.com/u/xiix.xoox)\
**Post date:** [October 25, 2019, 12:24am UTC](https://discourse.mcneel.com/t/dont-exit-loop-when-esc-button-press/90955/4 "2019-10-25T00:24:17Z")

</div>

Thank you. I know it must be, but in this case I want the user to enter it.

> ```
> import rhinoscriptsyntax as rs
> import scriptcontext as sc
> while True:
> Value=rs.GetString("Input:")
> if sc.escape_test(False):
> print "ESC pressed "
> return
> #if Enter button Pressed
> elif not Value or not Value.isdigit():rs.Rhino.UI.Dialogs.ShowMessageBox("Why not Input?","Note",0,System.Windows.Forms.MessageBoxIcon.Error)
> else:break
> print Value
> 
> ```

---

<div class="post-metadata">

**Author:** ![xiix.xoox](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/xiix.xoox/32/638532_2.png) [@xiix.xoox](https://discourse.mcneel.com/u/xiix.xoox)\
**Post date:** [October 25, 2019, 12:26am UTC](https://discourse.mcneel.com/t/dont-exit-loop-when-esc-button-press/90955/5 "2019-10-25T00:26:31Z")

</div>

LOL. What can I use now

---

<div class="post-metadata">

**Author:** ![ivelin.peychev](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/ivelin.peychev/32/676038_2.png) [@ivelin.peychev](https://discourse.mcneel.com/u/ivelin.peychev)\
**Post date:** [October 25, 2019, 12:30am UTC](https://discourse.mcneel.com/t/dont-exit-loop-when-esc-button-press/90955/6 "2019-10-25T00:30:34Z")

</div>

> [@xiix.xoox](#):
>
> What can I use now

`For`

---

<div class="post-metadata">

**Author:** ![nathancoatney](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/nathancoatney/32/767339_2.png) [@nathancoatney](https://discourse.mcneel.com/u/nathancoatney)\
**Post date:** [October 25, 2019, 12:30am UTC](https://discourse.mcneel.com/t/dont-exit-loop-when-esc-button-press/90955/7 "2019-10-25T00:30:44Z")

</div>

I think that GetString is consuming the Esc keypress.

As @clement says, according to the GetString help doc:

```
Returns:
  str: The string either input or selected by the user if successful.
       If the user presses the Enter key without typing in a string, an empty string "" is returned.
  None: if not successful, on error, or if the user pressed cancel.

```

So testing Value for None is equivalent to Esc keypress, (although there may be other ways to make a None return). A Return keypress with no text will give you an empty string, “”, so those two conditions may work for your example. If more explicit flow control is needed then add options to GetString and test for those.

---

<div class="post-metadata">

**Author:** ![ivelin.peychev](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/ivelin.peychev/32/676038_2.png) [@ivelin.peychev](https://discourse.mcneel.com/u/ivelin.peychev)\
**Post date:** [October 25, 2019, 12:31am UTC](https://discourse.mcneel.com/t/dont-exit-loop-when-esc-button-press/90955/8 "2019-10-25T00:31:56Z")

</div>

If Rhino gets into a non-responsive state, no ESC will help you.

Use `For` and spare yourself the trouble.

---

<div class="post-metadata">

**Author:** ![xiix.xoox](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/xiix.xoox/32/638532_2.png) [@xiix.xoox](https://discourse.mcneel.com/u/xiix.xoox)\
**Post date:** [October 25, 2019, 12:46am UTC](https://discourse.mcneel.com/t/dont-exit-loop-when-esc-button-press/90955/9 "2019-10-25T00:46:13Z")

</div>

Can you demo with this?

---

<div class="post-metadata">

**Author:** ![xiix.xoox](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/xiix.xoox/32/638532_2.png) [@xiix.xoox](https://discourse.mcneel.com/u/xiix.xoox)\
**Post date:** [October 25, 2019, 12:50am UTC](https://discourse.mcneel.com/t/dont-exit-loop-when-esc-button-press/90955/10 "2019-10-25T00:50:23Z")

</div>

infinite loop with “for”. Hmm  
What are the values of loop

---

<div class="post-metadata">

**Author:** ![ivelin.peychev](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/ivelin.peychev/32/676038_2.png) [@ivelin.peychev](https://discourse.mcneel.com/u/ivelin.peychev)\
**Post date:** [October 25, 2019, 12:51am UTC](https://discourse.mcneel.com/t/dont-exit-loop-when-esc-button-press/90955/11 "2019-10-25T00:51:41Z")

</div>

Why would you need infinite?  
After 1000 loops Rhino will become non-responsive. (depending on what you do inside that loop)

Better do For \_ in range(10000) will have more success, and it is more reliable.

---

<div class="post-metadata">

**Author:** ![nathancoatney](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/nathancoatney/32/767339_2.png) [@nathancoatney](https://discourse.mcneel.com/u/nathancoatney)\
**Post date:** [October 25, 2019, 1:01am UTC](https://discourse.mcneel.com/t/dont-exit-loop-when-esc-button-press/90955/12 "2019-10-25T01:01:36Z")

</div>

```
import rhinoscriptsyntax as rs
import scriptcontext as sc

while True:
    Value = rs.GetString('Input: ', None, ['Option_1', 'Option_2'])
    if Value is None: # empty string will also be 'not', so test for None
        print('maybe Esc was pressed..breaking from loop')
        break
    if Value == '':
        print('Enter pressed with no string')
        continue
    # test for options in same manner...
    print '{} was entered'.format(Value)
```

---

<div class="post-metadata">

**Author:** ![xiix.xoox](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/xiix.xoox/32/638532_2.png) [@xiix.xoox](https://discourse.mcneel.com/u/xiix.xoox)\
**Post date:** [October 25, 2019, 1:05am UTC](https://discourse.mcneel.com/t/dont-exit-loop-when-esc-button-press/90955/13 "2019-10-25T01:05:46Z")

</div>

Thank you!

---

<div class="post-metadata">

**Author:** ![xiix.xoox](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/xiix.xoox/32/638532_2.png) [@xiix.xoox](https://discourse.mcneel.com/u/xiix.xoox)\
**Post date:** [October 25, 2019, 1:06am UTC](https://discourse.mcneel.com/t/dont-exit-loop-when-esc-button-press/90955/14 "2019-10-25T01:06:36Z")

</div>

Thank you to everyone who helped me!

---

<div class="post-metadata">

**Author:** ![nathancoatney](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/nathancoatney/32/767339_2.png) [@nathancoatney](https://discourse.mcneel.com/u/nathancoatney)\
**Post date:** [October 25, 2019, 1:46am UTC](https://discourse.mcneel.com/t/dont-exit-loop-when-esc-button-press/90955/15 "2019-10-25T01:46:38Z")

</div>

I’ve coded myself into plenty of long running or infinite loops while testing. Sometimes, as you suggest, in order to figure out what is going on, I may change it to a for loop with enough cycles to describe the issue, but there is nothing wrong with a while True loop as long as you have your exit plan in place.

True, there is no built-in way I know of that will kill your script and keep Rhino running if you get into trouble (that would be nice tough). I have used the escape\_test in the past with success (sometimes it takes 2 escape presses).

If interested, here are some things I have discovered while dealing with the UX in these situations:

Rhino.RhinoApp.Wait() can help help with ‘not responding’ windows messages, although where to put it seems to be something of an art, to me at least.

Periodic prints, doc updates and sc.doc.Views.Redraw() can also provide feedback in long running loops. Maybe updating every 100 or 1000 or n cycles is more appropriate than every singe loop.

rs.StatusBar#######() functions are available if you can estimate completeness or some other scalar metric. Updating this seems to keep Rhino ‘alive’ pretty well.

Conduits are a little more complex but are another way (my favorite) to provide graphical updates on loop progress as well, if the loop is updating/generating geometry.

---

<div class="post-metadata">

**Author:** ![xiix.xoox](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/xiix.xoox/32/638532_2.png) [@xiix.xoox](https://discourse.mcneel.com/u/xiix.xoox)\
**Post date:** [October 25, 2019, 1:49am UTC](https://discourse.mcneel.com/t/dont-exit-loop-when-esc-button-press/90955/16 "2019-10-25T01:49:56Z")

</div>

Thank you. I will learn more

---

<div class="post-metadata">

**Author:** ![ivelin.peychev](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/ivelin.peychev/32/676038_2.png) [@ivelin.peychev](https://discourse.mcneel.com/u/ivelin.peychev)\
**Post date:** [October 25, 2019, 7:41am UTC](https://discourse.mcneel.com/t/dont-exit-loop-when-esc-button-press/90955/17 "2019-10-25T07:41:15Z")

</div>

> [@nathancoatney](#):
>
> (sometimes it takes 2 escape presses)

`Sometimes` is the key word here. I don’t like to have a solution that might work.

> [@nathancoatney](#):
>
> Rhino.RhinoApp.Wait()…Periodic prints, doc updates and sc.doc.Views.Redraw()…rs.StatusBar#######()

Yes, I know, I’ve tried them all. But they slow the solution.

In my experience While loops lead to crash in most cases. This should not be programmer’s luck. Rhino should be made in such a way to prevent non-responsive-ness. Also sometimes rhino goes into a RAM-killing frenzy that leads to system crash. That’s also Rhino issue. Since 3DExperience doesn’t do that and it’s a monster of a software. I do not accept this is an error of the script (the user). Just that Rhino is not developed to protect PC’s resources and to ensure Windows has enough resources to allow the user to kill Rhino when necessary.
