LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Registering Keyboard events on C++, not cin >> (https://www.linuxquestions.org/questions/programming-9/registering-keyboard-events-on-c-not-cin-238471/)

poeta_boy 10-03-2004 11:27 PM

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.

coolfrog 10-04-2004 03:15 AM

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.

rjlee 10-04-2004 03:21 AM

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.

poeta_boy 10-04-2004 10:37 PM

Hello:

Thanks a lot for the replay, unfortunately it keeps waiting for the enter..... any ideas :(

rjlee 10-05-2004 03:22 AM

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

aluser 10-05-2004 09:55 AM

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


All times are GMT -5. The time now is 06:47 PM.