#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    8
    Rep Power
    0

    Getting keystrokes in Windows


    I did a search a few days ago, and found a thread where Scorpions4ever posted this:

    If you're writing a DOS console program, use the getch() function:

    Code:
    int ch;
    while (1) {
       ch = getch();
       printf("Ascii value is %d\n", ch);
    }
    Hope this helps!
    As he said, it worked under DOS console, however, I need to do this in a windows program, for use with the winbgim.h library. If anyone has any help, your comments are greatly appreciated.

    SpacePirate
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,242
    Rep Power
    2222
    Handle the WM_CHAR message.

    You could also handle the WM_KEYDOWN and/or WM_KEYUP messages. The difference is that these two are fired off for any key activity and they return "virtual key codes". If the WM_KEYDOWN is translatable to an ANSI character, then a WM_CHAR message is sent to the current window.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    8
    Rep Power
    0
    Excellent! Thanks a lot! I did a quick google search for WM_KEYDOWN, and found this tutorial, complete with examples, etc. :D

    I only have a few more questions, mainly about the use of the code...

    Code:
      long FAR _export WndProc(HWND hwnd, UINT message,
                               UINT wParam,LONG lParam)
      {
        ...
        switch(message)
        {
           ...
           //handle other messages
           ...
          case WM_CHAR :
            //key entered was ASCII character stored in wParam
            return 0;
          case WM_SYSCHAR:
            //key entered was Alt-&ltASCII value stored in wParam>
            return 0;
          case WM_KEYDOWN :
            switch(wParam)
            {
              ...
              //check for other none ASCII keyboard entries
              ...
              case VK_LEFT :
                //left arrow keystroke trapped
                break;
              case VK_RIGHT :
                //right arrow keystroke trapped
                break;
              ...
              //check for other none ASCII keyboard entries
              ...
            }
            return 0;
          case WM_SYSKEYDOWN :
            //same structure as case for WM_KEYDOWN
            return 0;
        }
        return DefWindowProc(hwnd, message, wParam, lParam);
      }
    The above is from the link I posted. What I would like to know is when I can call this function. It seems to be a function I define, and can call in my main() loop.
    • Now, it seems to return a value from a switch, Is this possible?
    • Secondly, does it stop the program and wait for the user's input? I do not want this to happen, as I am trying to make a program where I move an image with keystrokes, with other events happening simultaneously.
    • Third, what would I write in my Main() to run this function?
    • Finally, what exactly is it returning, and how would I use this to, say, display the ascii value of the key on the screen?

    I'm sorry about the questions, but I am really not well versed in C++ yet, I'm more of a BASIC guy... :p

    Thanks a lot!
    ~SpacePirate
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,242
    Rep Power
    2222
    When you said you wanted to do this in a Windows program, I assumed that you knew how to write a Windows program. But after having read your questions, I seriously doubt that assumption.

    In an SDK program (Software Development Kit, AKA "the Windows API"), your main function creates an instance of your program and registers it. In that process, you define a WndProc callback function, which Windows will call when it has a message that needs to be handled. Then main() enters a message processing loop. But that's all mostly boiler-plate.

    In the WndProc function, you create a humungous switch statement with a case for every Windows message you want to handle. It is within those cases that you write your handlers; if you're smart, the case will call a function that does the handling. Part of that handling involves "message cracking" in which you decipher the two parameters that come with the message; their format and contents are different for different messages. And basically, that's it.

    If you use a framework like MFC (or even Visual BASIC), then that message loop and WndProc message routing is all done for you. You just tell the development studio which messages you want to handle and it creates the function skeleton for you; it even takes care of the "message cracking". Then you fill into the function skeleton with your code for handling that message.

    If none of that made any sense to you, then you need to study up on Windows programming before you go any further.
    Last edited by dwise1_aol; May 22nd, 2003 at 09:12 PM.
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    8
    Rep Power
    0
    Hrm. I did get the basic gist of it, and indeed, I'll need to look into programming for windows.

    Basically I switched to windows format, and used it the same as dos, mainly because in Borland C++ 5, the version I use, gives me trouble using graphics. I managed to get them to work, but it leaves some capdos program open, which makes it impossible to log out of windows, requiring me to restart.

    Well, thanks for your help!
    SpacePirate

IMN logo majestic logo threadwatch logo seochat tools logo