我怎样才能检测是否有输入标准输入等待窗口? [英] How can I detect if there is input waiting on stdin on windows?
问题描述
我要检测是否存在输入在Windows上的标准输入等待。
我用下面的通用结构在Linux上:
FD_SET currentSocketSet;
timeval结构电视;
tv.tv_sec = 0;
tv.tv_usec = 0;
INT结果= 1;
而(结果大于0){
FD_ZERO(安培; currentSocketSet);
FD_SET(STDIN,&安培; currentSocketSet);
结果=选择(STDIN + 1,&安培; currentSocketSet,NULL,NULL,&安培;电视);
如果(结果== -1){
的printf(网络错误 - 选择出错的使用id =%d个\\ n,错误号);
返回;
}否则如果(结果大于0){
//...做东西
}
}
请注意:我不想处理类似的kbhit键盘和键盘功能。我想办法做什么我问。我不希望第三方库要么做到这一点,我想一个原生的Windows库调用才能得到答案。
注2:在Windows上高于code不能与Windows错误code 10038试图在了一个操作不是一个套接字,这可能意味着Windows不支持选择的标准输入<。 / p>
由于在的 ReadConsoleInput()
文档:
一个进程可以在的等功能,以确定当有未读控制台输入。当输入缓冲器不为空,一个控制台输入缓冲器手柄的状态信号
要确定一个控制台输入缓冲区的未读的输入记录的数量,使用的 GetNumberOfConsoleInputEvents 功能。读取控制台输入缓冲器的输入记录,而不影响的未读的记录数,使用的 PeekConsoleInput 功能。要放弃在控制台的输入缓冲区所有未读的记录,使用 FlushConsoleInputBuffer 功能。
块引用>您可以使用
GetStdHandle()
得到一个句柄STDIN在上述功能的使用。I want to detect whether or not there is input waiting on stdin in Windows.
I use the following generic structure on Linux:
fd_set currentSocketSet; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; int result = 1; while (result > 0){ FD_ZERO(¤tSocketSet); FD_SET(STDIN, ¤tSocketSet); result = select(STDIN+1, ¤tSocketSet, NULL, NULL, &tv); if (result == -1){ printf("Network Error -- select errored with id=%d.\n", errno); return; } else if (result > 0){ //...do stuff } }
Note: I do not want to deal with the keyboard and keyboard functions like kbhit. I want a way to do what I asked. I do not want a third party library to do this either, I would like a native Windows library call to get the answer.
Note #2: On windows the above code fails with windows error code 10038 "An operation was attempted on something that is not a socket" which probably means that windows does not support selecting on STDIN.
解决方案As stated in the
ReadConsoleInput()
documentation: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.
You can use
GetStdHandle()
to get a handle to STDIN for use in the above functions.这篇关于我怎样才能检测是否有输入标准输入等待窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!