Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hii,

I need to detect the press of F1 key using C code

I tried this code, but it wont work for F1
C#
   printf("Press F1 key to continue\n");
   while(getch() == 0x3b)
   {
       printf("\n F1 Pressed");
       getch();
   }

  
Posted
Updated 20-Oct-10 11:24am
v2

1 solution

The MSDN says that the getch() function is deprecated and suggest to use the _getch() function[^] instead. Its documentation states:

The _getch and_getwch functions read a single character from the console without echoing the character. None of these functions can be used to read CTRL+C.
When reading a function key or an arrow key, each function must be called twice; the first call returns 0 or 0xE0, and the second call returns the actual key code.

These functions lock the calling thread and are therefore thread-safe. For non-locking versions, see _getch_nolock, _getwch_nolock.


I think that this should work properly:

C++
printf("Press F1 key to continue.\n");
while (true)
{
   int c = _getch();
   if (c != 0x00 && c != 0xE0) continue;
   if (_getch() == 0x3B) break;
}
 
Share this answer
 
Comments
Aryan9999 21-Oct-10 3:26am    
Thanks dude

now its working :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900