Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ReadConsoleInput() Limitation Problems (Program Does not Continue)

Status
Not open for further replies.

Jambon

Programmer
Aug 18, 2005
1
0
0
GB
Hello There. I recently decided to learn C++, having had much experience with other Programming Languages. To Test my skill, I decided to create a console-based version of the nokia game 'snake'. So far everything is going well, except a small problem with the Keyboard Input. The program operates on a cycle followed by a .5 second delay, to slow down the movement of the 'snake'. The only problem is, if ReadConsoleInput() has no keyboard inputs to read, it pauses the script there until there is. As the snake is supposed to move on its own, This pause is unnessecery and unwanted, and I was wondering how to force the program to continue, or check if there are inputs to read before trying to access them. here is the input code so far. (this is only a small part of the acual code)


if(ReadConsoleInput(Input, &InputRecord, 1, &Events));
{
switch(InputRecord.Event.KeyEvent.wVirtualKeyCode)
{
case VK_UP:
player1.Direction = UP;
break;

case VK_DOWN:
player1.Direction = DOWN;
break;

case VK_RIGHT:
player1.Direction = RIGHT;
break;

case VK_LEFT:
player1.Direction = LEFT;
break;

}

 
GetAsyncKeyState is probably what I would use rather than ReadConsoleInput.
 
Taken from MSDN July 2002.

ReadConsoleInput
The ReadConsoleInput function reads data from a console input buffer and removes it from the buffer.

BOOL ReadConsoleInput(
HANDLE hConsoleInput, // handle to console input buffer
PINPUT_RECORD lpBuffer, // data buffer
DWORD nLength, // number of records to read
LPDWORD lpNumberOfEventsRead // number of records read
);
Parameters
hConsoleInput
[in] Handle to the console input buffer. The handle must have the GENERIC_READ access right. For more information, see Console Buffer Security and Access Rights.
lpBuffer
[out] Pointer to an array of INPUT_RECORD structures that receives the input buffer data. The total size of the array required will be less than 64K.
nLength
[in] Size of the array pointed to by the lpBuffer parameter, in array elements.
lpNumberOfEventsRead
[out] Pointer to a variable that receives the number of input records read.
Return Values
If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks
If the number of records requested in the nLength parameter exceeds the number of records available in the buffer, the number available is read. The function does not return until at least one input record has been read.

A process can specify a console input buffer handle in one of the wait functions to determine when there is unread console input. When the input buffer is not empty, the state of a console input buffer handle is signaled.

To determine the number of unread input records in a console's input buffer, use the GetNumberOfConsoleInputEvents function. To read input records from a console input buffer without affecting the number of unread records, use the PeekConsoleInput function. To discard all unread records in a console's input buffer, use the FlushConsoleInputBuffer function.

Windows NT/2000/XP: This function uses either Unicode characters or 8-bit characters from the console's current code page. The console's code page defaults initially to the system's OEM code page. To change the console's code page, use the SetConsoleCP or SetConsoleOutputCP functions, or use the chcp or mode con cp select= commands.

Windows 95/98/Me: ReadConsoleInputW is supported by the Microsoft Layer for Unicode. To use this, you must add certain files to your application, as outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems.

Maybe the GetNumberOfConsoleInputEvents, PeekConsoleInput and FlushConsoleInputBuffer functions are what you need. Look at the "Remarks" section.

HyperEngineer
If it ain't broke, it probably needs improvement.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top