如何在Linux中实现C的getch()函数?

发布于 2024-09-10 12:49:40 字数 125 浏览 5 评论 0原文

在 TurboC++ 中,我可以使用 conio.h 中的 getch() 函数。但在Linux中,gcc不提供conio.h。如何获得getch()的功能?

In TurboC++, I can use the getch() function from conio.h. But in Linux, gcc doesn't provide conio.h. How can I get the functionality of getch()?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

往事随风而去 2024-09-17 12:49:40

试试这个 conio.h 文件:

#include <termios.h>
#include <unistd.h>
#include <stdio.h>

/* reads from keypress, doesn't echo */
int getch(void)
{
    struct termios oldattr, newattr;
    int ch;
    tcgetattr( STDIN_FILENO, &oldattr );
    newattr = oldattr;
    newattr.c_lflag &= ~( ICANON | ECHO );
    tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
    ch = getchar();
    tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
    return ch;
}

/* reads from keypress, echoes */
int getche(void)
{
    struct termios oldattr, newattr;
    int ch;
    tcgetattr( STDIN_FILENO, &oldattr );
    newattr = oldattr;
    newattr.c_lflag &= ~( ICANON );
    tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
    ch = getchar();
    tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
    return ch;
}

您还可以使用 ncurses 库在 gcc 中一些类似于 conio.h 的函数。

Try this conio.h file:

#include <termios.h>
#include <unistd.h>
#include <stdio.h>

/* reads from keypress, doesn't echo */
int getch(void)
{
    struct termios oldattr, newattr;
    int ch;
    tcgetattr( STDIN_FILENO, &oldattr );
    newattr = oldattr;
    newattr.c_lflag &= ~( ICANON | ECHO );
    tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
    ch = getchar();
    tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
    return ch;
}

/* reads from keypress, echoes */
int getche(void)
{
    struct termios oldattr, newattr;
    int ch;
    tcgetattr( STDIN_FILENO, &oldattr );
    newattr = oldattr;
    newattr.c_lflag &= ~( ICANON );
    tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
    ch = getchar();
    tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
    return ch;
}

You can also use the ncurses library in gcc for some functions similar to conio.h.

剪不断理还乱 2024-09-17 12:49:40

如果回显到屏幕不是问题,您可以尝试使用 stdio.h 中的 getchar()

If echoing to the screen is not a problem, you could try using getchar() from stdio.h.

打小就很酷 2024-09-17 12:49:40

getch() 似乎包含在内在 curses 库中。

getch() seems to be included in curses library.

夜未央樱花落 2024-09-17 12:49:40

根据这些解决方案 代码 您必须手动使用开源代码对于所描述的 getch() 和 getche() 函数,代码如下。

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

static struct termios old, new;

/* Initialize new terminal i/o settings */
void initTermios(int echo) 
{
  tcgetattr(0, &old); /* grab old terminal i/o settings */
  new = old; /* make new settings same as old settings */
  new.c_lflag &= ~ICANON; /* disable buffered i/o */
  new.c_lflag &= echo ? ECHO : ~ECHO; /* set echo mode */
  tcsetattr(0, TCSANOW, &new); /* use these new terminal i/o settings now */
}

/* Restore old terminal i/o settings */
void resetTermios(void) 
{
  tcsetattr(0, TCSANOW, &old);
}

/* Read 1 character - echo defines echo mode */
char getch_(int echo) 
{
  char ch;
  initTermios(echo);
  ch = getchar();
  resetTermios();
  return ch;
}

/* Read 1 character without echo */
char getch(void) 
{
  return getch_(0);
}

/* Read 1 character with echo */
char getche(void) 
{
  return getch_(1);
}

只需将其放在代码的主要方法之前即可

According to these solution code you must manually use open source code for getch() and getche() function as described the code is as following .

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

static struct termios old, new;

/* Initialize new terminal i/o settings */
void initTermios(int echo) 
{
  tcgetattr(0, &old); /* grab old terminal i/o settings */
  new = old; /* make new settings same as old settings */
  new.c_lflag &= ~ICANON; /* disable buffered i/o */
  new.c_lflag &= echo ? ECHO : ~ECHO; /* set echo mode */
  tcsetattr(0, TCSANOW, &new); /* use these new terminal i/o settings now */
}

/* Restore old terminal i/o settings */
void resetTermios(void) 
{
  tcsetattr(0, TCSANOW, &old);
}

/* Read 1 character - echo defines echo mode */
char getch_(int echo) 
{
  char ch;
  initTermios(echo);
  ch = getchar();
  resetTermios();
  return ch;
}

/* Read 1 character without echo */
char getch(void) 
{
  return getch_(0);
}

