读取用户输入,直到在C中按下ESC [英] Read user input until ESC is pressed in C

查看:88
本文介绍了读取用户输入,直到在C中按下ESC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在按下ESC键(或任何其他键)之前,是否有办法读取用户输入?我见过有关它的论坛,但它们都适用于C ++.我需要制作一个适用于C的软件.谢谢

Is there a way to read user input until the ESC key(or any other key) is pressed? I've seen forums about it but they've all been for C++. I need to make one that works for C. Thanks

推荐答案

让我们在ascii表中检查'esc'字符:

Let's check 'esc' character in ascii table:

$ man ascii | grep -i ESC
033   27    1B    ESC (escape)
$

因此,它的ascii值为:

Therefore, it's ascii value is:

  • '033'-八进制值
  • '27'-整数值
  • '1B'-十六进制值
  • 'ESC'-字符值

使用整数值"ESC"的示例程序:

A sample program using integer value of 'ESC':

#include <stdio.h>

int main (void)
{
    int c;

    while (1) {
        c = getchar();            // Get one character from the input
        if (c == 27) { break; }  // Exit the loop if we receive ESC
        putchar(c);               // Put the character to the output
    }

    return 0;
}

希望有帮助!

这篇关于读取用户输入,直到在C中按下ESC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