keystroke c++ that doesnt pause the program

so like ch key=getch(); is something i dont want

i have a program that is in an infinite loop but if i press 1 it would do one thing if i dont press anything it will continue looping
http://www.cplusplus.com/forum/articles/1295/

Also. Do a google search for GetAsyncKeyState
Last edited on
Actually, on Windows,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <windows.h>

using namespace std;

int main()
  {
  HANDLE hstdin = GetStdHandle( STD_INPUT_HANDLE );
  while (WaitForSingleObject( hstdin, 150 ) == WAIT_TIMEOUT)
    {
    cout << "Press the 'any' key..." << endl;
    }
  FlushConsoleInputBuffer( hstdin );
  cout << "Thanks!" << endl;
  return 0;
  }

If you are on Linux/Unix, let me know. You can also check out the NCurses library.
http://www.gnu.org/software/ncurses/
http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/

Good luck!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <ctime> 
#include <cstdlib>
#include <string>
#include <windows.h>
#include <ctime>
#include <conio.h>
#include <ctype.h>
#include <iomanip>

using namespace std;

void gotoy(int y);
void timechange(time_t start1);
void timeevent();

int boost=0;
int Eboost=0;
int power=40;
int HP=100;
int EHP=100;
int Epower=10;
int dtime=0;

time_t start, end,start1;

int main()
{
	
	start=time(NULL);
	srand((unsigned)time(0)); 
	cout<<"Fighting Game\n";
	cout<<"-------------\n";
	cout<<"Player 1\n";
	cout<<"HP: "<<HP<<"/100"<<endl<<endl<<endl;
	cout<<"Player 2\n";
	cout<<"HP: "<<EHP<<"/100"<<endl<<endl<<endl;
	cout<<"1. Attack (10 power)\n";
	cout<<"2. Power Attack (10 power)\n";
	cout<<"3. Cocaine (10 power)\n";
	cout<<"4. Heal (10 power)\n";
	
	do
	{		
		
		gotoy(3);
		cout<<"Player 1\n";
		cout<<"HP: "<<right<<setw(3)<<HP<<"/100"<<endl;
		cout<<"Power: "<<right<<setw(3)<<power<<"/100"<<endl<<endl;
		gotoy(7);
		cout<<"Player 2\n";
		cout<<"HP: "<<right<<setw(3)<<EHP<<"/100"<<endl;
		cout<<"Power: "<<right<<setw(3)<<Epower<<"/100"<<endl<<endl;
		timechange(start);
		
		
		if (GetAsyncKeyState(VK_UP)&&power>=10)
		{
			power-=10;
                        EHP-=10;
		}


	
	}
	while(EHP>1||HP>1);

	return 0;
}
void gotoy (int y)
{
	y-=1;
    COORD coord;
    coord.X = 0; coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void timechange(time_t start1)
{
	end=time(NULL);
	dtime=difftime(end, start1);
	if (dtime==1)
	{
		timeevent();
		dtime=0;
		start=end;
	}
	

}
void timeevent()
{
	power+=1;
	if (boost>0)
	{
		boost-=1;
		power+=3;
	}
	if (power>100)
	{power=100;}

	Epower+=1;
	if (Eboost>0)
	{
		Eboost-=1;
		Epower+=3;
	}
	if (Epower>100)
	{Epower=100;}

}


here is my code i tried GetAsyncKeyState but it happens too fast

so ill explain how my program works it uses a timer and the power builds up based on seconds so it needs to be in a constant loop to keep my screen updated

once a player has enough power you can preform a move

is there a command where it reads the key when the finger leaves the keyboard

Thanks

If the state of the key is acceptable with GetAsyncKeyState(), just set a timer and use GetTickCount(); Then you won't accept another power up for Xms.


thanks

how do i use GetAsyncKeyState with hexadecimals
Put 0x infront of the number to make it a hex.
Topic archived. No new replies allowed.