Hello Guest

mapping keys using keyboard locale

  • 3 Replies
  • 6415 Views
*

Offline NateS

  • **
  • 91
    • Esoteric Software
mapping keys using keyboard locale
« on: October 25, 2013, 17:10:10 »
Hi guys! I have a question about how LWJGL works on OS X. I notice this:

If I'm using a QWERTY layout and I press Y, I get a keycode for Y in keyPressed.
If I'm using a QWERTZ layout and I press the same physical key, I get a keycode for Z in keyPressed.

I like this behavior, but I am trying to understand how is is accomplished in LWJGL. It seems that LWJGL is using NSEvent keyCode:
https://github.com/LWJGL/lwjgl/blob/master/src/native/macosx/org_lwjgl_opengl_Display.m#L283

When I use GLFW I see this:

If I'm using a QWERTY layout and I press Y, I get a keycode for Y in keyPressed.
If I'm using a QWERTZ layout and I press the same physical key, I get a keycode for Y in keyPressed.

GLFW also uses NSEvent keyCode:
https://github.com/badlogic/jglfw/blob/master/jglfw/jni/glfw-3.0/src/cocoa_window.m#L464

I'm confused, if both are just using the NSEvent keyCode, how is LWJGL respecting the user's keyboard locale while GLFW is not?

NSEvent reference:
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSEvent_Class/Reference/Reference.html#//apple_ref/occ/instm/NSEvent/keyCode
This says: "The virtual key code. The returned value is hardware-independent." I take this to mean that the keyCode I get when I press Y with a QWERTY layout is the same code when the same physical key is pressed in the QWERTZ.

This text from HIToolbox/Events.h is even more explicit:
Quote
   /*
    *  Summary:
    *    Virtual keycodes
    * 
    *  Discussion:
    *    These constants are the virtual keycodes defined originally in
    *    Inside Mac Volume V, pg. V-191. They identify physical keys on a
    *    keyboard. Those constants with "ANSI" in the name are labeled
    *    according to the key position on an ANSI-standard US keyboard.
    *    For example, kVK_ANSI_A indicates the virtual keycode for the key
    *    with the letter 'A' in the US keyboard layout. Other keyboard
    *    layouts may have the 'A' key label on a different physical key;
    *    in this case, pressing 'A' will generate a different virtual
    *    keycode.
    */

So it would seem that NSEvent keyCode is supposed to represent the physical key and does not take into account the user's keyboard locale. If this is the case, how does LWJGL respect the user's keyboard locale on OS X?

*

Offline kappa

  • *****
  • 1316
Re: mapping keys using keyboard locale
« Reply #1 on: October 26, 2013, 10:12:17 »
Don't think LWJGL does anything special here, it just uses whatever KeyCode the Cocoa NSEvent provides it.

In LWJGL the NS KeyCode conversion to LWJGL key takes place in MacOSXNativeKeyboard.java.

p.s. just curious, you mentioned a while back that there was something lacking in LWJGL on OS X hence you had to use JGLFW, theres been a few fixes in the latest nightly builds for OS X LWJGL, do those fix the problems you had or are there still some outstanding?
« Last Edit: October 27, 2013, 15:46:29 by kappa »

*

Offline NateS

  • **
  • 91
    • Esoteric Software
Re: mapping keys using keyboard locale
« Reply #2 on: October 26, 2013, 13:52:23 »
Aye, both LWJGL and GLFW seem to just use the keyCode, and of course translate it to their respective key constants. Something has to be different though, because LWJGL definitely is magically respecting the keyboard locale. I'll look deeper at how the window is created, etc.

JGLFW has been working so I haven't had a chance to try the latest LWJGL. If it ain't broke, I don't want to go around fixing it. :) There are usually a lot of small details when trying to get it to work like it used to, and dealing with OS X specific issues is extra, extra painful.

*

Offline NateS

  • **
  • 91
    • Esoteric Software
Re: mapping keys using keyboard locale
« Reply #3 on: October 27, 2013, 13:43:47 »
I can't find LWJGL doing anything special. NSEvent keyCode should always be the same when I press the same key, no matter the keyboard type chosen. This is the case for JGLFW, but not LWJGL. Why?!  ???