getch

Why getch() returns before press any key?

有些话、适合烂在心里 提交于 2019-11-26 11:23:00
问题 int main(int argc, char *argv[], char *env[]) { printf(\"Press any key to exit.\\n\"); getch(); return 0; } According to the man page, getch should wait until any key is pressed ...but in fact it returns directly before press any key. (The value returned is -1 ). Why? Update I\'m on Linux. How can I implement Press any key to exit. , if not using getch() ? getchar() will only return after press Enter, it\'s not what I want. 回答1: On Linux, getch() is probably the curses function, which is

Equivalent function to C's “_getch()” in Java?

天涯浪子 提交于 2019-11-26 08:31:08
问题 I use Google Wave, and I want to emulate the ability to send messages before you actually hit the enter key. Is there a Java equivalent to the C function _getch() ? 回答1: I found a code, Equivalent function to C's “_getch() public static void getCh() { final JFrame frame = new JFrame(); synchronized (frame) { frame.setUndecorated(true); frame.getRootPane().setWindowDecorationStyle(JRootPane.FRAME); frame.addKeyListener(new KeyListener() { @Override public void keyPressed(KeyEvent e) {

How to have password echoed as asterisks

自作多情 提交于 2019-11-26 06:48:39
问题 I am trying to figure out how to prompt for a password, and have the users input echoed back as asterisks ( ******** ) So recently, I took on the project of creating a remote server that could be connected to using the socket module in Python. It\'s not yet done, as I am in the process of making the console used to connect to the server. I am trying make a login window where a user is prompted to enter their Username and Password, although when the password is entered I am looking for

How to implement getch() function of C in Linux?

荒凉一梦 提交于 2019-11-26 06:36:08
问题 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() ? 回答1: 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();

Using kbhit() and getch() on Linux

北城以北 提交于 2019-11-26 05:36:53
问题 On Windows, I have the following code to look for input without interrupting the loop: #include <conio.h> #include <Windows.h> #include <iostream> int main() { while (true) { if (_kbhit()) { if (_getch() == \'g\') { std::cout << \"You pressed G\" << std::endl; } } Sleep(500); std::cout << \"Running\" << std::endl; } } However, seeing that there is no conio.h , whats the simplest way of achieving this very same thing on Linux? 回答1: The ncurses howto cited above can be helpful. Here is an

How to get a single character without pressing enter?

梦想与她 提交于 2019-11-26 05:27:40
问题 How can I get a single keyboard character from the terminal with Ruby without pressing enter? I tried Curses::getch , but that didn\'t really work for me. 回答1: http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/2999 #!/usr/bin/ruby begin system("stty raw -echo") str = STDIN.getc ensure system("stty -raw echo") end p str.chr (Tested on my OS X system, may not be portable to all Ruby platforms). See http://www.rubyquiz.com/quiz5.html for some additional suggestions, including for

What is the equivalent to getch() & getche() in Linux?

淺唱寂寞╮ 提交于 2019-11-26 03:14:35
问题 I am not able to find the equivalent header file for conio.h in Linux. Is there any option for getch() & getche() function in Linux? I want to make a switch case base menu where the user will give his option just by pressing one key & process should be moved ahead. I don\'t want to let user to press ENTER after pressing his choice. 回答1: #include <termios.h> #include <stdio.h> static struct termios old, current; /* Initialize new terminal i/o settings */ void initTermios(int echo) { tcgetattr

getch and arrow codes

旧巷老猫 提交于 2019-11-26 02:36:54
问题 I\'m writing a programm that\'s using getch() to scan for arrow keys. My code so far is: switch(getch()) { case 65: // key up break; case 66: // key down break; case 67: // key right break; case 68: // key left break; } Problem is that when I press \'A\' , \'B\' , \'C\' or \'D\' the code will also executed, because 65 is the decimal code for \'A\' , etc... Is there a way to check for an arrow key without call others? Thanks! 回答1: By pressing one arrow key getch will push three values into the