Total members 11893 |It is currently Mon Nov 04, 2024 11:03 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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;
}




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : check keyboard events using C++
 Read two numbers from the keyboard and display larger number     -  
 Handle Key event-keyboard-Get typed Character and its code     -  
 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 request was GET or POST     -  



Topic Tags

C++ Inputs






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com