Numpad keycodes are acting weird?

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:

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)

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.

1 Like

Hello @Edenstudent,

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

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

I get these results on W10 with Processing 4.3:

   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:

size(100, 100);
size(100, 100, JAVA2D);

:)

3 Likes

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.

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 :grimacing:

There is an issue here related to this:

:)

1 Like

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.