benfry / processing4

Processing 4.x releases for Java 17
https://processing.org
Other
1.33k stars 239 forks source link

`SHIFT` and `PAGE_UP` keys use the same `keyCode` in `P2D` and `P3D` #702

Open slu4coder opened 1 year ago

slu4coder commented 1 year ago

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); }

Chirimen-Jako commented 1 year ago

I was also trying to submit the exact same issue, but I'm adding it here since I found this report. I am using the P3D renderer and a Japanese keyboard. Like slu4coder, I cannot distinguish between the [Shift] key and the [Page Up] key because they both return the same keyCode 16.

Additionally, I would like to ask if this issue is dependent on the hardware type of the keyboard.

// Processing 4.2 on Windows 10 22H2 build 19045.3086
void setup(){
  size(320, 240, P3D);
  background(255);
}
void keyPressed(){
  // [Shift] returns '16 0x10'
  // [Page Up] returns '16 0x10'
  int kc = keyCode;
  println(kc + " 0x" + hex(kc, 2));
}
void draw(){}
Chirimen-Jako commented 1 year ago

I found a workaround.

// Processing 4.2 on Windows 10 22H2 build 19045.3086
void setup(){ size(320, 240, P3D); }
void draw(){}
void keyPressed(KeyEvent e){

  // Default renderer
  //java.awt.event.KeyEvent nativeEvent = (java.awt.event.KeyEvent)e.getNative();

  // P2D/P3D renderer
  com.jogamp.newt.event.KeyEvent nativeEvent = (com.jogamp.newt.event.KeyEvent)e.getNative();

  int kcd = keyCode; // default
  int kcn = nativeEvent.getKeyCode(); // native

  // renderer:default
  // keyCode   :  default   native         
  // [Shift]   :  16 0x10 / 16 0x10
  // [Page Up] :  33 0x21 / 33 0x21

  // renderer:P2D/P3D
  // keyCode   :  default   native         
  // [Shift]   :  16 0x10 / 15 0x0F
  // [Page Up] :  16 0x10 / 16 0x10

  println(kcd, "0x" + hex(kcd, 2), "/", kcn, "0x" + hex(kcn, 2));
}
benfry commented 1 year ago

Ok, these should be getting mapped properly to the same values as in the Java2D renderer.