PDA

View Full Version : How to get the current layout of keyboard



junior-root
19th September 2017, 11:02
Hi,
I need to know the current layout of keyboard.
I cannot use QEvent::KeyboardLayoutChange (https://bugreports.qt.io/browse/QTBUG-27681) or QApplication::keyboardInputLocale() which is obsolete.

The ideal solution would be to know exactly when user change keyboard layout. But I will be happy with any solution that give me the current layout of keyboard.
PS This is a little game, not a text editor.

Thanks for the time spent reading this message.

d_stranz
19th September 2017, 17:20
I do not know if there is really a way to get the actual physical layout of the keys on the keyboard. At the keyboard driver level, the driver certainly knows which physical key has which character on it, but by the time it gets to the application layer, all of the physical keys have been mapped to virtual keys and there isn't an API that lets you go backward to physical location (at least not that I know of in Qt anyway).

Instead of trying to map specific keys to specific things in your game app yourself, why don't you let your users do it? For example, you could give them a default table of key -> action mappings ("A" = move left, "W" = move up, "S" = move left, "Z" = move down, for example) and then let them change them to suit their own preferences. Maybe someone has trouble using their left hand and wants to map these to the "O", "K", "L" and "," keys instead for right-hand use. Give them a "setup keyboard" UI that lets them change things to suit their own keyboard and personal preferences. All you have to do is to store these in a configuration file (try QSettings) and read it into an internal mapping table at run time.