# How to make a key to become a integer and use the key on keyPressed?

**URL:** <https://discourse.processing.org/t/how-to-make-a-key-to-become-a-integer-and-use-the-key-on-keypressed/17822>\
**Category:** Processing.py\
**Created:** [February 14, 2020, 5:20pm UTC](https://discourse.processing.org/t/how-to-make-a-key-to-become-a-integer-and-use-the-key-on-keypressed/17822 "2020-02-14T17:20:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![macedorrn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/macedorrn/32/8097_2.png) [@macedorrn](https://discourse.processing.org/u/macedorrn)\
**Post date:** [February 14, 2020, 5:20pm UTC](https://discourse.processing.org/t/how-to-make-a-key-to-become-a-integer-and-use-the-key-on-keypressed/17822/1 "2020-02-14T17:20:47Z")

</div>

[Python] I am a beginner  
I’ve been having troubles trying to first convert the value of a key (any key 0-9) returning an integer value so I can use this value whenever I press the key.  
I am supposed to make a square increase the size by the value of the key.  
my code so far is:

```auto
def calculate_size(diameter, key):
    diameter = 50
    key = int(key)
    diameter = diameter ** key
    return diameter ** int('key')

```

Then, I have no idea how to input this function in the keyPressed function.

---

<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 14, 2020, 6:04pm UTC](https://discourse.processing.org/t/how-to-make-a-key-to-become-a-integer-and-use-the-key-on-keypressed/17822/2 "2020-02-14T18:04:07Z")

</div>

```auto
ZERO, NINE = ord('0'), ord('9')
diam = 50

def setup():
    size(700, 700)
    noLoop()

def draw():
    background(int(random(PImage.ALPHA_MASK)))
    circle(width >> 1, height >> 1, diam)
    print diam

def keyPressed():
    global diam
    if key.isdigit(): diam += keyCode - ZERO
    redraw()

```

---

<div class="post-metadata">

**Author:** ![macedorrn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/macedorrn/32/8097_2.png) [@macedorrn](https://discourse.processing.org/u/macedorrn)\
**Post date:** [February 15, 2020, 8:42am UTC](https://discourse.processing.org/t/how-to-make-a-key-to-become-a-integer-and-use-the-key-on-keypressed/17822/3 "2020-02-15T08:42:22Z")

</div>

I have tried many ways to fix this issue, I feel it’s the only thing missing. the size of the circle should increase whenever I press a numerical key, and the size must remain whenever I click somewhere on canvas. So far, the program doesn’t crash when I press a key, but nothing happens to its size either. My work is:

```auto
circle_x = 250
circle_y = 250
circle_size = 50
diameter = 50

def calculate_size(diameter, key):
    return diameter ** key

def setup():
    size(500, 500)
    return 

def draw():
    global circle_x, circle_y, circle_size
    background(0, 0, 40)
    ellipse(circle_x,circle_y, circle_size, circle_size)

def mouseClicked():
    global circle_x, circle_y
    circle_x = mouseX
     circle_y = mouseY

def keyPressed():
    global circle_size, circle_x, circle_y, diameter
    if key == int(key):
        circle_size(circle_x, circle_y, calculate_size(diameter, key), calculate_size(diameter, key))

```

---

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [February 15, 2020, 9:45am UTC](https://discourse.processing.org/t/how-to-make-a-key-to-become-a-integer-and-use-the-key-on-keypressed/17822/4 "2020-02-15T09:45:45Z")

</div>

How about this:

```auto
def calculate_size(diameter, key):
    return diameter * key

...
 
def keyPressed():
    global circle_size, circle_x, circle_y, diameter
    if key.isdigit():
        circle_size = calculate_size(diameter, int(key))

```

I had to modify the `calculate_size()` function because the circles were massive.

---

<div class="post-metadata">

**Author:** ![macedorrn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/macedorrn/32/8097_2.png) [@macedorrn](https://discourse.processing.org/u/macedorrn)\
**Post date:** [February 15, 2020, 3:50pm UTC](https://discourse.processing.org/t/how-to-make-a-key-to-become-a-integer-and-use-the-key-on-keypressed/17822/5 "2020-02-15T15:50:11Z")

</div>

Thanks for the help. It worked now!!
