nonblocking keyboard input in linux
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: nonblocking keyboard input in linux

  1. #1
    Join Date
    Feb 2005
    Posts
    42

    nonblocking keyboard input in linux

    Good night everyone,

    my issue this time is how to achieve a non-blocking input from my keyboard (through stdin or any other way). the way i tried to do that was:

    Code:
    	fd_set inset, outset;
    	int result,flags;
    
    // set stdin to nonblocking
    	flags = fcntl(STDIN_FILENO,F_GETFL);
    	fcntl(STDIN_FILENO,F_SETFL,flags | O_NONBLOCK);
    
    // initialize KB input && socket input set
    	FD_ZERO(&inset);
    	FD_SET(STDIN_FILENO,&inset);
    	FD_SET(connection,&inset);
    
    // initialize socket-write set
    	FD_ZERO(&outset);
    	FD_SET(connection,&inset);
    
    // block while waiting for something to happen
    	result = select(connection+1,&inset,&outset,0,0);
    	if( result== -1){ /* error in select call */
    
    	} else { /* will never be 0 because we set no timeout, so it must be an event */
    		if( FD_ISSET(STDIN_FILENO,&inset)!=0 ){ /* there is kb input */
    			char k[32];
    			read(STDIN_FILENO,&k,sizeof(k));
    		}
    		if( FD_ISSET(connection,&inset)!=0 ){ /* there's socket msg */
    		}
    		if( FD_ISSET(connection,&outset)!=0 ){ /* we can send msg on  socket */
    		}
    	}
    
    	cout << "leaving" << endl;
    
    	return 0;
    But this doesn't work, everything is still only processed after i return key is pressed. I tried looking at the setvbuf function and disabling the stdin line buffered default setting with the 1st line on my main call:
    Code:
    setvbuf(stdin,0,_IONBF,0);
    but that has yelded 0 results, only after pressing the return key will the select() return, i wanted it to return on any activity on stdin (or any other keyboard method). Thanks to all for your time and help in advance.

    Cheers,
    Alex Paes

  2. #2
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: nonblocking keyboard input in linux

    Try to use getch or getche to get keys pressed (of course you will not be able to use the backspace key to correct input).
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  3. #3
    Join Date
    Feb 2005
    Posts
    42

    Re: nonblocking keyboard input in linux

    thanks for the tip SuperKoko,

    I'll try that out still today, i'll post here if it solves my problem but i dont' think that's what i'm looking for...

    If anyone has any other ideas please post them. Cheers.
    Alex Paes

  4. #4
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: nonblocking keyboard input in linux

    If you are writing an X application, check the XGrabKey (or alternatively the XGrabKeyboard) command. If you are writing a terminal application, I would recommend using the ncurses library, which provides the command getch().

    Of course if you are using some toolkit like Qt or Gtk, there are far easier ways than using the X commands.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center