Hi,
I’m playing with GLFW since it is now the window system for LWJGL.
I intercept key events from a GLFWKeyCallback (i’m not using the unicode char version because i want handle special keys like CAPS_LOCKS…)
I’m french and have an AZERTY keyboard and GLFW don’t do any keyboard layout mapping and provide always keycode in the US-Layout (So it’s QWERTY).
That mean, if i press “A” on my keyboard i receive the event “Q” from GLFW.
I’m starting to perform the mapping manually regarding the country of the swing method :
InputContext.getInstance().getLocale();
But it’s a pain, dirty and i’m not sure to achieve this with a robust solution.
So, have you other alternatives ?
At this time what tried to consider ?
- using scancode instead of keycode on a way that i can convert it to an awt key code by example (but i don’t find anything on it)
- Be able to change GLFW configration in order that it do the keyboard layout itself (but, it seems that their standard behaviour is to map to US-Keyboard only)
- using a third-party that will do that better than me (but at this time i don’t find it)
Best regards,
Here my local mapping that i use but it’s not cool:
private enum KeyboardLayout {
Qwerty,
Qwertz,
Azerty;
}
private static String country = null;
private static KeyboardLayout layout = null;
private static int mapKeyboardLayout( int glfwKeyCode ) {
if( country == null ) {
Locale locale = InputContext.getInstance().getLocale();
if( locale == null ) locale = Locale.getDefault();
country = locale.getCountry().toLowerCase();
layout = getKeyboardLayout(country);
}
switch( layout ) {
case Qwerty : return toQwertyLayout( glfwKeyCode , country );
case Qwertz : return toQwertzLayout( glfwKeyCode , country );
case Azerty : return toAzertyLayout( glfwKeyCode , country );
}
return glfwKeyCode;
}
private static KeyboardLayout getKeyboardLayout( String country ) {
if( country.equals("fr") ) return KeyboardLayout.Azerty; // FRANCE
if( country.equals("hr") ) return KeyboardLayout.Qwertz; // CROATIA
if( country.equals("si") ) return KeyboardLayout.Qwertz; // SLOVENIA
if( country.equals("cz") ) return KeyboardLayout.Qwertz; // CZECH REPUBLIC
if( country.equals("hu") ) return KeyboardLayout.Qwertz; // HUNGARY
if( country.equals("pl") ) return KeyboardLayout.Qwertz; // POLAND
if( country.equals("sk") ) return KeyboardLayout.Qwertz; // SLOVAKIA (Slovak Republic)
if( country.equals("DE") ) return KeyboardLayout.Qwertz; // GERMANY
return KeyboardLayout.Qwerty;
}
private static int toQwertyLayout( int glfwKeyCode , String country ) {
return glfwKeyCode;
}
private static int toQwertzLayout( int glfwKeyCode , String country ) {
switch( glfwKeyCode ) {
case GLFW.GLFW_KEY_Z : return GLFW.GLFW_KEY_Y;
case GLFW.GLFW_KEY_Y : return GLFW.GLFW_KEY_Z;
default : return glfwKeyCode;
}
}
private static int toAzertyLayout( int glfwKeyCode , String country ) {
switch( glfwKeyCode ) {
case GLFW.GLFW_KEY_A : return GLFW.GLFW_KEY_Q;
case GLFW.GLFW_KEY_Q : return GLFW.GLFW_KEY_A;
case GLFW.GLFW_KEY_W : return GLFW.GLFW_KEY_Z;
case GLFW.GLFW_KEY_Z : return GLFW.GLFW_KEY_W;
// ....... and so one
default : return glfwKeyCode;
}
}