Jump to content
  • Advertisement
Sign in to follow this  
Plasmarobo

Non halting Input/Keyscan function

This topic is 4407 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have looked through my reference guides (C/C++ Third Edition, Beginning Game prog in C++) and I haven't found any keyscan functions. I am looking for a function that scans for keypresses, without halting the program. Get(), Cin, and those things stop my program dead. This is good for command input, but I need some action input. I have done some internet and reverse engineering research and found these functions... Is one better than the others? Or all they all pretty much the same? Thanks! So the functions that I have found are: (char) getchar(); scanf(); gets(x); (How do I get a pointer to the main input buffer, set it to null?) Also, I have found that using a buffer to the console input events is overkill. I just need to scan the keyboard, not the mouse or any other device. Is there a console function for JUST scanning the keyboard? Thanks for your help.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by King Mir
Random google result of kbhit(): http://www.scriptlogic.com/Kixtart/htmlhelp/functions/kbhit.htm

I haven't actually used it much so I don't know what problems it might have.

Edit: How do I make a link on these forums?

Random google result of kbhit()

Like so:
<a href="http://www.scriptlogic.com/Kixtart/htmlhelp/functions/kbhit.htm">Random google result of kbhit()</a>

Share this post


Link to post
Share on other sites
HTML aside, now that I know how to "ping" the buffer, I need to be able to read the key from it. The KbHit thingy doesn't quite work that way. Any suggestions?

Am I just better off using the ReadConsoleInput buffer "and friends" functions?

Share this post


Link to post
Share on other sites
This is from my personal game engine. The code was taken from: Tricks of the Windows Game Programming Gurus, 1st or 2nd edition, little changed, just updated references. This is what you'll need.

This is the defines for the keyscan functions:
#define KeyDown(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KeyUp(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)

You will use KeyDown of course to check for the key code. Now this is how it is used:

if(KeyDown(VK_ESCAPE)){
// Your key code here
}

All of the key codes can easily be found in the Microsoft MSDN. You'll need to include windows.h for this to work. But this is only for Windows. And it is compatible with all version, for a portable version, you will need to go with the command described prior to my post.

Hope this helps.

Share this post


Link to post
Share on other sites
I think I did this ages ago by first calling kbhit, then calling getch/getchar if the return value was true. Not sure if that'll do the job for you, though.

Regards,
ZE

Share this post


Link to post
Share on other sites
As ZealousElixir said, you use (from conio.h) kbhit to check for a keypress and then getch to retrieve the specific character. Actually it returns an int so you cast if you want a char. Here is a function for a simple console app I wrote...


void Console::getInput()
{
if ( _kbhit() ) // if a keypress is detected
{
char c = static_cast<char>(_getch()); // get the character

if (c == '\r') // enter key
{
if (buffer.empty()) // nothing was typed
{
prompt();
return;
}

if (buffer == "quit" || buffer == "exit") // exit command was typed
{
m_active = false;
cout << endl;
return;
}

cout << endl;
queueCommand(buffer);
buffer.clear();
}
else if (c == '\b') // backspace key
{
if (buffer.empty()) // nothing to erase
return;

buffer.erase(buffer.end()-1, buffer.end()); // erase last char from buffer
cout << "\b \b"; // erase last char from screen
}
else // any other char
{
buffer += tolower(c); // append lowercase char to buffer
cout << c; // print it
}
}
}




It obviously has some stuff specific to my app but I think you can adapt it to yours. buffer is just a std::string. The main loop looks something like this...


while (console.active())
{
console.getInput();
console.executePendingCommand();
// ... other tasks, all return without pausing ...
Sleep(0);
}


Hope this helps.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!