Thread: reading keystrokes

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It's not "extra stuff", it's part of the code you need. It's not just one line to do this - it's about 15-20 lines. So you need all of this:
    Code:
    // Code supplied by LuckY
    
    #include <iostream> 
    #include <windows.h> 
    
    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 getChar(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 getChar(void)
    {
      TCHAR ch = 0;
      getChar(ch);
      return(ch);
    }
    
    int main(void)
    {
      std::cout << "Press a key" << std::endl;
      getChar();
      std::cout << "Done" << std::endl;
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #17
    Registered User
    Join Date
    Dec 2007
    Posts
    37
    you were almost right it was getchar(x) but you were really close

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by h3ckf1r3 View Post
    you were almost right it was getchar(x) but you were really close
    Since the code is C++, it implements two functions:
    bool getChar(TCHAR &ch);
    TCHAR getChar(void);

    The latter will work with what I wrote, whilst the former will work as you write it. Obvioulsy, calling just getChar() without assigning the return value to anything will not work.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #19
    Registered User
    Join Date
    Dec 2007
    Posts
    37
    Thanks for all the help you have been for me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-05-2009, 03:14 AM
  2. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  3. Replies: 2
    Last Post: 01-28-2008, 03:07 AM
  4. Reading a record from a File
    By David101 in forum C Programming
    Replies: 2
    Last Post: 12-14-2004, 06:42 PM
  5. Replies: 0
    Last Post: 07-12-2002, 01:40 PM