Thread: Bug in key detection system...both known methods not working

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    37

    Angry Bug in key detection system...both known methods not working

    This particular code fragment runs till you press escape (Cred for the arrow-key detection system to Prelude )
    It is supposed to show the objects of a class from a file. (Note, the program works like this. There's only one object - obj - and it is changed, then read from/written to file)
    Code:
        while(!esc)
        {
            clrscr();
            esc=GetAsyncKeyState(VK_ESCAPE);
            cout<<"\nRecord "<<i+1<<"\n";
            fin.read((char *)&obj, sizeof(obj));
            gotoxy(20,4);
            cout<<"Name : "<<obj.name;
            gotoxy(20,5);
            cout<<"Email : "<<obj.email;
            gotoxy(20,6);
            cout<<"Phone number : "<<obj.phone_num;
            gotoxy(20,7);
            cout<<"Physical Address : "<<obj.address;
            gotoxy(20,8);
            cout<<"Webpage : "<<obj.web;
            gotoxy(20,9);
            cout<<"Yahoo ID : "<<obj.yahoo;
            gotoxy(20,10);
            cout<<"MSN ID : "<<obj.msn;        if(GetAsyncKeyState(VK_LEFT)&SHRT_MAX)
            {
                fin.seekg(-7,ios::cur);
                i--;
            }
            if(GetAsyncKeyState(VK_RIGHT)&SHRT_MAX)
            {
                fin.seekg(7,ios::cur);
                i++;
            }
        }
    Problem is, it does not wait for the keypress...I cannot work it out this way, either -
    Code:
    //http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1045691686&id=1043284392
    //Code by Lucky
    #include <iostream>
    #include <windows.h>
    using namespace std;
    bool keyHit(void)
    {
         HANDLE stdIn=GetStdHandle(STD_INPUT_HANDLE);
         DWORD saveMode;
         GetConsoleMode(stdIn, &saveMode);
         SetConsoleMode(stdIn, ENABLE_PROCESSED_INPUT);
         bool ret=false;
         if(WaitForSingleObject(stdIn, 1)==WAIT_OBJECT_0)
              ret=true;
         SetConsoleMode(stdIn, saveMode);
         return(ret);
    }
    bool custom_getch(TCHAR &ch)
    {
         bool ret=false;
         HANDLE stdIn=GetStdHandle(STD_INPUT_HANDLE);
         DWORD saveMode;
         GetConsoleMode(stdIn, &saveMode);
         SetConsoleMode(stdIn, ENABLE_PROCESSED_INPUT);
         if(WaitForSingleObject(stdIn, INFINITE)==WAIT_OBJECT_0)
         {
              DWORD num;
              ReadConsole(stdIn, &ch, 1, &num, NULL);
              if(num==1)
                        ret=true;
         }
         SetConsoleMode(stdIn, saveMode);
         return(ret);
    }
    TCHAR custom_getch(void)
    {
          TCHAR ch=0;
          custom_getch(ch);
          return(ch);
    }
    int main(void)
    {
        char n;
        for(;;)
        {
         cout<<"\nPress a key";
         n=custom_getch();
         cout<<"\n"<<n<<"\nDone";
         getchar();
        }
    }
    ...because that only takes characters.
    Using getchar() after the last member of the object is shown doesnt work, either.
    Please help asap...
    Edit - Just to specify, I need it to wait for a keypress...else I could've easily used Sleep().
    Last edited by ultrabot90; 12-23-2007 at 10:16 AM.

  2. #2
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    GetAsyncKeyState does not return a bool, it returns the state. You need to test the specific bits to extract the state information. Only bit 0 and 7 are defined, therefore bits 1-6 may contain undocumented values. By assuming they are zero, you open your program to undefined behaviour.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looking for a bug tracking system
    By williejoey in forum C++ Programming
    Replies: 8
    Last Post: 12-14-2007, 12:34 AM
  2. kbhit() for enter key? not working
    By yahn in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2006, 02:44 AM
  3. 2 child windows, top one not working
    By cdave in forum Windows Programming
    Replies: 4
    Last Post: 10-15-2005, 10:28 PM
  4. Replies: 0
    Last Post: 02-21-2002, 06:05 PM