简体   繁体   English

按下键盘后如何终止正在执行的程序

[英]How to terminate an executing program once keyboard is pressed

Here is my code, my goal for this stage is creating an animation of ASCII, terminate it once any character is entered.这是我的代码,我这个阶段的目标是创建一个 ASCII 动画,一旦输入任何字符就终止它。 I use getch() for this stage but it seems not to work.我在这个阶段使用getch()但它似乎不起作用。

#include <stdio.h>
#include <time.h>
#include <curses.h>

int main() {
    int i, j, ms = 250;    
    const char *a = "*";
    struct timespec delay;
    delay.tv_sec = 0;
    delay.tv_nsec = ms * 1000000L;
    while(1) {
        for (i = 0; i < 4; i++) {
            printf("%c", a[i]);
            fflush(stdout);
            nanosleep(&delay, NULL);
            printf("\b");
        }
        if(getch() != -1)break;
        //printf("%c,%d\n",buffer,buffer);
    }
    return 0;
}

Before using any curses function, you need to call the initsrc function, manual say:在使用任何curses函数之前,都需要调用initsrc函数,manual说:

#include <curses.h>

WINDOW *initscr(void);

Description描述

initscr is normally the first curses routine to call when initializing a program. initscr 通常是初始化程序时要调用的第一个curses 例程。

The initscr code determines the terminal type and initializes all curses data structures. initscr 代码确定终端类型并初始化所有curses 数据结构。 initscr also causes the first call to refresh to clear the screen. initscr 还会导致第一次调用 refresh 以清除屏幕。 If errors occur, initscr writes an appropriate error message to standard error and exits;如果发生错误,initscr 将适当的错误信息写入标准错误并退出; otherwise, a pointer is returned to stdscr.否则,返回一个指向 stdscr 的指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM