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

Author Topic: [SFML 1.6]Not Getting KeyPressed events?  (Read 3569 times)

0 Members and 1 Guest are viewing this topic.

XianForce

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • http://xianforce-dev.blogspot.com
[SFML 1.6]Not Getting KeyPressed events?
« on: April 19, 2011, 01:30:21 am »
I've been using SFML for about a month now (switched over from SDL, want to get some hardware acceleration before I jump into OpenGL), and I've been implementing a component entity system.

Well, I got everything set up for events, but I've noticed that I'm not receiving key press events. I get every other event, I even set up a quick switch statement to check every other event (well, outside of joystick events...), but the only one I don't get is key presses, any idea on why that is?

Here's the code that's relevant:

Code: [Select]

//Events are split between a current and global state like this:
while(Core::mp_Window->GetEvent(Event));
        {
            if(Event.Type == sf::Event::Closed ||
                Event.Type == sf::Event::Resized ||
                Event.Type == sf::Event::LostFocus ||
                Event.Type == sf::Event::GainedFocus)
            {
                if(mp_GlobalState)
                {
                    mp_GlobalState->OnEvent(Event);
                }
            }

            else
            {
                if(mp_CurrentState)
                {
                    mp_CurrentState->OnEvent(Event);
                }
            }
        }

//In a test event functoid...
bool TestFunc::operator()(sf::Event& event)
{
    switch(event.Type)
    {
        case sf::Event::KeyPressed:
            std::cout << "KeyPressed" << std::endl;
            break;
        case sf::Event::KeyReleased:
            std::cout << "KeyReleased" << std::endl;
            break;
        case sf::Event::MouseWheelMoved:
            std::cout << "MouseWheelMoved" << std::endl;
            break;
        case sf::Event::MouseButtonPressed:
            std::cout << "MouseButtonPressed" << std::endl;
            break;
        case sf::Event::MouseButtonReleased:
            std::cout << "MouseButtonReleased" << std::endl;
            break;
        case sf::Event::MouseMoved:
            std::cout << "MouseMoved" << std::endl;
            break;
        default:
            break;
    }


    return true;
}



All the events passed to the global state work correctly, and I've tested all the events, but the only one that I don't get a response from is KeyPressed...

EDIT: I'm using SFML 1.6, Code::Blocks (MinGW), on Windows

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
[SFML 1.6]Not Getting KeyPressed events?
« Reply #1 on: April 19, 2011, 07:37:48 am »
Have you tried the samples included in the SFML SDK? Pong and PostFx, for example, use the KeyPressed event.
Laurent Gomila - SFML developer

XianForce

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • http://xianforce-dev.blogspot.com
[SFML 1.6]Not Getting KeyPressed events?
« Reply #2 on: April 19, 2011, 08:49:41 am »
Quote from: "Laurent"
Have you tried the samples included in the SFML SDK? Pong and PostFx, for example, use the KeyPressed event.


Yeah, those do work, both as the included executables, and when I compiled them myself.

Also, that line that I write out to verify that an event occurred, is written a seemingly infinite number of times. The only other relevant code for that issue would be:

Code: [Select]

for(it = eventComponents.begin(); it != eventComponents.end(); ++it)
    {
        //check if data is allocated...
        if(it->second)
        {
            //Call the event callback for each event component... Looks a bit confusing because
            //the functoid has to be dereferenced to call the () operator.
            (*((it->second)->mp_EventCallback))(event);
        }
    }


It's from an object that manages all the event components; it iterates through each one when an event occurs, and the event is passed to it.

So the event occurs, and it's then passed to the current state, which passes it to this object, which then passes it to each component... But I only have one component in this example...

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
[SFML 1.6]Not Getting KeyPressed events?
« Reply #3 on: April 19, 2011, 09:16:26 am »
If it works in the SFML examples, then there's something wrong in your code. Try to reduce it to something minimal, the error should become obvious.
Laurent Gomila - SFML developer

XianForce

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • http://xianforce-dev.blogspot.com
[SFML 1.6]Not Getting KeyPressed events?
« Reply #4 on: April 20, 2011, 11:51:45 pm »
Quote from: "Laurent"
If it works in the SFML examples, then there's something wrong in your code. Try to reduce it to something minimal, the error should become obvious.


I've reduced it to as small as I really can, and I see nothing. I don't mean to be a bother though :o.

But, I have a simple event loop:

Code: [Select]

while(Core::mp_Window->GetEvent(Event));
{
       if(mp_GlobalState)
       {
           mp_GlobalState->OnEvent(Event);
       }
}


Then in the OnEvent method for global state I have:

Code: [Select]

if(event.Type == sf::Event::Closed)
    {
        mi_ReturnCode = 0;
    }

    else if((event.Type == sf::Event::KeyPressed) && (event.Key.Code == sf::Key::Escape))
    {
        mi_ReturnCode = 0;
    }


Setting the ReturnCode to anything other than 1 will cause the program to end, yet pressing Escape doesn't work in this. But when I change it to KeyReleased, it does work...

Events are literally not referenced anywhere else in code...

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
[SFML 1.6]Not Getting KeyPressed events?
« Reply #5 on: April 21, 2011, 08:06:53 am »
Quote
I've reduced it to as small as I really can

I don't think so ;)
A minimal example is something really small, like a main() with a window and an event loop. Don't try to keep all the features of your program, we don't care if it still works as before or not. Remove everything that is not relevant to the problem, don't try to keep your classes, just take the code out of them and paste it in a big main.
If you do this, you will end up with a code that works, and at this moment you'll find the guilty code that was breaking it.
Laurent Gomila - SFML developer

XianForce

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • http://xianforce-dev.blogspot.com
[SFML 1.6]Not Getting KeyPressed events?
« Reply #6 on: November 20, 2011, 02:30:40 am »
So, I usually wouldn't resurrect a topic this old, but my problem is still the same, but I have more information :).

So the problem seems to be when I pass an sf::Event reference to a function. When I take care of the event locally, everything is fine, but once I pass that reference to another function, things get a little weird.

I do not get key press events for any letters, numbers, symbols, spaces/carriage returns, or the escape key. Every other key (F1-F12, Ctrl, Alt, Delete, Arrow Keys, etc.) still work with key press, though.

Before you tell me to make a stripped down example, I did and here it is :) :

Code: [Select]


#include <SFML/Window.hpp>
#include <iostream>

bool HandleEvents(sf::Event& event)
{
    if(event.Type == sf::Event::Closed)
    {
        return false;
    }

    if(event.Type == sf::Event::KeyPressed)
    {
        return false;
    }

    return true;
}

int main()
{
    sf::Window* window = new sf::Window(sf::VideoMode(600, 800, 32), "Test");
    sf::Event event;
    bool running = true;

    while(running)
    {
        while(window->GetEvent(event))
        {
            running = HandleEvents(event);
        }
    }

    return 0;
}


Same problem persists between the two. I didn't see any issue with KeyRelease, only KeyPress... So, help please :D?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
[SFML 1.6]Not Getting KeyPressed events?
« Reply #7 on: November 25, 2011, 10:38:53 pm »
Quote
I do not get key press events

If you deduce it from the code that you posted, then I can tell you that the problem is in your code's logic, not in SFML.

To strictly test something (like key events) don't try to do something complicated with a logic behind, just use your debugger or std::cout.

Code: [Select]
if (event.Type == sf::Event::KeyPressed)
{
    std::cout << "it works" << std::endl;
}


As an exercise, I let you find out why your code gives your the impression that key events are not received ;)
Laurent Gomila - SFML developer

 

anything