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

Author Topic: [SOLVED] Problem handling multiple key stroke  (Read 3486 times)

0 Members and 1 Guest are viewing this topic.

Esteban

  • Newbie
  • *
  • Posts: 6
    • View Profile
[SOLVED] Problem handling multiple key stroke
« on: May 13, 2024, 03:42:05 pm »
Hello everyone,
Its been a one year journey with SFML and everything has been great but i am facing a weird bug with inputs.
I am building a game so i have a HandleInput method in which i poll every event but when i am stroking 5 keys at the same time i get nothing (i tried to detect keyboard input using sf::Keyboard::isKeyPressed and by sf::Event.type and get the same bug)

Here is how i handle my event, this function is called by the main loop with 2 main functions, update and draw
sf::Event event;
while (data->window.pollEvent(event))
{
        if (sf::Event::Closed == event.type)
                data->window.close();
        else if (event.type == sf::Event::KeyPressed)
        {
                if (event.key.code == inputOfPlayers[0].moveUp)
                {
                        //do things
                }
                else if (event.key.code == inputOfPlayers[0].moveDown)
                {
                        //do other things
                }
        }
}

I am building a local multiplayer game so this error bother me a bit, i don't know how can i solve this ?
Thanks for reading this and lmk if i didnt make this clear enough
« Last Edit: May 13, 2024, 06:50:20 pm by Esteban »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Problem handling multiple key stroke
« Reply #1 on: May 13, 2024, 03:50:10 pm »
Your keyboard probably doesn't handle n-key rollover, but has a cheap implementation that will only allow 4-5 keys to be pressed in certain areas of your keyboard.
You're probably receiving some key pressed events, just not necessarily the up and down keys you've configured.

What keyboard do you have?

Try mapping the keys further apart, e.g. WASD for one player and arrow keys for the other.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Esteban

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Problem handling multiple key stroke
« Reply #2 on: May 13, 2024, 04:28:16 pm »
In my game the two players has keys  for deplacements and others for action, if one player hit two keys for shooting the other is stuck and cannot use any movement)
the key are the following  :

(thats a french keyboard tho) so instead of zqsd its wasd
The problem is that i want to put the game on an arcade born at my school and i cant decide which key is related to which joystick and neither for the button of each players.

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: Problem handling multiple key stroke
« Reply #3 on: May 13, 2024, 05:25:58 pm »
Yep, sounds like keyboard ghosting / ghost keys.
Microsoft have an webpage demo that can help find ghost keys (basically just highlights keys on screen as you press them): https://www.microsoft.com/applied-sciences/projects/anti-ghosting-demo

Most non gaming keyboards have ghosting, which keys ghost depends on the keyboard hardware.
Some keyboards will have a rating like 4-key rollever which means you are guaranteed to be allowed to press 4 different keys at once before ghosting happens. A keyboard with n-key rollover can handle any number of keys at once.

If we're talking about arcade machines based on a PC, it depends on how the controls are hooked up. One popular device (I've even got one here somewhere) is the I-PAC2. It's a $39 device that lets you wire up 2 arcade joysticks with 8 buttons each (and some extra like player 1 and 2) and emulate a keyboard. No coding needed. The good thing is it has no ghost keys.

One option if you are just testing is to plug in two keyboards at once. Definitely for Windows and maybe for Linux (not sure) keyboards are merged into one system device. So software like SFML sees one keyboard but really it gets the results of both merged together. That would still have ghosting on each keyboard, but not one ghosting the other.

So yeah, this is a keyboard hardware issue, not the fault of the OS, SFML or USB.
(Also very annoying. My sister worked out the keys on my old keyboard that she could press while we were playing Mortal Kombat 3 that would stop my jump and kick keys from registering)

Esteban

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Problem handling multiple key stroke
« Reply #4 on: May 13, 2024, 06:50:00 pm »
Thanks for your answer Kojack ! I looked up at this and this is my problem, i don't know how they set up the arcade so i will test on the battleground.
 Thanks for you fast and detail response.

 

anything