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

Author Topic: Strange behavior with isKeyPressed() function  (Read 1866 times)

0 Members and 1 Guest are viewing this topic.

underww

  • Newbie
  • *
  • Posts: 34
    • View Profile
Strange behavior with isKeyPressed() function
« on: July 11, 2015, 09:21:08 am »
Hi,

I'm using isKeyPressed function for smooth movement.
it works correctly when I'm using up/down/left/right with shift.

But when I use numpad, the problem comes up.
First, if I press shift and then numpad, it doesn't catch shift.
and when I press numpad first and then shift (while pressing numpad),
isKeyPressed(numpad) returns always true after I released the key. (not always but very often)

also in this situation, even if I turn the program off and on, the isKeyPressed function still returns true.
Here's a small example of the code. (I'm using windows 8.1 and vs2013)


        while (window.isOpen())
        {
                bool shift = sf::Keyboard::isKeyPressed(sf::Keyboard::LShift) || sf::Keyboard::isKeyPressed(sf::Keyboard::RShift);

                bool up = sf::Keyboard::isKeyPressed(sf::Keyboard::Up) || sf::Keyboard::isKeyPressed(sf::Keyboard::Numpad8);
                bool down = sf::Keyboard::isKeyPressed(sf::Keyboard::Down) || sf::Keyboard::isKeyPressed(sf::Keyboard::Numpad2);
                bool left = sf::Keyboard::isKeyPressed(sf::Keyboard::Left) || sf::Keyboard::isKeyPressed(sf::Keyboard::Numpad4);
                bool right = sf::Keyboard::isKeyPressed(sf::Keyboard::Right) || sf::Keyboard::isKeyPressed(sf::Keyboard::Numpad6);

                std::string string = "";

                if (shift)
                        string += "shift ";
                if (up)
                        string += "up";
                if (down)
                        string += "down";
                if (left)
                        string += "left";
                if (right)
                        string += "right";

                text.setString(string);

                window.clear();
                window.draw(text);
                window.display();
        }
 

I always very appreciate to SFML team, Thanks :)

Edit: fixed typos
« Last Edit: July 11, 2015, 09:26:48 am by underww »

kitteh-warrior

  • Guest
Re: Strange behavior with isKeyPressed() function
« Reply #1 on: July 11, 2015, 03:14:22 pm »
Holding shift and pressing a number pad key doesn't act as the number key for it. It uses its secondary function:

7 -> Home
8 -> Up
9 -> Pageup
etc., so they wouldn't be registered as a normal 8-key, 4-key, 6-key, or 2-key. (at least on Windows)

Under testing your code, it appears that if number lock is off, the keys work as expected. If number lock is on, pressing shift then pressing the number pad key: only the number pad key is detected. If the number pad key is pressed then the shift key, it works as expected.

This seems more like a keyboard limitation. Have you tried other programs that does this kind of key binding on your system?

underww

  • Newbie
  • *
  • Posts: 34
    • View Profile
Re: Strange behavior with isKeyPressed() function
« Reply #2 on: July 11, 2015, 05:01:30 pm »
I'm sure it's not the problem of keyboard limitation.
and I tested it on my laptop (windows 10) and the result was the same.

Holding numpad + shift (with numlock) for a while often make the program holds that numpad key forever.

kitteh-warrior

  • Guest
Re: Strange behavior with isKeyPressed() function
« Reply #3 on: July 11, 2015, 07:58:46 pm »
I'm sure it's not the problem of keyboard limitation.
and I tested it on my laptop (windows 10) and the result was the same.

Are you sure that the keyboard chipset isn't very similar between these two laptops?

Quote from: underww
Holding numpad + shift (with numlock) for a while often make the program holds that numpad key forever.

I didn't do that for me. (aside from letting go in a non unencapsulating way)
Using this code (because you didn't include all of the code required):
(click to show/hide)
And the console window shows to correct information when I pressed the number pad key then the shift key.

Although, retesting it, if I:
Press the number pad key first, then shift = works as expected
Press the shift key first, then number pad = doesn't work as expected
Press the number pad key first, then shift, then release the number pad key (while still holding down the shift key) = the number pad key appears to be continued to be held down, even after letting go of it.

Again:
Quote from: kitteh-warrior
Have you tried other programs that does this kind of key binding on your system?

Also, this may be related (because shift keys are involved, although it is using events and here it is using real time, though it still could be related). The differences between when number lock is on/off makes me question this, though.

 

anything