I am writing a program that is supposed to go through a loop each time someone hits the Enter key, how is this done?
for example:
while( KeyPressed == Enter )
{
DoStuff;
}
I am writing a program that is supposed to go through a loop each time someone hits the Enter key, how is this done?
for example:
while( KeyPressed == Enter )
{
DoStuff;
}
theres a program that checks if enter key was pressedPHP Code:
#include <conio.h> // for getch();
#include <iostream> // for cout
using namespace std;
#define ENTER 13 // Make it easier for you, so you can just put ENTER instead of 13
int main()
{
int key; // Variable to store the key press
key=getch(); // Scan for keypresses
if( key == ENTER ) // Pretty straight forward, could use: if( key == 13 ) also
cout << "Enter key pressed\n"; // Display Enter key pressed
else // If the enter key wasnt pressed
cout << "Enter key not pressed\n"; // Display Enter key not pressed
return 0;
}
You could have two threads. One that handle the keyboard input, and one that acctually do stuff. These two threads must share the variable KayPressed.
you could also do like this, and this is how most games would work like (exept those that works like the above
This is an easy simplified example... Depending on your programming skills, you can expand this. I have for example an eventbased system where a keypress can trigger an event for the game object (player or UI or something like that), and a mouseclick can trigger an objects event if its pressed.Code:void mainloop() { while(bGameRunning) { GetInput(); // <- set the Kayboard/Mouse/Joystick structs UpdateObjects(); /* <- this function update all game objects, these object can use the Keyboard variable and do stuff depending on what key is pressed. EX "Walk North". */ Render(); } }
So... how you do this is up to your skills... If you asked how to get the keyboard input in the first place, then I just say www.google.com everything is there...