# Why do I have delays when pressing a keyboard key continuously to move the character?

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 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;

• 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. – Seb0029 Dec 5 '17 at 0:23
• If you paste your code in the question, highlight and select the formatting option for code sample {} (next to the camera). – Stephan Dec 5 '17 at 0:29
• 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. – Seb0029 Dec 5 '17 at 0:35
• 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. – Seb0029 Dec 5 '17 at 0:47
if sdl_pollevent(sdlevent)=1 then