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

Author Topic: Better keyboard handling  (Read 68136 times)

0 Members and 1 Guest are viewing this topic.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Better keyboard handling
« Reply #30 on: November 26, 2013, 05:47:29 pm »
However the default symbol for a key may not be the most "standard" one; for example, on my french keyboard, pressing '1' yields a '&', and to get a '1' I have to press shift. SDL has a special handling for these keys, so that the numbers are always reported, even if they are not the default symbol. Same for 'รน' and '%', etc.
Since SDL as well as games seem to interpret the number keys always as numbers, why don't we do it, too?

Should modifier keys (shift, alt, control) be interpreted or not? In other words, does the reported symbol changes when we press a modifier key, or should there be only one symbol reported for each key?
I find it most intuitive if every key enumerator corresponds to one physical key (and if the keyboard has too few keys, some enumerators are unused), and thus, modifiers are not taken into account. In most games, modifiers are used explicitly; the worst I could imagine is a reverse-mapping on developer side to make different keyboards behave similarly.

The question is then, how do we name the special keys (comma, dash, ...). I would suggest to use the American layout as reference. Figuring out which key corresponds on a different key layout looks acceptable to me...

About scancodes, how did you imagine to provide them API-wise? Also as part of the sf::Event object created by the KeyPressed event?
« Last Edit: November 26, 2013, 05:49:45 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Better keyboard handling
« Reply #31 on: November 26, 2013, 07:54:05 pm »
Quote
I don't see how that is possible considering the symbols on the physical keys changes depending on the keyboard. For example you have a '&' and a '1' on your first number key, yet on my US layout I have '1' and '!' together on my first key.
So SFML would report '&' and '1' on a french keyboard, and '1' and '!' on a US keyboard. Since both contain '1', your weapon selection code can work in both cases.
But it's just an idea. It doesn't seem to be a standard way of doing it.

Quote
Since SDL as well as games seem to interpret the number keys always as numbers, why don't we do it, too?
I would prefer finding a general rule, but if it's the best solution I'll do it. Then we should also think about all the other keys, it's not just about numbers.

Quote
The question is then, how do we name the special keys (comma, dash, ...). I would suggest to use the American layout as reference. Figuring out which key corresponds on a different key layout looks acceptable to me...
Something like this?

