2

Code snippet (Full code here.) :

int Process (void)
{
  for (;;)
  {
    unsigned char Char_Fore = getch();
    if (Char_Fore == 0xE0)
    {
      unsigned char Char_Back = getch();
      switch (Char_Back)
      {
        case 0x48:
          Up();
          break;
        case 0x50:
          Down();
          break;
      }
    }
    else if (Char_Fore == 0x0D)
      Enter();
  }
  return 0;
}

This code can run properly on some machine, but some can't because of the getch() function.

getch() or _getch() is a function declared in conio.h , and the return value would vary if your keyboard is not IBM set 2, or the different compiler were chosen (not Mingw-gcc).

Is there a better way to analyze the key which was pressed in C without printing on the screen and without using a deprecated function like getch() or _getch() in console?

Kevin Dong
  • 5,001
  • 9
  • 29
  • 62

2 Answers2

4

I know this isn't really an answer, but I don't have enough reputation to comment.

I recall using a header called termios.h, and using it I could turn off echoing of characters, now this was on a UNIX system, but I'd imagine there's a Windows equivalent.

Here's the pressanykey code with windows.h, perhaps you could adjust it to your needs? http://www.cplusplus.com/forum/articles/7312/#msg33734

JMercer
  • 352
  • 2
  • 9
3

Check out curses or ncurses, sounds like kind of thing you want to be using.

PDCurses seems to be the Windows option - Is ncurses available for windows?

Community
  • 1
  • 1
Graeme
  • 2,971
  • 21
  • 26