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

Author Topic: Problem with Event Keyboard Input  (Read 9364 times)

0 Members and 2 Guests are viewing this topic.

Chyro

  • Newbie
  • *
  • Posts: 4
    • View Profile
Problem with Event Keyboard Input
« on: November 08, 2018, 12:06:36 pm »
Hey there.
I'm currently working on a little game project to gather more experience with C++ and SFML.

Now I came across a kinda odd problem while making a visual keyboard for entering a character name.
If I'm using sf::event to check for keypresses ->

(click to show/hide)

It works fine for the arrow keys, to move around the marker to select whatever key on the visual keyboard you want to use. But just below that, when I used the same thing to check if the 'Enter' key got pressed, it didn't work.

(click to show/hide)

After much trying around and looking for solutions, I noticed that for some reason, it only recognizes that 'Enter' key input when I'm holding alt while pressing Enter. Further trying out showed that this applies to most other keys, it only recognizes letter presses (like "N" or "L") or Spacebar if I'm holding alt at the same time. Yet the arrow keys work fine without holding Alt, with the exact same code.

Also, my current now working solution to it - simply using "sf::Keyboard::isKeyPressed(sf::Keyboard::Enter)", does work without the odd issue of having to hold 'Alt'.

So.. I do have a solution to my problem, but I still want to understand why it is causing that weird behavior when using event to check for the key inputs. While trying to figure it out, at one point I used the following code to check what the pressed key is recognized as by outputting it to the console:

(click to show/hide)

Oddly, it output the press of 'Enter' as '13'. Pressing alt + 'Enter' returned '58' which should be the correct value for the key.

Does anyone have an idea what could be causing this?
I figured I'll only put the relevant code here, but if you need I can post all of it.

Though I think I pretty much narrowed the issue down to that one question -
What could cause sf::event require 'alt' being held to recognize 'Enter' correctly, while arrow keys are recognized normally and sf::keyboard also recognizes 'Enter' normally?

(and no, there's nothing in my code doing anything with the 'alt' key or checking for it)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Problem with Event Keyboard Input
« Reply #1 on: November 08, 2018, 12:11:06 pm »
What's your full code?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Problem with Event Keyboard Input
« Reply #2 on: November 08, 2018, 12:11:14 pm »
Quote
I figured I'll only put the relevant code here, but if you need I can post all of it.
A complete and minimal code that reproduces the problem would be much better :)
Laurent Gomila - SFML developer

Chyro

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Problem with Event Keyboard Input
« Reply #3 on: November 08, 2018, 01:14:32 pm »
Uh.. okay, full code.
Here we go.
The project is using a state machine, so I guess I'll put in the main code and the code of the state where the issue happens. Probably more than is really needed, but its in convenient spoiler tags so you can just look at the stuff that may be relevant.

.. side note, I'm still quite new to programming (like 2 months or so) so please forgive if there's things in it that are inefficient or should be done in a simpler fashion. Don't mind if anything gets pointed out, as its always good to learn how to do things better. .. I'm just happy when it works the way I intend it to ^^

(click to show/hide)
(click to show/hide)
(click to show/hide)
(click to show/hide)
(click to show/hide)
(click to show/hide)


Oh yeah, looking at it again reminded me of something else.
While checking the keypressed with event doesn't recognize the normal 'Enter' as 'Enter' (enum index 58 of sf::keyboard), the keyreleased does correctly recognize it.
So it's exclusively keypressed that (for some reason) needs alt to be held for 'Enter' to be recognized.
« Last Edit: November 08, 2018, 01:34:11 pm by Chyro »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Problem with Event Keyboard Input
« Reply #4 on: November 08, 2018, 01:19:00 pm »
As suspected, you're checking the event variable outside of the event loop. You need to poll all the events and check the event variable inside the event loop, otherwise you only get stale or even invalid data.

See the events tutorial for the correct approach. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Chyro

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Problem with Event Keyboard Input
« Reply #5 on: November 08, 2018, 01:27:51 pm »
Correct me if I'm wrong, but I think it is inside the event loop?
If you check main.cpp, it's actually that loop that is running everything. On the line
  'currentState->handle_events(window, event);' (marked it with red in the spoiler above)
Is where the code with the issue is being called, with currentState being 'Naming', and that's inside the event loop. I'm passing the sf::event into the function so that it can be used and affected by the different game states while still being the same (one and only) event created in the main loop.

I mean.. if it got stale and invalid data, then the arrow-keys also wouldn't be recognized, no? But they are fully and correctly recognized, with the same code. And it does recognize other keys like Enter, Space, and letters - but only if alt is held while pressing them. Also, in another state (the title menu) that also gets the 'event' passed to it and uses it for checking key presses, the arrow keys as well as Enter + Space inputs get correctly recognized. If I used it wrongly, would stale/invalid data really cause this many keys to be recognized correctly? (not trying to pointlessly argue, but trying to understand why it works fine for the arrow keys and in another state, but not for other keys)

.. I did read the tutorial page about Events multiple times, and I admit I still don't 100% understand it. Am generally learning and trying to understand things through appying them, by trial and error and logic. I do read the theory, but often struggle to understand it until I try out the examples in different scenarios, see how it works, when it doesnt, and then figure out why it doesn't.

(as a side note, normally the program runs through the states starting with INTRO -> INTRO2 -> TITLE -> NAMING, but for testing / debugging I made it start with NAMING. Each of the other states is checking the event from main.cpp for a pressed Enter to progress (title menu also using arrows to navigate), and in each of them it works fine)
« Last Edit: November 08, 2018, 01:51:53 pm by Chyro »

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Problem with Event Keyboard Input
« Reply #6 on: November 08, 2018, 04:14:15 pm »
eXpl0it3r was referring to this loop

sf::Event event;
while (window.pollEvent(event))
{
   if (event.type == sf::Event::Closed)
   {
      window.close();
   }
}
 

You should be handling all of your events in here, not just sf::Event::Closed. Think about what would happen if multiple events came in at the same time (for example, keyboard press and mouse move). You will be looping through them and throwing all but the last one away. Only the last event will make it beyond this code.

Also think about what would happen if you didn't have any events come in since the last frame. event will be left uninitialized, but you are passing it along to your states anyway with whatever random data it contains.

I haven't gone through all of your code to see if this is directly causing your problems or not, but it would be worth fixing this.

---

Also
std::cout << "Pressed button: " << event.key.code << "\n";
 
Why not put this directly in the above event loop to see if you're ever receiving the Enter key event (don't forget to check type==sf::Event::KeyPressed first).

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Problem with Event Keyboard Input
« Reply #7 on: November 08, 2018, 06:40:32 pm »
Basically what Arcade said.

Also a general rule of thumb, just because something "works" doesn't mean it's correct, especially when other things are not working. ;)

while(window.isOpen())
{
    // ...
}
That's your main/game loop. As long as the window is open do X.

while(window.pollEvent(event)
{
    // ...
}
That's your event loop. As long as there are new events, write them into the event variable and do X.
Only within that loop, should you process events.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Chyro

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Problem with Event Keyboard Input
« Reply #8 on: November 08, 2018, 09:38:44 pm »
Alright, thanks for the advice.
After a second look I realized that it was in the 'window open' loop, but not inside the poll.event loop.
So I moved the event handler function into the event loop (also the global check for 'Escape' to close the window or go back to title menu, depending on state). And now it works fine.

.. at first I just moved all the main functions into the loop, but realized that makes everything pause as long as there's no event (was kinda funny, the animations and progression always paused unless I kept moving the mouse) but then moved the parts not relevant to events back out ^^

Now everything works as intended, thanks!