# Program stopping after executing certain lines when handling key presses

**URL:** <https://discourse.processing.org/t/program-stopping-after-executing-certain-lines-when-handling-key-presses/43838>\
**Category:** Processing.py\
**Created:** [February 6, 2024, 1:25am UTC](https://discourse.processing.org/t/program-stopping-after-executing-certain-lines-when-handling-key-presses/43838 "2024-02-06T01:25:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eb3yr](https://avatars.discourse-cdn.com/v4/letter/e/b487fb/32.png) [@Eb3yr](https://discourse.processing.org/u/Eb3yr)\
**Post date:** [February 6, 2024, 1:25am UTC](https://discourse.processing.org/t/program-stopping-after-executing-certain-lines-when-handling-key-presses/43838/1 "2024-02-06T01:25:33Z")

</div>

I’m having a problem where the program stops executing whenever I run certain lines of code when handling key presses and mouse clicks. I’m trying to accept certain inputs to create an object of certain mass, radius and velocity.

```auto
def mouseReleased():
    vel = (PVector(mouseX, mouseY).sub(PVector(lastMouseClick[0], lastMouseClick[1]))).mag()
    print(vel)
    bodies.Append(Body(mass, radius, PVector(mouseX, mouseY), PVector(0, 0)))
    
    
def keyReleased():
    print(key)
    if key == "i":
        incr *= -1
    elif key == "p":
        incr *= 10
    elif key == "l":
        incr *= 0.1
    elif key == "r":
        radius += incr
    elif key == "m":
        mass += incr

```

In particular, pressing any of the keys in the if statement, or executing the bodies.Append() line, will stop the program. The window will stay open but won’t allow further interaction. vel, bodies, incr, radius and mass are all global variables.

Any help would be greatly appreciated!

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [February 6, 2024, 2:06am UTC](https://discourse.processing.org/t/program-stopping-after-executing-certain-lines-when-handling-key-presses/43838/2 "2024-02-06T02:06:44Z")

</div>

My advice when reading variable _key_ is to apply this code:  
`k = key.upper() if key != CODED else keyCode`

Then use local variable _k_ instead w/ capitalized letters like the example from this link:

> [@snowflake Snow!](https://discourse.processing.org/t/snow/7156/3):
>
> I’ve refactored the original code in order to cache all pre-calculated values. heavy_plus_sign However, it didn’t speed anything. Alas, I guess it’s got slightly slower! no_mouth Regardless, using FX2D instead of default JAVA2D is what actually makes the sketch race! racing_car """ Snow! (v1.1) by Sky (2019-Jan-04) mod GoToLoop (2019-Jan-07) https://Discourse.Processing.org/t/snow/7156/3 """ from math import copysign FLAKES, YMIN, YMAX = 1001, -500, 50 RANGE = tuple(range(FLAKES))…

```auto
    k = key.upper() if key != CODED else keyCode

    if k == 'W' or k == UP:
        for f in flakes: f.vel.y -= f.maps[2]

    elif k == 'S' or k == DOWN:
        for f in flakes: f.vel.y += f.maps[2]

    elif k == 'A' or k == LEFT:
        for f in flakes: f.vel.x -= f.maps[2]

    elif k == 'D' or k == RIGHT:
        for f in flakes: f.vel.x += f.maps[2]

```

> [@Eb3yr](#):
>
> `bodies.Append(`

Ru sure that method is capitalized? If that’s a list type, it’s lower case instead: `bodies.append(`
