LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-26-2004, 12:07 AM   #1
rebelcan
Member
 
Registered: Jun 2003
Location: British Columbia, Canada
Distribution: Debian unstable
Posts: 83

Rep: Reputation: 15
nonblocking keyboard input?


does anybody know how to do nonblocking keyboard input for a console?

i'm trying to create a loop, but i don't want it to stop and wait for keyboard input if there isn't any.

any clues?
 
Old 02-26-2004, 12:10 AM   #2
chewysplace
Member
 
Registered: Sep 2003
Distribution: Slackware 10 w/ Kernel 2.6.8
Posts: 176

Rep: Reputation: 30
create a loop in what language?
 
Old 02-26-2004, 12:10 AM   #3
rebelcan
Member
 
Registered: Jun 2003
Location: British Columbia, Canada
Distribution: Debian unstable
Posts: 83

Original Poster
Rep: Reputation: 15
c++
 
Old 02-26-2004, 12:14 AM   #4
chewysplace
Member
 
Registered: Sep 2003
Distribution: Slackware 10 w/ Kernel 2.6.8
Posts: 176

Rep: Reputation: 30
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.
 
Old 02-26-2004, 12:21 AM   #5
rebelcan
Member
 
Registered: Jun 2003
Location: British Columbia, Canada
Distribution: Debian unstable
Posts: 83

Original Poster
Rep: Reputation: 15
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
 
Old 02-26-2004, 12:31 AM   #6
chewysplace
Member
 
Registered: Sep 2003
Distribution: Slackware 10 w/ Kernel 2.6.8
Posts: 176

Rep: Reputation: 30
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.
 
Old 02-26-2004, 12:41 AM   #7
rebelcan
Member
 
Registered: Jun 2003
Location: British Columbia, Canada
Distribution: Debian unstable
Posts: 83

Original Poster
Rep: Reputation: 15
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?
 
Old 02-26-2004, 12:44 AM   #8
chewysplace
Member
 
Registered: Sep 2003
Distribution: Slackware 10 w/ Kernel 2.6.8
Posts: 176

Rep: Reputation: 30
actualy i'm looking into it right now. rather interesting i think
i'll post something when i've figured it out.
 
Old 02-26-2004, 12:44 AM   #9
rebelcan
Member
 
Registered: Jun 2003
Location: British Columbia, Canada
Distribution: Debian unstable
Posts: 83

Original Poster
Rep: Reputation: 15
thanks for the help
 
Old 02-26-2004, 12:52 AM   #10
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
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.
 
Old 02-26-2004, 12:56 AM   #11
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
here i found this on my comp:
Code:
/*
 * 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;
    }
}
 
Old 02-26-2004, 01:26 AM   #12
chewysplace
Member
 
Registered: Sep 2003
Distribution: Slackware 10 w/ Kernel 2.6.8
Posts: 176

Rep: Reputation: 30
dang, thats a bit to go through. but much thanx! gona go through it in the morning. me sleepies.

Rebelcan, i'll check here in the morning to see if anybody has answered it or not, either way i'm still gona work on it.
 
Old 02-26-2004, 01:32 AM   #13
biosx
Member
 
Registered: Jul 2002
Location: Chicagoland
Distribution: Gentoo, Ubuntu
Posts: 63

Rep: Reputation: 15
Just another idea....

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).

read up on fork()
 
Old 02-26-2004, 07:51 AM   #14
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Set the terminal to non-canonical mode, and use select() with a (small) timeout to read from stdin.

Or, see:

http://www.linuxquestions.org/quest...p?postid=161647
 
Old 02-26-2004, 11:28 AM   #15
chewysplace
Member
 
Registered: Sep 2003
Distribution: Slackware 10 w/ Kernel 2.6.8
Posts: 176

Rep: Reputation: 30
Hko, the link you gave is dead. what did you type in for a search?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get keyboard input from C in Linux yanming Linux - Software 4 07-27-2007 01:51 AM
how do i disable keyboard input? thebover Linux - Newbie 5 08-24-2004 10:13 AM
my mouse input is takes as keyboard input in BASH e1000 Slackware 5 12-08-2003 03:00 PM
Keyboard input not on screen g_rat Programming 1 03-27-2003 11:50 PM
No keyboard input seen neo77777 Linux - General 5 07-12-2002 09:32 PM


All times are GMT -5. The time now is 11:39 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration