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

Author Topic: Getting key pressed events to work  (Read 2168 times)

0 Members and 1 Guest are viewing this topic.

Lowest0ne

  • Newbie
  • *
  • Posts: 11
    • View Profile
Getting key pressed events to work
« on: July 16, 2012, 04:54:18 pm »
Hi all,

I'm teaching myself OpenGL, using SFML for a context.  I've finally gotten to the point where I want to be able to move the camera.  So you know, press 'w' and the camera's "move foward" becomes true.

I'm having a problem getting the input to actually trigger something though.  My guess is there is something simple I'm just missing, perhaps some one can help.
// My main function is where the message loop is:
sf::Event pEvent;
bool open = (app.isOpen()? true : false);
while (open)
{              
        while (app.pollEvent(pEvent))
        {
                open = prgEvent(pEvent);                               
        }
        display();
        app.display();
}
app.close();
delete model;
delete terra;
delete cam;
return 0;

// prgEvent(pEvent) takes the event and splits it into types,
// returning false if program should end
bool prgEvent(sf::Event& pEvent)
{
        //  A false return signals the end of the program
        switch (pEvent.type)
        {
        case sf::Event::KeyPressed: return prgKeyPressed(pEvent.key.code);
        case sf::Event::Closed: return false;  // Note that this works
        }
        return true;
}

//prgKeyPressed() makes a switch on the actual key

#define print1(x) std::cout << x << '\n'
bool prgKeyPressed(sf::Keyboard::Key& kpCode)
{
        // A false return signals the end of the program
        switch (kpCode)
        {

        case sf::Keyboard::Escape: print1(kpCode); return false;
        }
        return true;
}
 
@Exploiter: Like those tags? :)

So, in the case of sf::Keyboard::Escape I print kpCode (which is 36), and then return false, right?

I get 36 printing on console, but the program doesn't quit.  Like I said, I must be missing something here.

Thanks
« Last Edit: July 16, 2012, 04:59:37 pm by Lowest0ne »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Getting key pressed events to work
« Reply #1 on: July 16, 2012, 05:10:23 pm »
Do you mean the code=cpp tags? Very much! ;D

I'm not quite sure what's the source of your problem is, but case keywords should always come with a break keyword. Otherwise it can happen that the next case statement would also get executed. Altough you return immediatly after a case statement, so I'm not completly sure what's the problem. Try to fix that, maybe it will also fix your problem.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Lowest0ne

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Getting key pressed events to work
« Reply #2 on: July 16, 2012, 06:08:06 pm »
Putting breaks after the returns doesn't do anything.

Edit:  Okay, so in the main() message loop:
        while (app.pollEvent(pEvent))
        {
                open = prgEvent(pEvent);
                print1(open);
        }
 

I am getting a zero printed when I hit escape.  ?? case sf::Event::Closed: return false; quits my program ?


Edit2:  However this:
std::ofstream out("test.txt");
while (open)
{              
        while (app.pollEvent(pEvent))
        {
                open = prgEvent(pEvent);
        }
        out << open << std::endl;
        display();
        app.display();
}
out.close();

Prints a whole bunch of 1s to test.txt with only a zero from when I acitvate sf::Event::Closed:

What the heck is going on?  it's like I lost scope.
« Last Edit: July 16, 2012, 06:19:33 pm by Lowest0ne »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Getting key pressed events to work
« Reply #3 on: July 16, 2012, 06:59:04 pm »
Unfortunatly I can't anything at the moment, thus I can only more or less guess.
Can you try to put the decleration of the event variable inside your while(open) loop? I'm not quite sure what SFML does if there's no even (i.e. if it will reset the event). Then again it only proceeds if it returns true...

Can you past the minimal, current and fully working code?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Lowest0ne

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Getting key pressed events to work
« Reply #4 on: July 16, 2012, 07:30:40 pm »
Strange how copy pasting code to make it clear for someone else finds the bug itself.
while (app.pollEvent(pEvent))
I sent more than one event when I hit Escape.  The first being the KeyPress of escape, the second I don't know (27), and then the KeyRelease of escape.  Of course, the key press set open to false, but the others set to true!

while (app.pollEvent(pEvent) && open)

As a side, I noticed:
while (app.pollEvent(pEvent))
{
        open = prgEvent(pEvent);
        print1(pEvent.key.code); // This prints the X location of the mouse on the window
}

Also, is there a link to how I can figure out what that mysterious 27 was?
« Last Edit: July 16, 2012, 07:42:19 pm by Lowest0ne »