ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
so your trying to make a program that outputs to the console, but for what reason would it stop and wait for a keyrequest? needz more detailz on tha program. example code would be nice.
void moveU( int &x,int &y, int &w, int &h, int &vx, int &vy, int &ph, int &ps) {
char move = ' ';
do {
for (int a=0; a<1000; a++);
system("clear");
if ( move == 'k' && ps<(h-3) ) {
ps += 1;
} else if ( move == 'i' && ps>1) {
ps -= 1;
}
drawScreen(h,w,x,y,ph,ps);
cout << "ps is: " << ps << ", ph is: " << ph;
cout << ", x is: " << x << ", y is: " << y << ", ";
cout << "your last choice was: " << move << endl;
move = 'a';
cout << "x to quit, i to move up, k to move down: ";
cin >> move;
} while ( move != 'x' ) ;
}
i want this function to be able to loop but not wait for keyboard input
i copied your code and made a quick run of it and dont see any problem because you are requesting a key to be pressed right here "cin >> move". i didnt mess with drawScreen() since i dont have the code for it but from what i can tell it works fine.
your loop is your while(move != x) which is dependent on a key press.
Last edited by chewysplace; 02-26-2004 at 12:32 AM.
is there any way to change the code so it loops even if the user doesn't enter anything? like if they press a key it will take note of it and act on it, but otherwise it will just keep looping?
the coolest thing to do is use SIGIO and signal driven i/o for stdin. it's quite simple with a couple calls to fcntl() to set the file owner and signal driven i/o flag and installation of a signal handler for SIGIO. you can go about and do w/e you want, and when the user hits the keyboard you'll run the signal handler and read from the keyboard. then you'll get returned back to where you were.
Last edited by infamous41md; 02-26-2004 at 12:53 AM.
/*
* asynctest.c: use async notification to read stdin
*
* Copyright (C) 2001 Alessandro Rubini and Jonathan Corbet
* Copyright (C) 2001 O'Reilly & Associates
*
* The source code in this file can be freely used, adapted,
* and redistributed in source or binary form, so long as an
* acknowledgment appears in derived source files. The citation
* should list that the code comes from the book "Linux Device
* Drivers" by Alessandro Rubini and Jonathan Corbet, published
* by O'Reilly & Associates. No warranty is attached;
* we cannot take responsibility for errors or fitness for use.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
int gotdata=0;
void sighandler(int signo)
{
if (signo==SIGIO)
gotdata++;
return;
}
char buffer[4096];
int main(int argc, char **argv)
{
int count;
struct sigaction action;
memset(&action, 0, sizeof(action));
action.sa_handler = sighandler;
action.sa_flags = 0;
sigaction(SIGIO, &action, NULL);
fcntl(STDIN_FILENO, F_SETOWN, getpid());
fcntl(STDIN_FILENO, F_SETFL, fcntl(STDIN_FILENO, F_GETFL) | FASYNC);
while(1) {
/* this only returns if a signal arrives */
sleep(86400); /* one day */
if (!gotdata)
continue;
count=read(0, buffer, 4096);
/* buggy: if avail data is more than 4kbytes... */
write(1,buffer,count);
gotdata=0;
}
}
You could use fork() and have one process handle the input from the user and have the other process drawing on the screen (or whatever you want to do).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.