Page 1 of 1
Getch() Function
#1
Getch() Function
Posted 14 January 2005 - 03:56 PM
I have written a simple game with a ball which bounces off the margins of the screen and at the bottom of the screen is my pad which is controlled by 'a' and 'd' buttons on my keyboard.:(simplified)
int c = 1;
while(1)
{
if(kbhit())
{
c = getch();
if(c == 27) exit(1);
if(c == 'a' || c == 'A') movepadleft();
if(c == 'd' || c == 'D') movepadright();
}
movetheball();
}
The thing that i don't like is: when i press a or d the pad moves for a short period of time, then stops and then moves constantly as long as i keep the button pressed.(it is the same thing just if i open notepad and press a letter: the letter i press apears, then there is a pause and then it prints that letter al long as i keep the button pressed).
This function(getch()) is not suitable for this problem? What alternatives i have to solve the problem?
int c = 1;
while(1)
{
if(kbhit())
{
c = getch();
if(c == 27) exit(1);
if(c == 'a' || c == 'A') movepadleft();
if(c == 'd' || c == 'D') movepadright();
}
movetheball();
}
The thing that i don't like is: when i press a or d the pad moves for a short period of time, then stops and then moves constantly as long as i keep the button pressed.(it is the same thing just if i open notepad and press a letter: the letter i press apears, then there is a pause and then it prints that letter al long as i keep the button pressed).
This function(getch()) is not suitable for this problem? What alternatives i have to solve the problem?
#2
Re: Getch() Function
Posted 15 January 2005 - 08:59 PM
You can use GetASyncKeyState() API in windows.h or kbhit() which is in conio.h
But GetASyncKeyState() takes an ASCII code as a parameter and returns 1 or 0 depending on whether it was pressed or not.
kbhit() takes no parameter but returns 1 or 0 depending on whether any key was pressed or not.Here's an example which shows kbhit() functionality.
Run the program....Within 3 Seconds if you press Any Key...the ZERO will Turn into 1. And I guess u now know why...any trouble let me know.
But GetASyncKeyState() takes an ASCII code as a parameter and returns 1 or 0 depending on whether it was pressed or not.
kbhit() takes no parameter but returns 1 or 0 depending on whether any key was pressed or not.Here's an example which shows kbhit() functionality.
Run the program....Within 3 Seconds if you press Any Key...the ZERO will Turn into 1. And I guess u now know why...any trouble let me know.
#include <conio.h>
#include <stdio.h>
#include <windows.h>
void main()
{
long time=GetTickCount()+3000;
while(time>=GetTickCount())
{
gotoxy(13,13);
printf("%d",kbhit());
}
getch();
}
#4
Re: Getch() Function
Posted 22 January 2005 - 07:09 AM
Correct me if I'm wrong, but since the GetASyncKeyState() function returns a 1 or 0, I believe you could use the function like this:
while (GetASyncKeyState([desired key]))
{
[move pad desired direction]
}
This post has been edited by Link-Jun: 22 January 2005 - 07:10 AM
Page 1 of 1

Ask A New Question
Reply


MultiQuote





|