February 17, 2008
Hi,
I've tried this example from conio.h library
 and couldn't got a reaction from _getch()
 on function and arrow keys.


#include <stdio.h>
#include <stdlib.h>

int main()
{
   int input;

   printf(" Press charcter key...");
   input = _getch();
   printf("\n'%c' was returned by _getch()\n",
	  input);
   return 0;
}

Could somebody say where I was wrong and what
should I do in this situation.

Thanks a lot.
Yevgeniy.
2/16/08
February 17, 2008
Yevgeniy Tsatsko skrev:
> Hi,
> I've tried this example from conio.h library
>  and couldn't got a reaction from _getch()
>  on function and arrow keys.
[snip]
> Could somebody say where I was wrong and what
> should I do in this situation.

You could build you own:
#include <stdio.h>
#include <windows.h>

WORD GetChar()
{
  DWORD NumEventsRead;
  INPUT_RECORD InputRecord;
  HANDLE StdIn = GetStdHandle(STD_INPUT_HANDLE);
  while(1)
  {
    if(!ReadConsoleInput(StdIn, &InputRecord, 1, &NumEventsRead))
      return 0;
    if(InputRecord.EventType & KEY_EVENT && InputRecord.Event.KeyEvent.bKeyDown)
    {
      if(InputRecord.Event.KeyEvent.wVirtualKeyCode != VK_CONTROL &&
         InputRecord.Event.KeyEvent.wVirtualKeyCode != VK_MENU  &&
         InputRecord.Event.KeyEvent.wVirtualKeyCode != VK_SHIFT)
      {
        return InputRecord.Event.KeyEvent.wVirtualKeyCode;
      }
    }
  }
}

int main()
{
   int input;

   printf(" Press charcter key...");
   input = GetChar();
   printf("\n'%c' was returned by _getch()\n", input);
   return 0;
}

Notice that this version does not distinguish between 'a' and 'A',
it merely return the ID of the key pressed. Both 'p' and 'P' will
print 'P' F1 will print 'p'. You can look op the values in vinuser.h
The value for F1 is called VK_F1