Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
仅当按下C中的某个键时停止_C_Macos_Keyboard_While Loop - Fatal编程技术网

仅当按下C中的某个键时停止

仅当按下C中的某个键时停止,c,macos,keyboard,while-loop,C,Macos,Keyboard,While Loop,如果我有一个while循环,我只想在按下q键时停止,我会怎么做。 然而,我不想让它完全改变计划 #define TRUE 1 #define FALSE 0 typedef int boolean; int main(int argc,char* argv[]){ char *script = malloc(MAXPATH); script = "ls"; boolean a; a = TRUE; while(a){ //this is the while loop i want to br

如果我有一个while循环,我只想在按下q键时停止,我会怎么做。 然而,我不想让它完全改变计划

#define TRUE  1
#define FALSE 0
typedef int boolean;

int main(int argc,char* argv[]){
char *script = malloc(MAXPATH);
script = "ls";
boolean a;
a = TRUE;
while(a){ //this is the while loop i want to break incase of a keypress
system(script);
}

do something else 
something else....
这将在Mac OS X上运行


getchar()和getc()都会暂停响应,从而使循环停止

当主循环在stdin上读取char时,可以使计算在pthread中运行。如果您不只是想从stdin中读取字符,那么SDL库具有更好的输入控件。gtk还有窗口接收的事件。命令“xev”是一个适用于linux的xlib程序,其工作原理与此类似。它打开一个空白窗口,并在关键事件出现时读取它们。

您可以使计算在pthread中运行,而主循环在stdin上读取char。如果您不只是想从stdin中读取字符,那么SDL库具有更好的输入控件。gtk还有窗口接收的事件。命令“xev”是一个适用于linux的xlib程序,其工作原理与此类似。它会打开一个空白窗口,并在出现按键事件时读取它们。

我可以检测按键的C本机函数有:

 getchar() and getc()

 kbhit() is a function returns integer value whenever the key is pressed
您可以使用上述功能

 #include <stdio.h>
 #include <conio.h>
 #include <math.h>
 #include <time.h>

  int main()
  {
  int m;
  clrscr();
  do
    {

   if(kbhit())
     {
     if((m=getch())==97)
       {
        printf("Key a is pressed....\n");
       }
      }
 }  while(1);
    getch();
    return 0;
  }
#包括
#包括
#包括
#包括
int main()
{
int m;
clrsc();
做
{
if(kbhit())
{
如果((m=getch())==97)
{
printf(“按下a键…\n”);
}
}
}而(1),;
getch();
返回0;
}

可以检测按键的C本机函数有:

 getchar() and getc()

 kbhit() is a function returns integer value whenever the key is pressed
您可以使用上述功能

 #include <stdio.h>
 #include <conio.h>
 #include <math.h>
 #include <time.h>

  int main()
  {
  int m;
  clrscr();
  do
    {

   if(kbhit())
     {
     if((m=getch())==97)
       {
        printf("Key a is pressed....\n");
       }
      }
 }  while(1);
    getch();
    return 0;
  }
#包括
#包括
#包括
#包括
int main()
{
int m;
clrsc();
做
{
if(kbhit())
{
如果((m=getch())==97)
{
printf(“按下a键…\n”);
}
}
}而(1),;
getch();
返回0;
}
您可以在类UNIX操作系统中使用select()机制

多功能一体:

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/select.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
    fd_set readfds;
    struct timeval tv;
    int ch;
    int bool, ret;

    FD_ZERO(&readfds);
    bool = 1;
    while (bool) {
        FD_SET(STDIN_FILENO, &readfds);
        tv.tv_sec = 0;
        tv.tv_usec = 0;
        /* int select(int nfds, fd_set *readfds, fd_set *writefds,
         *           fd_set *exceptfds, struct timeval *timeout);         */
        ret = select(STDIN_FILENO + 1, &readfds, NULL, NULL, &tv);
        if (ret < 0) {
            perror("select error");
            exit(1);
        } else if (ret == 0) {
            /* timeout */
        } else if (FD_ISSET(STDIN_FILENO, &readfds)) {
            ch = fgetc(stdin);
            if (ch == 'q') {
                bool = 0;
            }
        }
        sleep(1);
        fprintf(stderr, ".");
    }

    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
int main(int argc,char*argv[])
{
fd_设置读取FDS;
结构时间值电视;
int-ch;
内特布尔,内特;
FD_零(&readfds);
bool=1;
while(bool){
FD_集(标准文件号和读取FDS);
tv.tv_sec=0;
tv.tv_usec=0;
/*int select(int NFD、fd_集*读FDS、fd_集*写FDS、,
*fd_集*例外FDS,结构时间值*超时)*/
ret=select(标准文件号+1,&readfds,NULL,NULL,&tv);
如果(ret<0){
perror(“选择错误”);
出口(1);
}else if(ret==0){
/*超时*/
}else if(FD_ISSET(标准文件号和readfds)){
ch=fgetc(标准偏差);
如果(ch='q'){
bool=0;
}
}
睡眠(1);
fprintf(标准“.”);
}
返回0;
}
您可以在类UNIX操作系统中使用select()机制

多功能一体:

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/select.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
    fd_set readfds;
    struct timeval tv;
    int ch;
    int bool, ret;

    FD_ZERO(&readfds);
    bool = 1;
    while (bool) {
        FD_SET(STDIN_FILENO, &readfds);
        tv.tv_sec = 0;
        tv.tv_usec = 0;
        /* int select(int nfds, fd_set *readfds, fd_set *writefds,
         *           fd_set *exceptfds, struct timeval *timeout);         */
        ret = select(STDIN_FILENO + 1, &readfds, NULL, NULL, &tv);
        if (ret < 0) {
            perror("select error");
            exit(1);
        } else if (ret == 0) {
            /* timeout */
        } else if (FD_ISSET(STDIN_FILENO, &readfds)) {
            ch = fgetc(stdin);
            if (ch == 'q') {
                bool = 0;
            }
        }
        sleep(1);
        fprintf(stderr, ".");
    }

    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
int main(int argc,char*argv[])
{
fd_设置读取FDS;
结构时间值电视;
int-ch;
内特布尔,内特;
FD_零(&readfds);
bool=1;
while(bool){
FD_集(标准文件号和读取FDS);
tv.tv_sec=0;
tv.tv_usec=0;
/*int select(int NFD、fd_集*读FDS、fd_集*写FDS、,
*fd_集*例外FDS,结构时间值*超时)*/
ret=select(标准文件号+1,&readfds,NULL,NULL,&tv);
如果(ret<0){
perror(“选择错误”);
出口(1);
}else if(ret==0){
/*超时*/
}else if(FD_ISSET(标准文件号和readfds)){
ch=fgetc(标准偏差);
如果(ch='q'){
bool=0;
}
}
睡眠(1);
fprintf(标准“.”);
}
返回0;
}

我很高兴亚历克斯·钱德尔的回答。我从没听说过波尔

poll()对于POSIX风格的系统(如提问者平台)是一个很好的答案

_kbhit()是MS Windows上最简单的答案。当然,他们的民意测验是不同的

1表示在我的列表中只有一个描述符块,100毫秒表示超时

对于stdin,我的文件句柄是{0

阅读手册,了解你可以询问的许多事件,我只想要POLLIN

#include <stdio.h>
#include <poll.h>
#include <errno.h>

static struct pollfd attention = { 0, POLLIN } ;

int main()
{
   int   x, y;

   for (;;)
   {
      x = poll(&attention, 1, 100);

      if (x < 0)
      {
         printf("problem %d\n", errno);
         break;
      }
      else if (x)
      {
         printf("you rang %x ?", x);
         y = getc(stdin);
         printf(" %c of course\n", y);
         if (y == '.') break;
      }
   }

   return 0;
}
#包括
#包括
#包括
静态结构pollfd attention={0,POLLIN};
int main()
{
int x,y;
对于(;;)
{
x=投票和注意,1100人;
if(x<0)
{
printf(“问题%d\n”,错误号);
打破
}
如果(x)
{
printf(“您拨打了%x?”,x);
y=getc(标准偏差);
printf(“当然是%c\n”,y);
如果(y='.')中断;
}
}
返回0;
}

我很高兴亚历克斯·钱德尔的回答。我从未听说过波尔()

poll()对于POSIX风格的系统(如提问者平台)是一个很好的答案

_kbhit()是MS Windows上最简单的答案。他们的Poll()当然不同

1表示在我的列表中只有一个描述符块,100毫秒表示超时

对于stdin,我的文件句柄是{0

阅读手册,了解你可以询问的许多事件,我只想要POLLIN

#include <stdio.h>
#include <poll.h>
#include <errno.h>

static struct pollfd attention = { 0, POLLIN } ;

int main()
{
   int   x, y;

   for (;;)
   {
      x = poll(&attention, 1, 100);

      if (x < 0)
      {
         printf("problem %d\n", errno);
         break;
      }
      else if (x)
      {
         printf("you rang %x ?", x);
         y = getc(stdin);
         printf(" %c of course\n", y);
         if (y == '.') break;
      }
   }

   return 0;
}
#包括
#包括
#包括
静态结构pollfd attention={0,POLLIN};
int main()
{
int x,y;
对于(;;)
{
x=投票和注意,1100人;
if(x<0)
{
printf(“问题%d\n”,错误号);
打破
}
如果(x)
{
printf(“您拨打了%x?”,x);
y=getc(标准偏差);
printf(“当然是%c\n”,y);
如果(y='.')中断;
}
}
返回0;
}

在哪个平台上?这个问题无法抽象回答。哪个平台?没有非平台特定的答案。没有标准C库提供的答案。Mac os x运行10.7.3 i686-apple-darwin11-llvm-gcc-4.2(gcc)4.2.1(基于apple Inc.build 5658)(llvm build 2336.1.00)在哪个平台上?这个问题不能抽象地回答。在哪个平台上?没有答案不是p