Non halting Input/Keyscan function

Started by
8 comments, last by Hollower 17 years, 9 months ago
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.
___________________________________________________Optimists see the glass as Half FullPessimists See the glass as Half EmptyEngineers See the glass as Twice as big as it needs to be
Advertisement
kbhit() or _kbhit()
seems to be what your looking for. Found in conio.h. It's Non-standard.
What does kbhit() do/return/etc
___________________________________________________Optimists see the glass as Half FullPessimists See the glass as Half EmptyEngineers See the glass as Twice as big as it needs to be
Random google result of kbhit()

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


[Edited by - King Mir on July 26, 2006 8:54:38 PM]
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>
------------------This is so stupid!
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?
___________________________________________________Optimists see the glass as Half FullPessimists See the glass as Half EmptyEngineers See the glass as Twice as big as it needs to be
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.
I think he is making a console app.
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

[twitter]warrenm[/twitter]

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.

This topic is closed to new replies.

Advertisement