The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Getting keystrokes in Windows
Discuss Getting keystrokes in Windows in the C Programming forum on Dev Shed. Getting keystrokes in Windows C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

May 22nd, 2003, 10:39 AM
|
|
Junior Member
|
|
Join Date: May 2003
Posts: 8
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
|

May 22nd, 2003, 01:57 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
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.
|

May 22nd, 2003, 07:24 PM
|
|
Junior Member
|
|
Join Date: May 2003
Posts: 8
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-<ASCII 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
|

May 22nd, 2003, 08:09 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
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.
|

May 23rd, 2003, 08:49 AM
|
|
Junior Member
|
|
Join Date: May 2003
Posts: 8
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
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|