LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 10-03-2004, 11:27 PM   #1
poeta_boy
Member
 
Registered: Oct 2003
Location: Mexico
Distribution: Ubuntu
Posts: 223

Rep: Reputation: 31
Registering Keyboard events on C++, not cin >>


Hello:

I'm making a program that will control a little camera as a security device. That is, the camera is connected to the computer using the serial cable, and it will receive things that way.

I want that the user can press the UP key and the camera will move a little up. I've covered the movement and data transmission stuff, so I'm bassically stucked in the C++ programming and registering keyboard events thing.

I tried:

Code:
#include <iostream.h>
#include <stdlib.h>

int main()
{
      char key;
      cout << "Press a key " << endl;
      cin >> key;
      cout << "You pressed: " << key << endl;

      system("PAUSE");
      return 0;
}
but it doesn't do the trick, for the following reasons:

** It waits for the user to press enter, and I don't want that. Just the key to be pressed
** It only gets the keys with a "char" value, meaning, it does get the 'a' key but not the 'UP' key.
** Pressing UP key wil bring the last command pressed (as the shell usually do).

So I've googled for it and I found out that this type of keyboard management is not recognized by an ANSI standard because (curious) it's not standard.... I learned that the way the OS does this is different, lets say, from UNIX to Windows.

I've done some Windows specific programming before, like using Windows.h, handlers and all that, so that when I wanted to register keyboard I just had to do a switch and case with all the possible keys.... but I don't (can't) use MFC or win specific thing this time.... even tough I do need it for windows....

Is there a way to do this? even if it means to include ASM code inside the C++ programm?

Thanks a lot.
 
Old 10-04-2004, 03:15 AM   #2
coolfrog
Member
 
Registered: Sep 2004
Location: India
Distribution: Slackware 10.0
Posts: 77

Rep: Reputation: 15
hi,
I am very much interested in the work your doing. Could you explain in more detail how are you going about controlling the camera (sending commands etc).
Thanx.
 
Old 10-04-2004, 03:21 AM   #3
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
Basically, you need to turn off line-buffering on standard input (cin).

Internally, cin normally uses the C stdin stream, so you can probably just do:
Code:
#include <stdio.h>
int main() {
char buffer[1];
setvbuf(stdin,buffer,_IONBF,1);
}
See http://www.cplusplus.com/ref/cstdio/setvbuf.html

(Note that some C++ implementations reimplement the standard input stream seperatly from the C stdin stream; in this case, you'll have to dig the FILE pointer out of cin and pass that as the first argument to setvbuf. Also, I haven't tested this.)

Edit: In Linux, it's more usual to use a library like ncurses to trap the low-level keyboard events, but that obviously isn't easily portable to Windows.

Last edited by rjlee; 10-04-2004 at 03:22 AM.
 
Old 10-04-2004, 10:37 PM   #4
poeta_boy
Member
 
Registered: Oct 2003
Location: Mexico
Distribution: Ubuntu
Posts: 223

Original Poster
Rep: Reputation: 31
Hello:

Thanks a lot for the replay, unfortunately it keeps waiting for the enter..... any ideas
 
Old 10-05-2004, 03:22 AM   #5
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
You can use:
Code:
char c;
cin.get(&c);
to read a single character into c.

Note that special keys, such as up/down arrows may generate two characters.

A google search also yields this: http://forums.devshed.com/t104623/s....ghlight=curses

Last edited by rjlee; 10-05-2004 at 03:24 AM.
 
Old 10-05-2004, 09:55 AM   #6
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
Here's how to do it with ncurses:
Code:
#include <ncurses.h>

int main()
{
        int c;
        initscr();
        printw("Enter a character: ");
        c = getch();
        printw("\nYou entered %c.  Good job.\nPress any key to exit.", (char)c);
        (void)getch();
        endwin();
        return 0;
}
You might have to do something special to get the arrow keys to work -- I think http://www.faqs.org/docs/Linux-HOWTO...ing-HOWTO.html says how

Last edited by aluser; 10-05-2004 at 09:57 AM.
 
1 members found this post helpful.
  


Reply



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
Keyboard and mouse events Soltis Linux - General 3 03-23-2005 09:25 AM
Keyboard on USB adapter loses events Herr Kloßen Linux - Hardware 0 12-20-2004 12:50 PM
Mouse& keyboard events mvillarino Programming 0 05-27-2004 11:49 AM
Keyboard events under GNOME Dj Supreme Programming 1 02-13-2004 04:59 AM
how to capture mouse and keyboard events programatically karthikeyan Programming 3 02-21-2003 07:42 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:54 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration