Linux under Analog key Kbhit (), Detect button termination while (1) dead loop

Source: Internet
Author: User
Tags vmin

How to control the while loop through another event and use keystrokes to control the while loop when necessary

1, set a loop flag. Loop in the main process and check the loop flag in each loop to decide whether to continue, change the flag after detecting the key input in the thread


2, key detection in the main process, the thread for the loop operation, the main process detects the key after the closed loop thread


3, in the loop to scan the keyboard buffer, to determine if there is a key information


Here we do not use multi-threaded mechanism, directly to detect the key information to control the while loop, when the button is pressed to terminate the while loop, that is, when scanning the button does not block while loop event. No nonsense directly on the code.

#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys /socket.h> #include <netinet/in.h> #include <termios.h> #include <termio.h> #include <unistd.h > #include <sys/ioctl.h> #include <fcntl.h> #include <errno.h> #include <regex.h> #include <sys/time.h>using namespace std;static struct Termio term_orig;static int kbdflgs;<span style= "Color:rgb (0, 130, 0); Font-family:consolas, ' bitstream Vera Sans Mono ', ' Courier New ', Courier, monospace; font-size:13.63636302947998px; line-height:15.397727012634277px; White-space:nowrap; " >/*  determine if there are keystrokes pressed  */</span>int kbhit (void) {struct Timeval tv;struct termios Old_termios, New_termios; int Error;int count = 0;tcgetattr (0, &old_termios); New_termios = Old_termios;new_termios.c_lflag &= ~ICANON;new _termios.c_lflag &= ~echo;new_termios.c_cc[vmin] = 1;new_termios.c_cc[vtime] = 0;error = tcsetattr (0, TCSANOW, & NeW_termios); tv.tv_sec = 0;tv.tv_usec = 100;select (1, NULL, NULL, NULL, &AMP;TV); error + = IOCTL (0, Fionread, &count); err or + = tcsetattr (0, Tcsanow, &old_termios); return error = = 0? Count:-1;} /*** system_mode* Reset the system to what it is before Input_mode was* called*/void system_mode (void) {if (IOCTL (0, Tcset A, &term_orig) = =-1) {return;} Fcntl (0, F_SETFL, Kbdflgs);} /*** input_mode* set the system into raw mode for keyboard i/o* returns 0-error* 1-no error*/int Input_mode ( void) {struct Termio term;if (IOCTL (0, Tcgeta, &term) = =-1) {return 0;} (void) IOCTL (0, Tcgeta, &term_orig); term.c_iflag = 0;term.c_oflag = 0;term.c_lflag = 0;term.c_cc[vmin] = 1;term.c_cc[ Vtime] = 0;if (IOCTL (0, Tcseta, &term) = =-1) {return 0;} Kbdflgs = fcntl (0, F_GETFL, 0); int flags = FCNTL (0, F_GETFL); flags &= ~o_ndelay;fcntl (0, F_SETFL, flags); return 1;} /*** getch* read a single character from the keyboard without echo* returns:the keypress character*/int getch (void) {unsigned Char Ch;input_mode (); while (read (0, &ch, 1)! = 1); System_mode (); return (CH);} int main () {while (!kbhit ()) {cout << ' Chenxun is a sb ' << Endl;}}


Linux under Analog key Kbhit (), Detect button termination while (1) dead loop

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.