How to detect kbhit() in C#??

  • Thread starter Thread starter Lakshan
  • Start date Start date
L

Lakshan

Hi,
How do you emulate a key board hit in C# without actually reading in
the character.

For instance I have the following code

Console.WriteLine("Press a enter to exit");
Console.ReadLine();

Instead of doing the above, I want to do something like the
following:

bool bExitFlagSet = false;
//The other thread may set the flag
//to true if certain conditions occur
SpawnAnotherThread();
Console.WriteLine("Press a enter to exit");
while(!kbhit() and (bExitFlagSet == false))
{
//be in the loop until a key press occurs
//or the bExitFlagSet is set to true from another
//thread
Sleep(5000);
}

How do I implement the kbhit() above. I am trying to find a way to
implement this in a console application.

Your help will be greatly appreciated.
Thanks
Lakshan

Lakshan
 
Lakshan said:
Hi,
How do you emulate a key board hit in C# without actually reading in
the character.

For instance I have the following code

Console.WriteLine("Press a enter to exit");
Console.ReadLine();

Instead of doing the above, I want to do something like the
following:

bool bExitFlagSet = false;
//The other thread may set the flag
//to true if certain conditions occur
SpawnAnotherThread();
Console.WriteLine("Press a enter to exit");
while(!kbhit() and (bExitFlagSet == false))
{
//be in the loop until a key press occurs
//or the bExitFlagSet is set to true from another
//thread
Sleep(5000);
}

How do I implement the kbhit() above. I am trying to find a way to
implement this in a console application.

Your help will be greatly appreciated.
Thanks
Lakshan

Lakshan

One way to do that might be to hook keyboard messages (WH_KEYBOARD).
Although, I am not sure on how you would check which window was hit..
 
Back
Top