# Numpad keycodes are acting weird?

**URL:** https://discourse.processing.org/t/numpad-keycodes-are-acting-weird/43917
**Category:** Coding Questions
**Created:** [February 15, 2024, 6:33am UTC](https://discourse.processing.org/t/numpad-keycodes-are-acting-weird/43917 "2024-02-15T06:33:09Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Edenstudent](https://avatars.discourse-cdn.com/v4/letter/e/58956e/32.png) [@Edenstudent](https://discourse.processing.org/u/Edenstudent)
#### Post date: [February 15, 2024, 6:33am UTC](https://discourse.processing.org/t/numpad-keycodes-are-acting-weird/43917/1 "2024-02-15T06:33:09Z")

</div>

So, I know that the keyCodes are different for the numpad and that they actually if you have NumLock on or off. I have a little program that gets the numbers for me:

```auto
void setup() {
}

void draw() {
}

void keyPressed() {
  println(key, keyCode);
}

```

So, for the 5 button, it’s supposed to be either 101 or 12 (numLock on vs or off, respectively) as it is with the above program. However, my big program which scans with button inputs, is acting weird. For the numLock on/off, it’s giving 133/12.  
Also, for the 9, it should be 105/33, but instead is 137/16, and 16 is supposed to be the shift keyCode, which is making things even more troublesome… so both can be wrong.

(the function is just meant to active on button presses and set the Input objects, which have pre-saved codes, to true if it matches… shouldn’t be changing anything about the keyCode itself)

```auto
void keyPressed() {
  println("code:", keyCode); // check what was just pressed
  // update inputs
  for (int i = 0; i < inputs.length; i++)
    // go through inputs and check if the input has a corresponding keyCode, and set the pressed of the same spot to true
    for (int j = 0; j < inputs[i].code.length; j++)
      for (int k = 0; k < inputs[i].code[j].length; k++)
        if (keyCode == inputs[i].code[j][k])
          inputs[i].pressed[j] = true;
}

```

Here’s a small table with the numLock on/off and what it should be (correct) vs what it is instead (false), in case there’s a pattern.  
Button correct false  
1 97/35 129/3  
2 98/40 130/40  
3 99/34 131/11  
4 100/37 132/37  
5 101/12 133/12  
6 102/39 134/39  
7 103/36 135/2  
8 104/38 136/38  
9 105/33 137/16

I can see that it’s offset on the numLock on being +22, but not sure how or why it’s like that. When numLock is off and they are functions, only the corners (1, 3, 7, 9) are different while the arrows (and 5) are the same.  
Also, the “home” key above the numLock that shares the same function as the 7 when numLock is off has the same code in both programs as whichever the 7 is, same with the delete key and delete beside the numPad 0… but none of the letters or normal functions (enter, backspace, space, shift) are showing that behaviour.

If anyone knows about how this works or why it might be happening between the 2 programs, I would greatly appreciate any insight, as I can’t seem to figure out why the codes are acting differently.

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [February 15, 2024, 11:01am UTC](https://discourse.processing.org/t/numpad-keycodes-are-acting-weird/43917/2 "2024-02-15T11:01:41Z")

</div>

Hello @Edenstudent,

There is a comment in the reference about _issues with how keyCode behaves across different renderers and operating systems_:

> **[keyCode / Reference](https://processing.org/reference/keyCode.html)**
>
> The variable keyCode is used to detect special keys such as the UP, DOWN, LEFT, RIGHT arrow keys and ALT, CONTROL, SHIFT. When checking for these keys, it can be useful to first chec…

Try you code with:  
`size(100, 100, P2D);`

I get these results on W10 with Processing 4.3:

```auto
   JAVA2D P2D
1 97/35 129/3
2 98/40 130/40
3 99/34 131/11
4 100/37 132/37
5 101/12 133/12
6 102/39 134/39
7 103/36 135/2
8 104/38 136/38
9 105/33 137/16

```

JAVA2D is the default renderer and these are the same:

```auto
size(100, 100);
size(100, 100, JAVA2D);

```

`:)`

---

<div class="post-metadata">

### Author: ![Edenstudent](https://avatars.discourse-cdn.com/v4/letter/e/58956e/32.png) [@Edenstudent](https://discourse.processing.org/u/Edenstudent)
#### Post date: [February 15, 2024, 8:14pm UTC](https://discourse.processing.org/t/numpad-keycodes-are-acting-weird/43917/3 "2024-02-15T20:14:26Z")

</div>

Oh, you’re right, I didn’t think about how the renderer would change the keyCode (for some reason); yes, my large program uses P2D (because of image rendering reasons that JAVA2D can’t keep up with). The major issue is the 9 being 16 though, which should only be used by the shift key (and is in both renderers); any idea how to work around that? I can change it if I need, but it is a troublesome nuisance.

---

<div class="post-metadata">

### Author: ![Edenstudent](https://avatars.discourse-cdn.com/v4/letter/e/58956e/32.png) [@Edenstudent](https://discourse.processing.org/u/Edenstudent)
#### Post date: [February 15, 2024, 8:47pm UTC](https://discourse.processing.org/t/numpad-keycodes-are-acting-weird/43917/4 "2024-02-15T20:47:03Z")

</div>

I did change it around so as not to include 9, which is fine. But this rendering issue has also led to this, which I can’t seem to find a solution to as well 😬

> [@App Icon Processing 4](https://discourse.processing.org/t/app-icon-processing-4/43918):
>
> I’m trying to set the icon for the application (and in the task bar) in Processing4, and I have this: PImage icon = loadImage("icon.png"); surface.setIcon(icon); The image is 778x758; it doesn’t show up though in the program or task bar, so I’m not sure what to do here as this is the documentation: [https://processing.github.io/processing-javadocs/core/processing/core/PSurface.html#setIcon-processing.core.PImage-](https://processing.github.io/processing-javadocs/core/processing/core/PSurface.html#setIcon-processing.core.PImage-)

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [February 15, 2024, 9:32pm UTC](https://discourse.processing.org/t/numpad-keycodes-are-acting-weird/43917/5 "2024-02-15T21:32:17Z")

</div>

> [@Edenstudent](#):
>
> The major issue is the 9 being 16 though, which should only be used by the shift key (and is in both renderers); any idea how to work around that? I can change it if I need, but it is a troublesome nuisance.

There is an issue here related to this:

> <https://github.com/benfry/processing4/issues/702>
>
> Hi guys,
> 
> thanks for your excellent work on Processing 4. Sorry to report this… annoying issue making it essentially impossible to detect the difference between SHIFT and PAGE\_UP when using the P2D renderer. Problem: Both use keyCode = 16 :-(
> 
> This is not a problem under the DEFAULT renderer. It uses keyCodes 33-35 for HOME/END/PG\_UP/PG\_DN and 16 for SHIFT.
> P2D however uses keyCodes 2, 3, 16(?) and 11 for the above keys and again 16 for SHIFT.
> 
> Btw, I am typing on a German keyboard but for the keys involved that shouldn't make any difference, I guess.
> 
> Good luck! slu4.
> 
> void setup() { size(200, 200, P2D); }
> void draw() {}
> void keyPressed() { println(key, int(key), key==CODED, keyCode); }

`:)`

---

<div class="post-metadata">

### Author: ![Edenstudent](https://avatars.discourse-cdn.com/v4/letter/e/58956e/32.png) [@Edenstudent](https://discourse.processing.org/u/Edenstudent)
#### Post date: [February 16, 2024, 7:28am UTC](https://discourse.processing.org/t/numpad-keycodes-are-acting-weird/43917/6 "2024-02-16T07:28:48Z")

</div>

Sorry to ask this but small follow up question; is there a way to distinguish between the arrow keys and the numPad (when numLock is off)? I’ve been playing around with it and trying to read up on it but can’t seem to find the answer.