if (event.type == sf::Event::KeyPressed)
{
    if (layout == "us" && event.key.code == sf::Keyboard::Num1
     || layout == "fr" && event.key.code == sf::Keyboard::Ampersand
     || layout == ".." && event.key.code == ..
     || ...)
    {
        // '1' was pressed :)
    }
???

Quote
About scancodes, how did you imagine to provide them API-wise? Also as part of the sf::Event object created by the KeyPressed event?
Yes, and the corresponding functions in sf::Keyboard.
Laurent Gomila - SFML developer

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Better keyboard handling
« Reply #32 on: November 26, 2013, 07:56:48 pm »
Just a new idea: maybe a reverse lookup function would do the job

if (event.key.scancode == sf::Keyboard::scancode(sf::Keyboard::Num1)) // if same physical key as '1', whatever its default symbol is
{
    // '1' is pressed
}
Laurent Gomila - SFML developer

wintertime

  • Sr. Member
  • ****
  • Posts: 255
    • View Profile
Re: Better keyboard handling
« Reply #33 on: November 26, 2013, 08:20:19 pm »
There seem to be 3 sets of scancodes, but most keyboards would use same set.
According to http://retired.beyondlogic.org/keyboard/keybrd.htm you could mostly just do:
if(event.key.scancode == 0x16)
{
    event.key.code = sf::Keyboard::Num1;
}
 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Better keyboard handling
« Reply #34 on: November 26, 2013, 08:55:18 pm »
According to SDL source code, yes, there are standards for scancodes. They refer to the USB standard, but I haven't had a look at it. However the SDL sources have different lookup tables for each OS. Maybe that's something worth investigating.

But how would it help? Obviously you would have to name scancodes (sf::Scancode::Num1 instead of 0x16), like SDL does, but you just move the problem from key codes to scancodes, i.e. which scancode is returned when you press '1' on a french keyboard: Scancode::Num1 or Scancode::Ampersand?
Laurent Gomila - SFML developer

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
Re: Better keyboard handling
« Reply #35 on: November 26, 2013, 09:10:02 pm »
I still can't see how providing constants for dynamic things shall work at all.. If Scancode::Num1 can be different across platforms/keyboard, then I think the design is flawed.

wintertime

  • Sr. Member
  • ****
  • Posts: 255
    • View Profile
Re: Better keyboard handling
« Reply #36 on: November 26, 2013, 10:11:29 pm »
Keyboards are so quirky you always have to remap some codes. But from what I read at http://www.scs.stanford.edu/10wi-cs140/pintos/specs/kbd/scancodes-9.html#ss9.5 (there are many more pages dedicated to scan codes including pictures with national mappings) they are much more standardized than national layouts.
Set 1 and 3 seem to be out of fashion for a long time (whos gonna run a XT with an 8088 cpu and set 1?), set 2 is used mostly and also USB-legacy support produces set 2 only. Though some linuxes may try to run with set 3 (I hope there is some setting available to read from OS to tell this situation).
It obviously is a difficult thing and needs much testing, but I would think most keys would produce same scan code on same physical key position on different layouts. And it feels better than back-translating 100 localized layouts.

Edit: Its even more complicated, windows may map the perfectly good set 2 scan codes to set 1 scan code. http://download.microsoft.com/download/1/6/1/161ba512-40e2-4cc9-843a-923143f3456c/scancode.doc
« Last Edit: November 26, 2013, 10:42:35 pm by wintertime »

Josh_M

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: Better keyboard handling
« Reply #37 on: November 26, 2013, 11:08:07 pm »
As someone completely uneducated on the topic, what is wrong with using the USB codes on that list from wintertime? I assume it's because some embedded/propriety formats (ie: laptops) or PS/2 connections have different input.

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Re: Better keyboard handling
« Reply #38 on: November 27, 2013, 02:07:44 am »
The most common input handling should always work with scancodes IMO. I simply hate having games with odd controls just because someone based it on the QWERTY layout (e.g. having to use X and Z, while Z and Y are swapped on German layouts compared to English ones).

Events like "Text entered" could report the resulting key.

Also to note: At least SDL 1.2 was inconsistent with keys as well, not sure how/if that's changes since then though.

Scancode/key conversion: Never do it manually! This should be done on operating system level.

Also there should be some easy accessible way to convert a scancode to a readable "label" for that specific key. This one would have to be based on the current keyboard layout.

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
Re: Better keyboard handling
« Reply #39 on: November 27, 2013, 08:05:51 am »
I agree to Mario. From what I understood it's exactly what I proposed as well. :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Better keyboard handling
« Reply #40 on: November 27, 2013, 08:26:11 am »
Answering "we must use scancodes" is not enough. How do you identify them in the API (a number or a named constant)? How would they be used (how do you implement consistent WSAD movement in your game)? How do they solve the various problems that were mentioned in this thread?

I really think we need some details now ;)
Laurent Gomila - SFML developer

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
Re: Better keyboard handling
« Reply #41 on: November 27, 2013, 08:37:49 am »
What if SFML allows to identify the keyboard model and provides enums for the most common used standardized scan codes? This allows to hardcode key mappings in source code, depending on the active layout of the user.

So, when a key event in SFML is triggered, it carries the scan code + a readable label (character itself or name of special key). The developer can then go and compare it with the active model's scan mode mapping table, like this:

if( event.type == sf::Event::KeyPressed ) {
  if( event.key.scanCode == sf::Keyboard::toScanCode( sf::Keyboard::Q ) ) {
    std::cout << "Key " << event.key.label << " was pressed." << std::endl;
    // ...
  }
}

getScanCode translates into a scan code that's valid for the active keyboard layout. Maybe there could even be a sf::KeyboardLayout that holds the key code<->scan code mappings shown above.

sf::Keyboard::Q is still a key code, a way to specify the location of the key I want to listen for. This just has to be defined in SFML and can't be implemented to work localized for everybody (i.e. when I, as a German, give sf::Keyboard::Z, then the QWERTY Z key will be targeted, not the QWERTZ Z key).

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Better keyboard handling
« Reply #42 on: November 29, 2013, 04:21:13 pm »
No more idea / comment / whatever?
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Better keyboard handling
« Reply #43 on: November 29, 2013, 04:37:06 pm »
What were the main problems with the current system, apart from missing keys? If that's clear for everyone, it might be easier to focus on a corresponding solution...
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Better keyboard handling
« Reply #44 on: November 29, 2013, 04:46:15 pm »
Quote
What were the main problems with the current system, apart from missing keys?
Everything that is explained in the first post ;)
Laurent Gomila - SFML developer