3
\$\begingroup\$

What happens is I press right for a fraction of a second then release and my character moves to the right, as it is supposed to. I then press right again but this time for 5 seconds. My character moves immediately to the right and keeps moving with no pause between the first movement and the rest, as it is supposed to. But after 5 seconds of pressing right I then release the key and press left, but my character still keeps moving to the right for another 6 seconds. This effect is worse the longer I press the key. Why is that? I checked the boolean variables I am using to track the pressed and not pressed status of my keys and it seems like the status doesn't change until after the aforementioned 6 seconds during which the character keeps walking right although I am pressing left. However, key presses are still being detected quickly and on time in each iteration of the main game loop.

Based on my checks, key presses are still being checked quickly at each iteration of the main loop, and so is the key down/pressed status of keyboard keys. However, the key up/released status of keys is only checked until after the 6 seconds mentioned. Further checks showed that SDL2 poll event still reports a keydown status even though I am not pressing any keys and only reports a keyup after the 6 seconds. Is it windows 10 interfering with my program? It's like there's another program that keeps pressing right (same issue with other keys) eventhough I am no longer physically pressing that key.

I would appreciate your help and advice. Thank you.

I am programming in Free Pascal using SDL2. Below is a sample of my keyboard and other inputs procedure:

if sdl_pollevent(sdlevent)=1 then
begin
    { writeln(sdlevent^.type_,' ',sdl_keydown,' ',sdl_keyup); }
    if sdlevent^.type_=sdl_keydown then
    begin
        joystickactive:=false;
        { writeln(sdlevent^.key.keysym.sym); }
        { writeln(sdl_getkeyname(sdlevent^.key.keysym.sym)); }
        enterkeyintf:=false;
        if enterkeyintf=true then
        begin
            assign(t[1],'tf\tf0.tf');
            append(t[1]);
            settextbuf(t[1],bufin);
            writeln(t[1],sdlevent^.key.keysym.sym);
            writeln(t[1],sdl_getkeyname(sdlevent^.key.keysym.sym));
            close(t[1]);
        end;
        o[1]:=0;
        repeat
            o[1]:=o[1]+1;
            if sdlevent^.key.keysym.sym=keycode[o[1]] then keybool[o[1]]:=true;
        until o[1]=totalkeys;
        o[1]:=0;
        repeat
            o[1]:=o[1]+1;
            if sdlevent^.key.keysym.sym=alphanumm[o[1]] then alphanumbool[o[1]]:=true;
        until o[1]=totalalphanum;
    end;
    if sdlevent^.type_=sdl_keyup then
    begin
        joystickactive:=false;
        o[1]:=0;
        repeat
            o[1]:=o[1]+1;
            if sdlevent^.key.keysym.sym=keycode[o[1]] then keybool[o[1]]:=false;
        until o[1]=totalkeys;
        o[1]:=0;
        repeat
            o[1]:=o[1]+1;
            if sdlevent^.key.keysym.sym=alphanumm[o[1]] then alphanumbool[o[1]]:=false;
        until o[1]=totalalphanum;
    end;
end;
\$\endgroup\$
12
  • \$\begingroup\$ I'm not sure if this is how I should my code on this forum. If it goes against the rules but here it is... This website won't let me press enter in this comment and won't let me delete it. \$\endgroup\$
    – Seb0029
    Dec 5, 2017 at 0:23
  • 1
    \$\begingroup\$ If you paste your code in the question, highlight and select the formatting option for code sample {} (next to the camera). \$\endgroup\$
    – Stephan
    Dec 5, 2017 at 0:29
  • \$\begingroup\$ Sorry I can't find the button to edit my question nor the button to reply to your comment. The website doesn't show any of these buttons. Hence me adding this new comment. It's a bit confusing. Ok I found how to edit. It's written in tiny greyed out letters under my question, lol. Hard to spot or pay attention to. \$\endgroup\$
    – Seb0029
    Dec 5, 2017 at 0:35
  • \$\begingroup\$ So I did a few more checks. I checked the sequence of sdl event types when the keys are being pressed and released and sdl reports a type 768 for sdl_keydown (key pressed) and 769 for sdl_keyup (key released). However there is a type 771 that is detected by sdl after I press a key and keep pressing. What is sdl event type 771? This keeps being reported by sdl event type and is what I think is causing the character to keep walking right for another 6 seconds eventhough I am no longer pressing any key. \$\endgroup\$
    – Seb0029
    Dec 5, 2017 at 0:47
  • 1
    \$\begingroup\$ Copy your answer out of the question, and post it as an answer. You're allowed to answer your own questions. Glad you figured it out! \$\endgroup\$
    – Stephan
    Dec 5, 2017 at 1:19

1 Answer 1

3
\$\begingroup\$

I think what was happening is that SDL2 would pollevent once and not again if the key being pressed is still the same. If it's the same it would default to event type 771 (I couldn't find on the internet what that corresponds to). So I added the code to make the program run sdl_pollevent once more if it gets the code 771. Solved the problem:

if sdl_pollevent(sdlevent)=1 then
begin
    if sdlevent^.type_=771 then sdl_pollevent(sdlevent);
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Don't forget to accept your answer as solved to close the question. \$\endgroup\$
    – Stephan
    Dec 5, 2017 at 1:27
  • \$\begingroup\$ Thanks. I get a message saying I can accept it in two days. \$\endgroup\$
    – Seb0029
    Dec 5, 2017 at 3:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .