Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Al Gorithm

C Input

This topic is 5356 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 mostly deal with output, and when I deal with input, I deal with Windows. When I went to make a console application, I encountered a problem; all the standard library functions for keyboard input for single character, getc and getchar, stop the program and wait for input before continuing. I need a way for it to continue to run the background processes while expecting character input. How would I solve this, as the standard functions fail to? [edited by - Al Gorithm on April 13, 2004 3:57:13 PM]

Share this post


Link to post
Share on other sites
Advertisement
You could make an input procedure.

You could do this by creating a thread, stuffing the getch() in there, and have the thread execute a procedure you supplied to it whenever the user presses a button.

Share this post


Link to post
Share on other sites
You can use kbhit(). It will retun true if a key has been pressed. Otherwise it returns 0; When you would call getc() to get the user input, first check kbhit() to see if there''s input waiting for you. If not, continue with the program.

Anyhow, that''s one way. There are others.

Share this post


Link to post
Share on other sites
You''re in luck... I just happen to be using console mode recently. And I found this example on the net:


#include "stdafx.h"
#include <windows.h>
#include <stdio.h>

/*int main(int argc, char* argv[])
{
return 0;
}
*/


VOID MouseEventProc(MOUSE_EVENT_RECORD);
VOID ResizeEventProc(WINDOW_BUFFER_SIZE_RECORD);
VOID KeyEventProc(KEY_EVENT_RECORD);
VOID GetInputEvents(VOID);


DWORD main(VOID)
{
HANDLE hStdin;
DWORD cNumRead, fdwMode, fdwSaveOldMode, i;
INPUT_RECORD irInBuf[128];
DWORD err;

// Get the standard input handle.


hStdin = GetStdHandle(STD_INPUT_HANDLE);
if (hStdin == INVALID_HANDLE_VALUE)
{
err = GetLastError();
DebugBreak();
}
// MyErrorExit("GetStdHandle");


// Save the current input mode, to be restored on exit.


if (! GetConsoleMode(hStdin, &fdwSaveOldMode) )
{
err = GetLastError();
DebugBreak();
}
// MyErrorExit("GetConsoleMode");


// Enable the window and mouse input events.


fdwMode = ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT;// | ENABLE_ECHO_INPUT;

if (! SetConsoleMode(hStdin, fdwMode) )
{

err = GetLastError();
DebugBreak();
}
// MyErrorExit("SetConsoleMode");


// Loop to read and handle the input events.


while (1)
{

// Wait for the events.


ReadConsoleInput(
hStdin, // input buffer handle

irInBuf, // buffer to read into

128, // size of read buffer

&cNumRead); // number of records read

// MyErrorExit("ReadConsoleInput");

DWORD foo = GetLastError();
// Dispatch the events to the appropriate handler.


for (i = 0; i < cNumRead; i++)
{
switch(irInBuf[i].EventType)
{
case KEY_EVENT: // keyboard input

//KeyEventProc(irInBuf[i].Event.KeyEvent);

printf("key\n");
break;

case MOUSE_EVENT: // mouse input

//MouseEventProc(irInBuf[i].Event.MouseEvent);

printf("Mouse\n");
break;

case WINDOW_BUFFER_SIZE_EVENT: // scrn buf. resizing

// ResizeEventProc(

// irInBuf[i].Event.WindowBufferSizeEvent);

break;

case FOCUS_EVENT: // disregard focus events

printf("focus\n");
break;

case MENU_EVENT: // disregard menu events

break;

default:
// MyErrorExit("unknown event type");

break;
}
}
}

return 0;
}


Only, you need to make one modification:
Call GetNumberOfConsoleInputEvents( hStdin, &cNumRead)
before the call to ReadConsoleInput. Reason being, ReadConsoleInput is a blocking call (waits until there are events).

However, GetNumberOfConsoleInputEvents is not. It will return the number of messages waiting. Or 0 if none, and in this case, just don''t call ReadConsoleInput.

Share this post


Link to post
Share on other sites
IIRC, there is no standard C function that will not block you''re program when attempting to recieve input. Any solution to this problem is, therefore, non-standard, and likely platform-specific.

Share this post


Link to post
Share on other sites
Dum dum dum....

Reading the thread before posting!

scanf blocks.

[edited by - Zipster on April 14, 2004 1:35:49 AM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!