Switch to full style
C++ code examples
Post a reply

check keyboard events using C++

Sat Jan 26, 2013 9:03 pm

check and monitor keys(keyboard ) events using C++
cpp code
#include <windows.h>
#include <iostream>
using namespace std;

int main()
{
HANDLE hIn;
HANDLE hOut;
COORD KeyWhere;
COORD LoopWhere;
COORD EndWhere;
bool Continue = TRUE;
DWORD EventCount;
int LoopCount = 0;
int KeyEvents = 0;
INPUT_RECORD InRec;
DWORD NumRead;
unsigned char HoldKey;

hIn = GetStdHandle(STD_INPUT_HANDLE);
hOut = GetStdHandle(STD_OUTPUT_HANDLE);

cout << "Key Events : " << flush; // SHOW no. of ENTERY " Key Events "

KeyWhere.X = 15;
KeyWhere.Y = 0;
LoopWhere.X = 0;
LoopWhere.Y = 1;
EndWhere.X = 0;
EndWhere.Y = 2;
//////////////////// FIRST LOOP ///////////////////////////////////////////////
while (Continue)
{
SetConsoleCursorPosition(hOut,
LoopWhere);
cout << LoopCount++ << " " << flush;
Sleep(100); // To slow it down!!
cout<<EventCount<<endl;
GetNumberOfConsoleInputEvents(hIn,&EventCount);

//EventCount "DWORD" always start with "SAME NUMBER"

///////////////////// SECOND LOOP /////////////////////////////////////////////

while (EventCount > 0)
{

ReadConsoleInput(hIn,&InRec,1,&NumRead); //why?

if (InRec.EventType == KEY_EVENT ) // if event BY key
{
if (InRec.Event.KeyEvent.bKeyDown)//
{
cout<<"I am here"<<endl;
HoldKey = InRec.Event.KeyEvent.uChar.AsciiChar;
}

else

{
cout<<"Don't Play with me "<<flush<<endl;
++KeyEvents;
SetConsoleCursorPosition(hOut,
KeyWhere);
cout << KeyEvents << flush;// SHOW no. of ENTERY "keyEvents"

LoopCount = 0;

if (HoldKey =='x'|| InRec.Event.KeyEvent.dwControlKeyState & LEFT_ALT_PRESSED ) //why?
{


cout<<"I am in the hill"<<endl;
cout<<"NO ONO "<<endl;
SetConsoleCursorPosition(hOut,EndWhere);
cout << "Exiting..." << endl;
Continue = FALSE;

}

}//END OF THE "else"

}//END OF THE FIRST "if-condition"

GetNumberOfConsoleInputEvents(hIn,&EventCount);
}// END OF THE SECOND LOOP
}//END OF THE FIRST LOOP

return 0;
}




Post a reply
  Related Posts  to : check keyboard events using C++
 Handle Key event-keyboard-Get typed Character and its code     -  
 Read two numbers from the keyboard and display larger number     -  
 How can a GUI component handle its own events     -  
 Handle Focus events with FocusListener     -  
 Window events-handling by WindowListener     -  
 Text Listener for events TextListener-on TextArea-TextField     -  
 Check DNS error     -  
 check if number is odd or even     -  
 check if domain name is taken by php     -  
 check folder content using asp     -  

Topic Tags

C++ Inputs