c - Number Pad [Enter] 不在 C 中?

标签 c macos keyboard stdin

我正在为一项大学作业编写一个小型 C 程序,我注意到我的代码中有一个奇怪的错误。我通常使用带短键盘的 iMac,但它的电池没电了,所以我插入了带数字键盘的标准 USB 键盘。

奇怪的是,如果我在数字键盘上按 [Enter],它似乎会执行常规 [Enter} 键的操作,但\n 我试图在我为读取键盘输入而创建的标准输入函数在我使用数字键盘的 [Enter] 键时不起作用。

什么意思?

这是我读取用户输入的函数:

/* This is my implementation of a stdin "scanner" function which reads
 * on a per character basis until the the termination signals are found
 * and indescriminately discarding all characters in the input in excess
 * of the supplied (limit) parameter.  Eliminates the problem of 'left-over'
 * characters 'polluting' future stdin reads.
 */
int readStdin(int limit, char *buffer) 
{
   char c;
   int i = 0;
   int read = FALSE;
   while ((c = myfgetc(stdin)) != '\n' && c != '\0') {
      /* if the input string buffer has already reached it maximum
       limit, then abandon any other excess characters. */
      if (i <= limit) {
         *(buffer + i) = c;
         i++;
         read = TRUE;
      }
   }
   /* clear the remaining elements of the input buffer with a null character. */
   for (i = i; i < strlen(buffer); i++) {
      *(buffer + i) = '\0';
   }
   return read;
}

/* This function used to wrap the standard fgetc so that I can inject programmable
 * values into the stream to test my readStdin functions.
 */
int myfgetc (FILE *fin) {
   if (fakeStdIn == NULL || *fakeStdIn == '\0')
      return fgetc (fin);
   return *fakeStdIn++;
}

注意:myfgetc 和随后的 *fakeStdIn 是我可以对我的代码进行单元测试并以编程方式将项目“注入(inject)”到标准输入流中的一种方式关于这个问题的建议:How do I write a testing function for another function that uses stdin input? .

最佳答案

这个小测试的输出是什么?

#include <stdio.h>
int main(int argc, char* argv[]) {
    int c;
    while((c=getchar()) != EOF) {
        printf("%d\n", c);
    }
    return 0;
}

关于c - Number Pad [Enter] 不在 C 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6091073/

相关文章:

macos - Subl 命令不起作用 - 未找到命令

MacOS : find out if a process (given a PID) is running in 32bit or in 64bit Intel mode

android - 键盘显示时隐藏标签栏

Android:尝试用 View 替换键盘时如何防止 View 跳转/闪烁

c - 在工作线程 epolled 和配置线程之间交换全局变量

创建一个幸运数字猜谜游戏(将数组传递给函数/调用函数)

c++ - DWMEnableBlurBehind 使我的界面控件半透明

C无限循环与链表

macos - NSImageView 动画

java - 键盘隐藏时隐藏光标