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

Author Topic: Problem with Window.EnableKeyRepeat and arrow keys  (Read 3532 times)

0 Members and 1 Guest are viewing this topic.

CodingMadeEasy

  • Newbie
  • *
  • Posts: 38
    • MSN Messenger - petetheheat_represent@msn.com
    • AOL Instant Messenger - 622+Amaretto+Ave
    • Yahoo Instant Messenger - petetheheat_baller@yahoo.com
    • View Profile
    • http://www.youtube.com/CodingMadeEasy
Problem with Window.EnableKeyRepeat and arrow keys
« on: September 22, 2012, 07:18:41 pm »
I have created an inputmanager class which handles all the input in my game. What I have done is set EnableKeyRepeat to false so that it helps for single key presses. Anyways it works fine until you try to check for input using the arrow keys.

Whenever I do input using the arrow keys it treats it as continuous input but when I use other key codes it treats it as a single key press :/. Here's my inputmanager code

InputManager.h

#ifndef INPUTMANAGER_H
#define INPUTMANAGER_H

#include<vector>
#include<SFML/Graphics.hpp>

class InputManager
{
    public:
        InputManager();
        ~InputManager();

        void Update(sf::Event event);

        bool KeyPressed(int key);
        bool KeyPressed(std::vector<int> keys);

        bool KeyReleased(int key);
        bool KeyReleased(std::vector<int> keys);

        bool KeyDown(sf::RenderWindow &Window, sf::Key::Code key);
        bool KeyDown(sf::RenderWindow &Window, std::vector<sf::Key::Code> keys);
    protected:
    private:
        sf::Event event;
};

#endif // INPUTMANAGER_H

 

InputManager.cpp

#include "InputManager.h"

InputManager::InputManager()
{
    //ctor
}

InputManager::~InputManager()
{
    //dtor
}

void InputManager::Update(sf::Event event)
{
    this->event = event;
}

bool InputManager::KeyPressed(int key)
{
    if(event.Key.Code == key && event.Type == sf::Event::KeyPressed)
        return true;
    return false;
}

bool InputManager::KeyPressed(std::vector<int> keys)
{
    for(int i = 0; i < keys.size(); i++)
    {
        if(KeyPressed(keys[i]))
            return true;
    }
    return false;
}

bool InputManager::KeyReleased(int key)
{
    if(event.Key.Code == key && event.Type == sf::Event::KeyReleased)
        return true;
    return false;
}

bool InputManager::KeyReleased(std::vector<int> keys)
{
    for(int i = 0; i < keys.size(); i++)
    {
        if(KeyReleased(keys[i]))
            return true;
    }
    return false;
}

bool InputManager::KeyDown(sf::RenderWindow &Window, sf::Key::Code key)
{
    if(Window.GetInput().IsKeyDown(key))
        return true;
    return false;
}

bool InputManager::KeyDown(sf::RenderWindow &Window, std::vector<sf::Key::Code> keys)
{
    for(int i = 0; i < keys.size(); i++)
    {
        if(Window.GetInput().IsKeyDown(keys[i]))
            return true;
    }
    return false;
}

 

Thanks in advance :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Problem with Window.EnableKeyRepeat and arrow keys
« Reply #1 on: September 22, 2012, 08:01:04 pm »
Can the error be reproduced with a simple code (a main with an event loop), without your classes on top of it?

What's your OS?
Laurent Gomila - SFML developer

CodingMadeEasy

  • Newbie
  • *
  • Posts: 38
    • MSN Messenger - petetheheat_represent@msn.com
    • AOL Instant Messenger - 622+Amaretto+Ave
    • Yahoo Instant Messenger - petetheheat_baller@yahoo.com
    • View Profile
    • http://www.youtube.com/CodingMadeEasy
Re: Problem with Window.EnableKeyRepeat and arrow keys
« Reply #2 on: September 22, 2012, 08:10:48 pm »
I will try that right now and I'm using Windows 7. Also if you must know. I'm using sfml 1.6

CodingMadeEasy

  • Newbie
  • *
  • Posts: 38
    • MSN Messenger - petetheheat_represent@msn.com
    • AOL Instant Messenger - 622+Amaretto+Ave
    • Yahoo Instant Messenger - petetheheat_baller@yahoo.com
    • View Profile
    • http://www.youtube.com/CodingMadeEasy
Re: Problem with Window.EnableKeyRepeat and arrow keys
« Reply #3 on: September 22, 2012, 08:18:10 pm »
Ok I figured out the problem but I don't know how to fix it. Ok well my input manager is not running in the event loop. When I run this code in the event loop it runs fine

if(event.Type == sf::Event::KeyPressed && event.Key.Code == sf::Key::Up)
    std::cout << "UP" << std::endl;
 

When I run it outside of the event loop that's when things go wrong but the weird thing is that if you change sf::Key::Up to sf::Key::W then it works fine even if it's outside the event loop : /

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Problem with Window.EnableKeyRepeat and arrow keys
« Reply #4 on: September 22, 2012, 08:40:01 pm »
Can you show how you use your input manager?
Laurent Gomila - SFML developer

CodingMadeEasy

  • Newbie
  • *
  • Posts: 38
    • MSN Messenger - petetheheat_represent@msn.com
    • AOL Instant Messenger - 622+Amaretto+Ave
    • Yahoo Instant Messenger - petetheheat_baller@yahoo.com
    • View Profile
    • http://www.youtube.com/CodingMadeEasy
Re: Problem with Window.EnableKeyRepeat and arrow keys
« Reply #5 on: September 25, 2012, 06:58:44 pm »
Sorry for the late reply but I have the code at home. But the inputmanager is essentially used like this

(dummycode.h)

#include "InputManager.h"

class Dummy
{
    public:
        void LoadContent();
        void UnloadContent();
        void Update(sf::RenderWindow &Window, sf::Event event);
        void Draw(sf::RenderWindow);
    private:
        InputManager input;
};
 

(dummycode.cpp)

#include "dummycode.h"

void DummyCode::LoadContent()
{
}

void DummyCode::UnloadContent()
{
}

void DummyCode::Update(sf::RenderWindow &Window,  sf::Event event)
{
    input.Update(event);
    if(input.KeyPressed(sf::Key::Up))
       // do something
}

void DummyCode::Draw(sf::RenderWindow &Window)
{
}
 


(main.cpp)

#include "dummycode.h"

int main()
{
    // all the initialize stuff

    DummyCode dummyCode;
   
    dummyCode.LoadContent();
   
    while(Window.IsOpened())
    {
        sf::Event event;
       while(Window.GetEvent(event))
       {
            // do the event loop actions
       }

        dummyCode.Update(Window, event);
        dummyCode.Draw(Window);

        Window.Display();
    }
}
 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Problem with Window.EnableKeyRepeat and arrow keys
« Reply #6 on: September 25, 2012, 07:05:14 pm »
You use the event outside the event loop, i.e. when its value is undefined. The event is valid only within the event loop, after GetEvent returns true.
Laurent Gomila - SFML developer

CodingMadeEasy

  • Newbie
  • *
  • Posts: 38
    • MSN Messenger - petetheheat_represent@msn.com
    • AOL Instant Messenger - 622+Amaretto+Ave
    • Yahoo Instant Messenger - petetheheat_baller@yahoo.com
    • View Profile
    • http://www.youtube.com/CodingMadeEasy
Re: Problem with Window.EnableKeyRepeat and arrow keys
« Reply #7 on: September 25, 2012, 07:20:23 pm »
Yea i understand that but why does it work for all keys except for the arrow keys when I use it outside the event loop?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Problem with Window.EnableKeyRepeat and arrow keys
« Reply #8 on: September 25, 2012, 09:04:10 pm »
The behaviour is undefined, so anything can happen and there's no point trying to understand what happens ;)
Laurent Gomila - SFML developer

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Problem with Window.EnableKeyRepeat and arrow keys
« Reply #9 on: September 25, 2012, 09:10:49 pm »
So if poll returns false the event struct has been altered and might be in undefined state?
Back to C++ gamedev with SFML in May 2023

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Problem with Window.EnableKeyRepeat and arrow keys
« Reply #10 on: September 25, 2012, 10:50:53 pm »
Quote
So if poll returns false the event struct has been altered and might be in undefined state?
Nop. If pollEvent returns false, the sf::Event instance is left untouched, which means that it's either uninitialized or filled with the previous event.
Laurent Gomila - SFML developer

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Problem with Window.EnableKeyRepeat and arrow keys
« Reply #11 on: September 25, 2012, 11:03:48 pm »
So using even outside event loop is/should be ok if we made sure it has been filled properly at least once before, right?
while(1)
{
sf::Event eve;
bool oka=false;
while(app.pollEvent(eve))
{
oka=true;
//whatever
}
if(oka)
{
//using event here
}
}
 
Back to C++ gamedev with SFML in May 2023

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Problem with Window.EnableKeyRepeat and arrow keys
« Reply #12 on: September 25, 2012, 11:23:51 pm »
Yes. But where do you go with that? The standard (and only) way to use SFML events is an event loop which looks like this:

sf::Event event;
while (window.pollEvent(event)) // GetEvent in SFML 1.6
{
    // process event
}
Laurent Gomila - SFML developer

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Problem with Window.EnableKeyRepeat and arrow keys
« Reply #13 on: September 25, 2012, 11:26:20 pm »
Erm..  :D
Only thing I can think of is displaying last caught event in some debugger interface without overhead of reupdating bilion times for each key, text and mouse stroke. ;D And(maybe) the ops problem, to protect against using uninitialized event struct. :P
Back to C++ gamedev with SFML in May 2023

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Problem with Window.EnableKeyRepeat and arrow keys
« Reply #14 on: September 25, 2012, 11:40:42 pm »
The ops problem is that he's processing the event outside the event loop. No need to go further and make this simple issue confusing for him ;)
Laurent Gomila - SFML developer