# keyPressed is behaving very oddly

**URL:** <https://discourse.processing.org/t/keypressed-is-behaving-very-oddly/16865>\
**Category:** Beginners\
**Created:** [January 4, 2020, 7:17pm UTC](https://discourse.processing.org/t/keypressed-is-behaving-very-oddly/16865 "2020-01-04T19:17:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Psteely](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/psteely/32/7647_2.png) [@Psteely](https://discourse.processing.org/u/Psteely)\
**Post date:** [January 4, 2020, 7:17pm UTC](https://discourse.processing.org/t/keypressed-is-behaving-very-oddly/16865/1 "2020-01-04T19:17:59Z")

</div>

This is going to be something dumb but…

I have a code snipet showing my issue.

```auto
int currentRow;
void setup() {
}
void draw() {
}

void keyPressed() {
  currentRow = key;
  println(key);
  println(currentRow);
}

```

If I run the avove code and press the number 5 I get  
5  
53  
in the console

If I run

```auto
int currentRow;

void setup() {
}
void draw() {
}

void keyPressed() {
  currentRow = key-48;
  println(key);
  println(currentRow);
}

```

I get  
5  
5  
in the console…  
Why oh why do I need the -48 for this to work. It seems to be a difference between the key and the keyCode but I make no reference to the keyCode.

I can Obvs work around it but my internal OCD won’t let it lie.

Thanks

---

<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:** [January 4, 2020, 7:44pm UTC](https://discourse.processing.org/t/keypressed-is-behaving-very-oddly/16865/2 "2020-01-04T19:44:54Z")

</div>

Field PApplet::_key_ is of datatype `char`:

- [Processing.GitHub.io/processing-javadocs/core/processing/core/PApplet.html#key](http://Processing.GitHub.io/processing-javadocs/core/processing/core/PApplet.html#key)
- [Processing.org/reference/char.html](http://Processing.org/reference/char.html)

While field PApplet::_keyCode_ is of datatype `int`:

- [Processing.GitHub.io/processing-javadocs/core/processing/core/PApplet.html#keyCode](http://Processing.GitHub.io/processing-javadocs/core/processing/core/PApplet.html#keyCode)
- [Processing.org/reference/int.html](http://Processing.org/reference/int.html)

Even though a `char` is an unsigned 16-bit (2-byte) integer datatype, when it’s converted to a String (for example, when we use **println()** on it) its numerical value is converted to a UTF-16 character representation.

Notice that Java’s minimum datatype for math calculation is the signed 32-bit (4-byte) `int` datatype.

So when we have an expression like this key - 48, or even such as this key - ‘0’, both operands are coerced up to datatype `int` before it proceeds w/ the operator `-` minus.

If you hit the key 5, field _key_ will contain the value `'5'`, which when converted to `int` becomes `53`.

Therefore key - 48 is coerced to 53 - 48.  
The same case for key - ‘0’, which is coerced to 53 - 48 as well.

---

<div class="post-metadata">

**Author:** ![Psteely](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/psteely/32/7647_2.png) [@Psteely](https://discourse.processing.org/u/Psteely)\
**Post date:** [January 4, 2020, 7:54pm UTC](https://discourse.processing.org/t/keypressed-is-behaving-very-oddly/16865/3 "2020-01-04T19:54:40Z")

</div>

Thanks for the speedy info, that sounds like a lot of coercion 🙂  
Will have a review of the attached docs. It had me confused for a while TBH.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 4, 2020, 9:00pm UTC](https://discourse.processing.org/t/keypressed-is-behaving-very-oddly/16865/4 "2020-01-04T21:00:00Z")

</div>

> [@GoToLoop](#):
>
> which when converted to `int` becomes `53` .

That’s the ASCII value by the way

0 being 48 in Ascii etc.

> **[ASCII | Printable characters](https://en.wikipedia.org/wiki/ASCII#Printable_characters)**
>
> Codes 20hex to 7Ehex, known as the printable characters, represent letters, digits, punctuation marks, and a few miscellaneous symbols. There are 95 printable characters in total.\[n\]
> Code 20hex, the "space" character, denotes the space between words, as produced by the space bar of a keyboard. Since the space character is considered an invisible graphic (rather than a control character): 223  it is listed in the table below instead of in the previous section.
> Code 7Fhex corresponds to the non-p...
