简体   繁体   English

在OSX上无需输入即可获取密钥

[英]Getting key without enter on OSX

Please make sure you are using OSX! 请确保您正在使用OSX!

gcc info: gcc信息:

Using built-in specs. 使用内置规格。 Target: i686-apple-darwin11 Configured with: /private/var/tmp/llvmgcc42/llvmgcc42-2336.11~28/src/configure --disable-checking --enable-werror --prefix=/Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2 --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-prefix=llvm- --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin11 --enable-llvm=/private/var/tmp/llvmgcc42/llvmgcc42-2336.11~28/dst-llvmCore/Developer/usr/local --program-prefix=i686-apple-darwin11- --host=x86_64-apple-darwin11 --target=i686-apple-darwin11 --with-gxx-include-dir=/usr/include/c++/4.2.1 Thread model: posix gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00) 目标:i686-apple-darwin11配置为:/private/var/tmp/llvmgcc42/llvmgcc42-2336.11~28/src/configure --disable-checking --enable-werror --prefix = / Applications / Xcode.app / Contents /Developer/usr/llvm-gcc-4.2 --mandir = / share / man --enable-languages = c,objc,c ++,obj-c ++ --program-prefix = llvm- --program-transform-name = / ^ [cg] [^ .-] * $ / s / $ /-4.2 / --with-slibdir = / usr / lib --build = i686-apple-darwin11 --enable-llvm = / private / var / tmp /llvmgcc42/llvmgcc42-2336.11~28/dst-llvmCore/Developer/usr/local --program-prefix = i686-apple-darwin11- --host = x86_64-apple-darwin11 --target = i686-apple-darwin11- with-gxx-include-dir = / usr / include / c ++ / 4.2.1线程模型:posix gcc版本4.2.1(基于Apple Inc.内部版本5658)(LLVM内部版本2336.11.00)

I am trying to get a char from keypress and display it. 我试图从按键中获取一个字符并显示它。 I am trying to do this without the Curses library (will be used for Android and OSX among others and I don't feel like porting). 我正在尝试在没有Curses库的情况下执行此操作(它将用于Android和OSX等,并且我不希望移植)。 Based on another post I came up with the following.... 根据另一篇文章,我提出了以下内容。

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

static char ch;
void getkey() {
  struct termios orig_term_attr;
  struct termios new_term_attr;

  /* set the terminal to raw mode */
  tcgetattr(fileno(stdin), &orig_term_attr);
  memcpy(&new_term_attr, &orig_term_attr, sizeof(struct termios));
  new_term_attr.c_lflag &= ~(ECHO|ICANON);
  new_term_attr.c_cc[VTIME] = 0;
  new_term_attr.c_cc[VMIN] = 0;
  tcsetattr(fileno(stdin), TCSANOW, &new_term_attr);

  /* read a character from the stdin stream without blocking */
  /*   returns EOF (-1) if no character is available */
  char test = fgetc(stdin); 
  if(test != -1)
    printf("Value is : %c \n",test);
  ch = test;
  /* restore the original terminal attributes */
  tcsetattr(fileno(stdin), TCSANOW, &orig_term_attr);
}

int main()
{
  do
  {
    getkey();
    int ch2 = (int) ch;
    if(ch2 != -1){
      printf("%c \n",ch);
  }
  }while(1==1);
}

But this doesn't seem to clear the buffer so when I type a then b the c I see... 但这似乎并没有清除缓冲区,所以当我键入a然后b c时,我看到...

aababc aababc

This is currently being compiled and run on My OSX box with the commands gcc tect.c and ./a.out 当前正在使用命令gcc tect.c和./a.out对此文件进行编译并在“我的OSX”框中运行。

I would like it to be abc 我希望它是abc

Your code works but you print the character twice: 您的代码有效,但是您两次打印了字符:

printf("Value is : %c \n",test);
printf("%c \n",ch);

I've tried myself: 我已经尝试过了:

Value is : a 
a 
Value is : b 
b 
Value is : c 
c 
Value is : d 
d 

By the way, you should not use a global variable but return the key instead... 顺便说一句,您不应使用全局变量,而应返回键...

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

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