Home | Join! | Help | Browse | Forums | NuWorld | NWF | PoPo   
Thought of the Day
Clapping Words
vice
epicure
obstreperous
torpid
matriculate
crescendo
anesthesiologist
enigmatic
repertoire


thecav
Age. 38
Gender. Male
Ethnicity. White
Location Prince George, VA
School. Univ of Virginia
» More info.
Good Times
Tuesday. 6.1.04 11:40 pm
LWell I got one thing accomplished today. I finished "The Da Vinci Code", yay! It was a *really* good book. I was right about it being the Holy Grail, but it was something much much more than that! *grins* I love books or movies that make you really think or get your gears in motion when you read it. This book definitely did that. I'll never look at the Last Supper or Leonardo Da Vinci the same again. Read and you'll understand.

OBesides reading, I did a few other things, not very interesting. Then I just kind of laid around and watched some TV with Twyla. Well.. that is when I wasn't being mercilessly tickled! But that's another story. ``Deranged Tickler! Until I come up with a better name, hehehe.

VOooooh, ok I love programming problems! Haha. I read Heather's latest entry and she wanted to know how to let console programs recieve input from the keyboard besides standard I/O mechanisms like "cin". I've got an answer for you, one that I find works fairly good. Here goes.

EThe answer to your problem (as are most in Windows programming) is solved via a Win32 API function. I don't know if you've used the Win32 API before, but its basically a set of functions that access the core kernel of the Windows OS. Meaning the API define the basic functions that makes Windows work. The function you want is called GetAsyncKeyState. This is its definition:

YSHORT GetAsyncKeyState( int vKey );

OThis function will return a value telling you whether a specific key is pressed or not. It takes as an argument an int specifying what key to test and returns a non-zero value if the key is pressed and returns zero if the key is not pressed. A list of the key codes can be found here. Use the chart on that site to find the numeric code of the key you want to check. Be aware that the numbers on that chart are written as hexadecimal numbers not base 10. The only prerequisite to using this function is to include the "windows.h" header file. This header file contains the function definition and all the key code constants used in the funtion.

UThe best way to show its use is in a sample program. In this program a console screen will appear and stay there. Whenever the user presses the "C" key on the keyboard the message "Hello Chris!" will appear in the console window. Pressing the escape key will close the program.

-----------------

#include <iostream>
#include "windows.h"

using namespace std;

int main()
{
   // Check for the escape key
   while (GetAsyncKeyState(VK_ESCAPE) == 0)
   {
      // Check for the "C" key
      if (GetAsyncKeyState( 0x43 ) != 0)
         cout << "Hello Chris!" << endl;
   }

   return 0;
}

-------------------

This compiled for me under Visual Studio.NET 2003. If if doesn't work for you try:

#include <iostream.h>
#include <windows.h>

*smTry that and make sure to delete the "using namespace std;" line if the original code listing doesn't work. It all depends on if your compiler uses the STL library or not. Let me claify a few things. One, as you can see it uses an infinite loop to keep checking the two key presses. Since the user can press the key at any time, you have to enter into an infinite loop waiting for the user to enter a key. Second, in the line checking for the "C" to be pressed, the argument passed to GetAsyncKeyState is "0x43". Notice the "0x" preceeding the numeric code. The "43" comes from that chart I gave to link to earlier, the prefix "0x" tells the compiler this is a hexadecimal number (which it is). Thirdly, notice how to check for the escape key the constant VK_ESCAPE is used. This is listed on the key code website and this constant is defined inside the "windows.h" header file. Lastly, if you run the program you'll notice that when the "C" key is pressed the message is written quite a few times to the screen. This is because the GetAsyncKeyState function only checks the key, but does not halt execution. So.. each time through the loop the key is pressed it prints the message. For example, if the loop runs through in 1 millisecond (a reasonable estimate) and you have the key pressed down for half a second, the message is printed 500 times. Beware of this behavior. You may have to include guards to make sure this behavior doesn't happen if it's undesirable.

ile*You can probably see how to extend this to use in your own programs. Let me know if you want to know anything else, ok? I love this stuff!

Later.*kiss kiss*
7 Comments.

umm...
(',')
» finallyfoundareasonnow on 2004-06-02 12:40:11

spellchecker
misspelled clarify....the spelling nazi strikes again!
» finallyfoundareasonnow on 2004-06-02 12:40:48

Thank you!
This should work perfectly! I'll try it out as soon as I have time to work on it.
» Bremma on 2004-06-02 06:18:04

...
It's scandalously apparent that you love this stuff. ;)
» EngelGenannt on 2004-06-02 12:52:30

Question(s)
1. Will this work with graphics?(void renderScene()) 2. I answered this mysef as Ii typed... I'm a dummy.
» Bremma on 2004-06-03 09:27:54


The second question I meant. Im still asking 1.
» Bremma on 2004-06-03 09:28:13

Gah!
Sorry for the triple post.. turns out I didn't answer my question.. ... and a realized you never beale to read what I am asking here, so my question will be posted in my blog. See it there.
» Bremma on 2004-06-03 09:32:18

Sorry, you do not have permission to comment.

If you are a member, try logging in again or accessing this page here.

thecav's Weblog Site • NuTang.com

NuTang is the first web site to implement PPGY Technology. This page was generated in 0.072seconds.

  Send to a friend on AIM | Set as Homepage | Bookmark Home | NuTang Collage | Terms of Service & Privacy Policy | Link to Us | Monthly Top 10s
All content � Copyright 2003-2047 NuTang.com and respective members. Contact us at NuTang[AT]gmail.com.