C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 22nd, 2003, 10:39 AM
SpacePirate SpacePirate is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 8 SpacePirate User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Getting keystrokes in Windows

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

Quote:
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

Reply With Quote
  #2  
Old May 22nd, 2003, 01:57 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,122 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 13 h 38 m
Reputation Power: 1949
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.

Reply With Quote
  #3  
Old May 22nd, 2003, 07:24 PM
SpacePirate SpacePirate is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 8 SpacePirate User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Excellent! Thanks a lot! I did a quick google search for WM_KEYDOWN, and found this tutorial, complete with examples, etc.

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...

Thanks a lot!
~SpacePirate

Reply With Quote
  #4  
Old May 22nd, 2003, 08:09 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,122 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 13 h 38 m
Reputation Power: 1949
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 08:12 PM.

Reply With Quote
  #5  
Old May 23rd, 2003, 08:49 AM
SpacePirate SpacePirate is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 8 SpacePirate User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Getting keystrokes in Windows

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap