linux下c程序中向上箭头键的输入

linux下c程序中向上箭头键的输入,c,linux,C,Linux,我想从用户那里获取输入,并检查用户是否放弃了箭头键作为输入。我尝试过getch函数,但它只允许用户输入一个字符。我希望用户可以输入上/下箭头键或任何包含多个字符的字符串。稍后我想检查用户是否放弃了up/down键作为输入或其他字符串。请帮助。如果您必须坚持不寻找您的答案 我试过了,但是我的编译器不支持conio库,所以我不能使用getch函数 为什么需要使用getch()?您可以同样轻松地使用任何其他函数来获取输入并为您完成工作 其次,我希望能够输入字符串,因此我必须使用一些函数,如fgets(

我想从用户那里获取输入,并检查用户是否放弃了箭头键作为输入。我尝试过getch函数,但它只允许用户输入一个字符。我希望用户可以输入上/下箭头键或任何包含多个字符的字符串。稍后我想检查用户是否放弃了up/down键作为输入或其他字符串。请帮助。

如果您必须坚持不寻找您的答案


我试过了,但是我的编译器不支持conio库,所以我不能使用getch函数

为什么需要使用
getch()
?您可以同样轻松地使用任何其他函数来获取输入并为您完成工作

其次,我希望能够输入字符串,因此我必须使用一些函数,如
fgets()

在我的示例中,如果您愿意,我使用了
fgets()

但问题是,
fgets()
不要将向上箭头作为输入。我还有别的办法吗

在终端上输入向上箭头时,每台计算机上的向上箭头看起来可能不同。例如,在我的计算机上,当我输入一个向上箭头时,它看起来是这样的:
^[[A
。然而,看起来,
fgets()
确实将箭头作为输入。正如您在链接的问题中所看到的:

按一个箭头键
getch
将三个值推入 缓冲区:

  • '\033'
  • “[”

  • “A”、“B”、“C”或“D”

“A”表示向上箭头,“B”表示向下箭头,“C”表示输入右箭头,而“D”表示输入左箭头。在我的示例中,我只处理了向上箭头,但应该很容易调整程序以检测其他箭头

现在来看一个例子:

#include <stdio.h>
#include <string.h>

#define MAX_STRING_SIZE 100

void flush_input(char userInput[]);

int main(void) {
  char input[MAX_STRING_SIZE] = {0};
  int i;
  int upArrow = 0; //1 if up arrow found, 0 if not found

  printf("Enter anything with an up arrow key (or not if you so choose):\n");
  fgets(input, sizeof(input), stdin);
  flush_input(input);

  size_t inputLen = strlen(input);

  for (i = 0; i < (int)inputLen; i++) {
    if (input[i] == '\33') {
      if(input[i+2] == 'A') {
        upArrow = 1;
      }
    }
  }

  if (upArrow != 0) {
    printf("You entered an up arrow somewhere in the input\n");
  } else {
    printf("You did not enter an up arrow anywhere\n");
  }
  return 0;
}


void flush_input(char userInput[]) {
  if(strchr(userInput, '\n') == NULL) {
    /* If there is no new line found in the input, it means the [enter] character
    was not read into the input, so fgets() must have truncated the input
    therefore it would not put the enter key into the input.  The user would
    have typed in too much, so we would have to clear the buffer. */
    int ch;
    while ( (ch = getchar()) != '\n' && ch != EOF );
  } else {
    //If there is a new line, lets remove it from our input.  fgets() did not need to truncate
    userInput[strcspn(userInput, "\r\n")] = 0;
  }
}

因此,总之,我不知道为什么您认为由于您不能使用
getch(),所以
。您用于获取输入的功能完全不相关。此过程中唯一重要的部分是了解如何在终端中输入箭头键。

如果您必须坚持不寻找答案


我试过了,但是我的编译器不支持conio库,所以我不能使用getch函数

为什么需要使用
getch()
?您可以同样轻松地使用任何其他函数来获取输入并为您完成工作

其次,我希望能够输入字符串,因此我必须使用一些函数,如
fgets()

在我的示例中,如果您愿意,我使用了
fgets()

但问题是,fgets()不要将向上箭头作为输入。还有其他方法可以做到这一点吗

当在终端上输入向上箭头时,每台计算机上的向上箭头看起来可能不同。例如,在我的计算机上,当我输入向上箭头时,它看起来是这样的:
^[[A
。然而,看起来,
fgets()
确实将向上箭头作为输入。正如您在链接问题中看到的:

按一个箭头键
getch
将三个值推入 缓冲区:

  • '\033'
  • “[”

  • “A”、“B”、“C”或“D”

“A”表示向上箭头,“B”表示向下箭头,“C”表示输入右箭头,而“D”表示输入左箭头。在我的示例中,我只处理了向上箭头,但应该很容易调整程序以检测其他箭头

现在来看一个例子:

#include <stdio.h>
#include <string.h>

#define MAX_STRING_SIZE 100

void flush_input(char userInput[]);

int main(void) {
  char input[MAX_STRING_SIZE] = {0};
  int i;
  int upArrow = 0; //1 if up arrow found, 0 if not found

  printf("Enter anything with an up arrow key (or not if you so choose):\n");
  fgets(input, sizeof(input), stdin);
  flush_input(input);

  size_t inputLen = strlen(input);

  for (i = 0; i < (int)inputLen; i++) {
    if (input[i] == '\33') {
      if(input[i+2] == 'A') {
        upArrow = 1;
      }
    }
  }

  if (upArrow != 0) {
    printf("You entered an up arrow somewhere in the input\n");
  } else {
    printf("You did not enter an up arrow anywhere\n");
  }
  return 0;
}


void flush_input(char userInput[]) {
  if(strchr(userInput, '\n') == NULL) {
    /* If there is no new line found in the input, it means the [enter] character
    was not read into the input, so fgets() must have truncated the input
    therefore it would not put the enter key into the input.  The user would
    have typed in too much, so we would have to clear the buffer. */
    int ch;
    while ( (ch = getchar()) != '\n' && ch != EOF );
  } else {
    //If there is a new line, lets remove it from our input.  fgets() did not need to truncate
    userInput[strcspn(userInput, "\r\n")] = 0;
  }
}

因此,总之,我不知道为什么您认为由于您不能使用
getch(),所以
。您用于获取输入的函数完全不相关。此过程中唯一重要的部分是了解箭头键是如何在终端中输入的。

看一看,请展示您迄今为止的研究/调试工作。请先阅读第页。怎么样?我尝试过,但我的编译器不支持conio库我不能使用getch函数。其次,我想也能输入字符串,所以我必须使用一些函数,如fgets。但问题是fgets不接受箭头作为输入。有没有其他方法可以做到这一点?@RaKo如何获得输入是不相关的。另外,你可以检测带有
fgets()的箭头键
。有关这方面的更多信息,请参阅我的答案。我的示例对您有用吗?请看一看。请展示您迄今为止的研究/调试工作。请先阅读第页。如何?我尝试了这个方法,但我的编译器不支持conio库,因此无法使用getch函数。其次,我希望能够输入字符串,因此我必须使用一些f功能类似于fgets。但问题是fgets不接受箭头作为输入。我还有其他方法可以做到这一点吗?@RaKo您如何获得输入是不相关的。另外,您可以检测带有
fgets()
的箭头键。有关这方面的更多信息,请参阅我的答案。我在其中的示例对您有用吗?