### Hold key problem

Hello guys! I have a question for you and I'm sorry if I'm wasting your time.
I made a game in C++(I'm in high school so I don't know a lot of things).
My problem is that when I try to move my character in right it works if I press the key one time , but if I hold it , the character have a little bit of delay.
How can I remove that delay?
 ``1234567891011`` ``````if(kbhit()){ a=getch(); if((a=='A' && joc[hM-1][lM]!='*' && joc[hM-1][lM]!='#') || (a=='a' && joc[hM-1][lM]!='*' && joc[hM-1][lM]!='#')){ joc[hM][lM]=' '; hM--; joc[hM][lM]=minge;} else if((a=='D' && joc[hM+1][lM]!='*' && joc[hM+1][lM]!='#') || (a=='d' && joc[hM+1][lM]!='*' && joc[hM+1][lM]!='#')){ joc[hM][lM]=' '; hM++; joc[hM][lM]=minge;}``````
A simple solution is to set a bool to true when the key is pressed, and to set it back to false once the key is released.
In a seperate function/place you check this bool every frame, and if it is true, you apply whatever you want to happen when the key is pressed.

For example:

 ``123456789101112131415161718192021222324252627282930`` ``````bool movingLeft = false; bool movingRight = false; void keyPressed(char key) { if(key == 'A' || key == 'a') movingLeft = true; if(key == 'D' || key == 'd') movingRight = true; } void keyReleased(char key) { if(key == 'A' || key == 'a') movingLeft = false; if(key == 'D' || key == 'd') movingRight = false; } void myFunc() { if(movingLeft) { // Do stuff } if(movingRight) { // Do other stuff } }``````

You could add in some `else`s if you like/need them, of course...
Hmm.... Actually in that delay the program thinks that I released the key
Last edited on
Ah... May I ask how a key press is detected in your program?
ahm
while(1){
if(kbhit()){
a=getch();
if(a == 'A' || a == 'a')
/* do something * /
if(a=='B' || a=='b')
/* do other stuff */

}
(kbhit means if a key is pressed)
Right. kbhit() detects key presses. I thought for a moment you'd written kbhit() yourself. But it's in conio.h of course.
The problem is the repeat delay in the OS itself. And it doesn't look like conio.h has a function to tell you a key has been released. (key release is not the same as the key not being pressed)

You could try using a library that lets you deal with key events. Or directly use the OS functions for this.
=(
@creative3000

try using this..
 ``1234567891011121314151617181920212223`` ``````do { a=getch(); switch(tolower(a)) { case 'a': if( joc[hM-1][lM]!='*' && joc[hM-1][lM]!='#') { joc[hM][lM]=' '; hM--; joc[hM][lM]=minge; break; } case 'd': if(joc[hM+1][lM]!='*' && joc[hM+1][lM]!='#') { joc[hM][lM]=' '; hM++; joc[hM][lM]=minge; break; } } }while(/*whatever the end result should be*/);``````

or
 ``12345678910111213141516171819202122232425`` ``````do { a=getch(); switch(a) { case 'A': case 'a': if( joc[hM-1][lM]!='*' && joc[hM-1][lM]!='#') { joc[hM][lM]=' '; hM--; joc[hM][lM]=minge; break; } case 'D': case 'd': if(joc[hM+1][lM]!='*' && joc[hM+1][lM]!='#') { joc[hM][lM]=' '; hM++; joc[hM][lM]=minge; break; } } }while(/*whatever the end result should be*/);``````

in place of
 ``123456789101112`` ``````if(kbhit()){ a=getch(); if((a=='A' && joc[hM-1][lM]!='*' && joc[hM-1][lM]!='#') || (a=='a' && joc[hM-1][lM]!='*' && joc[hM-1][lM]!='#')){ joc[hM][lM]=' '; hM--; joc[hM][lM]=minge;} else if((a=='D' && joc[hM+1][lM]!='*' && joc[hM+1][lM]!='#') || (a=='d' && joc[hM+1][lM]!='*' && joc[hM+1][lM]!='#')){ joc[hM][lM]=' '; hM++; joc[hM][lM]=minge;} ``````

It didn't work =(
Problem solved =D!

if(GetKeyState(VK_A)<0 && joc[hM-1][lM]!='*' && joc[hM-1][lM]!='#' ){
Control=0;
if(hM!=1){
joc[hM][lM]=' ';
hM--;
joc[hM][lM]=minge;}}
else
if(GetKeyState(VK_D)<0 && joc[hM+1][lM]!='*' && joc[hM+1][lM]!='#'){
Control=0;
if(hM!=15){
joc[hM][lM]=' ';
hM++;
joc[hM][lM]=minge;}}
Topic archived. No new replies allowed.