/* Read 1 character with echo */
char getche(void) 
{
  return getch_(1);
}

Just put it before your main method of code

神仙妹妹 2024-09-17 12:49:40

如果出于某种原因您无法使用诅咒,请尝试以下操作:

# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <ctype.h>
# include <termios.h>

/* get a single char from stdin    */
int getch(void)
{
   struct termios oldattr, newattr;
   int ch;
   tcgetattr(0, &oldattr);
   newattr=oldattr;
   newattr.c_lflag &= ~( ICANON | ECHO );
   tcsetattr( 0, TCSANOW, &newattr);
   ch=getchar();
   tcsetattr(0, TCSANOW, &oldattr);
   return(ch);
}

If, for any reasons, you can't use curses, try this:

# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <ctype.h>
# include <termios.h>

/* get a single char from stdin    */
int getch(void)
{
   struct termios oldattr, newattr;
   int ch;
   tcgetattr(0, &oldattr);
   newattr=oldattr;
   newattr.c_lflag &= ~( ICANON | ECHO );
   tcsetattr( 0, TCSANOW, &newattr);
   ch=getchar();
   tcsetattr(0, TCSANOW, &oldattr);
   return(ch);
}
忆伤 2024-09-17 12:49:40

在 Unix 中,getch() 是 ncurses 库的一部分。但我为 这个问题编写了一个解决方法,让您可以使用 getch-就像没有其他诅咒包袱的功能一样。

In Unix, getch() is part of the ncurses library. But I wrote a workaround for this question that lets you use getch-like functionality without the rest of the curses baggage.

执手闯天涯 2024-09-17 12:49:40

conio.h 仅在 Dos 中存在,

对于 linux,请使用

sudo apt-get install libncurses-dev

&然后

-lncurses

// 在 IDE 中,您必须链接它:
例如:代码块、设置 ->编译器->链接器设置,并添加“ncurses”

conio.h is only in Dos,

for linux, use

sudo apt-get install libncurses-dev

& then

-lncurses

// In IDE, you have to link it:
for example: codeblocks, Setting -> Compiler -> Linker setting, and add 'ncurses'

ぶ宁プ宁ぶ 2024-09-17 12:49:40

getch() 位于 libcurses 中。 curses 的使用有点复杂,因为它与底层终端有深层链接,并且必须进行初始化。带有libcurses初始化的curses getch()的工作示例位于getchar() 对于向上和向下箭头键返回相同的值 (27)

getch() is in libcurses. the use of curses is a bit more complex because it has deep links to the underlying terminal and has to be initialized. a working example for curses getch() with initialization of libcurses is in getchar() returns the same value (27) for up and down arrow keys

失与倦" 2024-09-17 12:49:40

您可以使用 libcaca 中的 getch() 等效项:

__extern int caca_conio_getch (void)

You can use getch() equivalent from libcaca:

__extern int caca_conio_getch (void)
ゃ人海孤独症 2024-09-17 12:49:40

您还可以使用系统命令来控制 Linux 中的终端,如下所示

char getch()    {

        char c;

        system("stty raw -echo");

        c = getchar();

        system("stty -raw echo");

        return c;

}

该功能不需要用户按 Enter 键并接受用户输入而不回显 它需要您将 stdlib.h 库添加到您的代码中

注意:该功能只是适用于基于 UNIX 的操作系统

任何改进或指出代码中的任何问题将不胜

感激

You can also use system command to control the terminal in linux like this

char getch()    {

        char c;

        system("stty raw -echo");

        c = getchar();

        system("stty -raw echo");

        return c;

}

This function does not requires the user to press enter and takes input from the user without echoing It requires you to add stdlib.h library to your code

Note : This function is only applicable to UNIX-based OS

Any improvements or pointing out any issues in the code will be appreciated

Regards

猫性小仙女 2024-09-17 12:49:40

如果您想在 Ubuntu 上使用 conio.h,请按照以下步骤操作:-

  1. 打开终端
  2. sudo apt-get install git
  3. git clone https://github。 com/zoelabbb/conio.h.git
  4. cd conio.h
  5. sudo mv conio.h /usr/include/
  6. 关闭 IDE 并再次打开。完毕。

如果您遇到问题,请点击此链接:在此处输入链接说明

If you want to use conio.h on Ubuntu, then follow these steps:-

  1. Open terminal
  2. sudo apt-get install git
  3. git clone https://github.com/zoelabbb/conio.h.git
  4. cd conio.h
  5. sudo mv conio.h /usr/include/
  6. Close your IDE and open again. Done.

If you are in trouble then follow this link: enter link description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文