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]
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.
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.
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.
Anyhow, that''s one way. There are others.
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.Event.KeyEvent); <br></font><br> printf(<font color=darkred>"key\n"</font>);<br> <font color=blue>break</font>; <br> <br> <font color=blue>case</font> MOUSE_EVENT: <font color=gray>// mouse input <br></font><br> <font color=gray>//MouseEventProc(irInBuf.Event.MouseEvent); <br></font><br> printf(<font color=darkred>"Mouse\n"</font>);<br> <font color=blue>break</font>; <br> <br> <font color=blue>case</font> WINDOW_BUFFER_SIZE_EVENT: <font color=gray>// scrn buf. resizing <br></font><br> <font color=gray>// ResizeEventProc( <br></font><br> <font color=gray>// irInBuf.Event.WindowBufferSizeEvent); <br></font><br> <font color=blue>break</font>; <br> <br> <font color=blue>case</font> FOCUS_EVENT: <font color=gray>// disregard focus events <br></font><br> printf(<font color=darkred>"focus\n"</font>);<br> <font color=blue>break</font>;<br> <br> <font color=blue>case</font> MENU_EVENT: <font color=gray>// disregard menu events <br></font><br> <font color=blue>break</font>; <br> <br> <font color=blue>default</font>: <br> <font color=gray>// MyErrorExit("unknown event type"); <br></font><br> <font color=blue>break</font>; <br> } <br> }<br> } <br> <br> <font color=blue>return</font> 0; <br>} <br></pre><!–ENDSCRIPT–><br><br>Only, you need to make one modification:<br>Call GetNumberOfConsoleInputEvents( hStdin, &cNumRead)<br>before the call to ReadConsoleInput. Reason being, ReadConsoleInput is a blocking call (waits until there are events).<br><br>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.<br><br>
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.
Dum dum dum....
Reading the thread before posting!
[edited by - Zipster on April 14, 2004 1:35:49 AM]
Reading the thread before posting!
scanf
blocks. [edited by - Zipster on April 14, 2004 1:35:49 AM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement