Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How to detect keyboard layout?  (Read 3962 times)

0 Members and 1 Guest are viewing this topic.

VCras

  • Newbie
  • *
  • Posts: 6
    • View Profile
How to detect keyboard layout?
« on: March 07, 2017, 04:23:02 pm »
I'm using the latest version of SFML with C++. I want to program my controls so that they change depending on keyboard layout. I have 2 questions:

1.) How can I detect the layout of the users' keyboard (QWERTY, AZERTY, Dvorak, etc)? (I do not want to ask the player for their keyboard layout. It's not professional.) If SFML can't help (I don't see anything in the docs), could you tell me a cross platform way of doing it (ideally with user-friendly documentation)?

Note: Mac OS is not supported in https://github.com/SFML/SFML/pull/568. So this is not a solution.

2.)Let's say, (for the sake of argument), I'm using AZERTY and I press Q. Will this activate sf::Keyboard::isKeyPressed(sf::Keyboard::Q)? Or will it activate sf::Keyboard::isKeyPressed(sf::Keyboard::A) (A being the key in QWERTY that is in the same place as Q in AZERTY)?

Thanks! Sorry if I seem demanding at all; I'm in quite a muddle. I'd greatly appreciate your help  ;D.


eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: How to detect keyboard layout?
« Reply #1 on: March 07, 2017, 04:41:15 pm »
There's no automatic way to detect the keyboard layout with SFML (yet).

However, as long as you provide a way to change key bindings, it will be on a good track. Yes, it might be annoying that one has to adjust the keys when starting the game, but it's also not very uncommon. For example, I don't know many games who will detect that my Swiss-German keyboard layout has Y and Z swapped. It's annoying that I have to change it, but as long as I can change it, it remains a minor annoyance.

Depending on how many keys you use, I've also seen games ask you to press the relevant keys at the start of the game and will automatically bind them. This fulfills two purposes, you don't have to select a default set of key bindings and you're already teaching the user all the available keys and actions. Of course this doesn't work for everything.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

VCras

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: How to detect keyboard layout?
« Reply #2 on: March 07, 2017, 04:49:58 pm »
Thank you for your swift reply  :D. I'd still like an answer to my questions.
Detecting layout seems like a very basic feature - don't other game libraries do it?
I don't want to annoy my players at all.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
How to detect keyboard layout?
« Reply #3 on: March 07, 2017, 05:08:16 pm »
1) I don't know exactly how other libraries do it. I assume many will support scan codes, which are layout independent.
As layout detection would be dependent on the different OS, there isn't a cross-platform way for it, but maybe someone has written a library for it?

2) It can be detected with Q. The key names of the current layout will be matching the SFMK keyboard keys. It's layout specific.

I can also recommend to take a look at Thor's action system: http://www.bromeon.ch/libraries/thor/tutorials/v2.0/actions.html
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

VCras

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: How to detect keyboard layout?
« Reply #4 on: March 07, 2017, 05:37:08 pm »
Thank you. Thor looks interesting  :). If anyone knows of a cross-platform, documented library for doing detecting keyboard layouts (Edit: or which would provide scan code support), could you let me know?
« Last Edit: March 07, 2017, 05:39:19 pm by VCras »

 

anything