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

Author Topic: KeyPressed Event Not Firing When Expected (SFML.NET 2.2)  (Read 6752 times)

0 Members and 1 Guest are viewing this topic.

Kimimaru

  • Newbie
  • *
  • Posts: 4
    • View Profile
KeyPressed Event Not Firing When Expected (SFML.NET 2.2)
« on: November 29, 2015, 11:49:52 pm »
Hey everyone, I'm playing around with SFML in C# and am encountering a strange problem with the KeyPressed event where it fires once, then doesn't fire while I'm holding down the key until later.

When the KeyPressed event is fired, I set the value of the index of the corresponding key in an array to true and set the value of the previous pressed value to that value before (Ex. prevPressed[index] = pressed[index]; pressed[index] = true).

Here's some sample code (I have confirmed that I subscribed to the event only once in code not shown):

while (GameWindow.IsOpen)
{
    //Process events
    GameWindow.DispatchEvents();

    //Update game logic here
    gameCore.Update();
 

In gameCore.Update() all I have is this:

if (Input.PressedKey(Keyboard.Key.A))
    Debug.Log("Pressed A!");
 

All the Input.PressedKey() method does is check if the previous state of the key wasn't pressed and the current state of the key is pressed, effectively checking if it was JUST pressed on that frame so we don't handle held input when not necessary. The first time I press the key, it acts like normal, but when I hold it I see a bunch of the "Pressed A!" logs and don't see the KeyPressed event getting fired again, halting the "Pressed A!" logs, until roughly 20 frames later. My framerate is capped at 60, but this occurs even if I don't set the limit. The KeyReleased event works as normal.

Any ideas? I have a temporary workaround that I'd rather not keep where I loop through the entire array (101 elements) each frame and set prevPressed[index] to pressed[index] after gameCore.Update().
« Last Edit: November 29, 2015, 11:54:07 pm by Kimimaru »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: KeyPressed Event Not Firing When Expected (SFML.NET 2.2)
« Reply #1 on: November 30, 2015, 02:03:26 am »
Disable the key repeat.
GameWindow.SetKeyRepeatEnabled(false);
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Kimimaru

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: KeyPressed Event Not Firing When Expected (SFML.NET 2.2)
« Reply #2 on: November 30, 2015, 02:27:15 am »
Thanks for the reply! Unfortunately the problem isn't that the key is repeating, but rather that the KeyPressedEvent isn't occurring when I expect it to. I should be seeing the "Pressed A!" log only once, after which the event should fire again and set prevPressed[index] to true (since pressed[index] is true); the next time gameCore.Update() is called, I shouldn't be seeing the "Pressed A!" log. However, I am indeed seeing it multiple times while holding down the key, and the KeyPressedEvent starts firing again after roughly 20 frames, instead of 1 frame, for some odd reason.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: KeyPressed Event Not Firing When Expected (SFML.NET 2.2)
« Reply #3 on: November 30, 2015, 05:34:21 pm »
the problem isn't that the key is repeating
I am indeed seeing it multiple times while holding down the key, and the KeyPressedEvent starts firing again after roughly 20 frames
The problem you describe is the key pressed event repeating, which happens when key repeat is enabled.
Getting repeated events should be expected.

Do you still get this effect even after following zsbzsb's advice?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Kimimaru

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: KeyPressed Event Not Firing When Expected (SFML.NET 2.2)
« Reply #4 on: November 30, 2015, 06:59:14 pm »
The effect doesn't persist after calling SetKeyRepeatEnabled(false).

I'm sorry for not being clear, I will try to describe my problem more concisely:

When I hold down the 'A' key, I want the KeyPressedEvent to fire repeatedly. After pressing the 'A' key initially, everything works well. When I hold the 'A' key for subsequent frames, the event doesn't repeatedly fire until around 20 frames later. This is the main issue. If it fired after the first frame of pressing the key while I'm holding it down, that would be the behavior I'd expect, but it just isn't working that way.

Again thanks for the replies! Hopefully we can sort out this issue soon.

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: KeyPressed Event Not Firing When Expected (SFML.NET 2.2)
« Reply #5 on: November 30, 2015, 08:11:25 pm »
That delay is expected if you have SetKeyRepeatEnabled set to true. Try holding down a key in other programs, such as in your text editor. You will see the same delay. One character gets printed and then a small delay before others get printed. I believe this delay is determined by your operating system, not SFML.

See the SFML events tutorial for more information on this
http://www.sfml-dev.org/tutorials/2.3/window-events.php
« Last Edit: November 30, 2015, 08:13:13 pm by Arcade »

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: KeyPressed Event Not Firing When Expected (SFML.NET 2.2)
« Reply #6 on: November 30, 2015, 08:33:52 pm »
It sounds to me like what you want is not events, but real time state checking, which in normal SFML is sf::Keyboard::isKeyPressed(), I'm not sure how/if the dotnet binding is different.

Kimimaru

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: KeyPressed Event Not Firing When Expected (SFML.NET 2.2)
« Reply #7 on: December 01, 2015, 03:01:02 am »
That delay is expected if you have SetKeyRepeatEnabled set to true. Try holding down a key in other programs, such as in your text editor. You will see the same delay. One character gets printed and then a small delay before others get printed. I believe this delay is determined by your operating system, not SFML.

See the SFML events tutorial for more information on this
http://www.sfml-dev.org/tutorials/2.3/window-events.php

That must be it then; thanks! I ended up finding a better workaround in place that's more efficient than what I came up with before, so I should be fine for keyboard input now. Thanks to everyone for all the help!

 

anything