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> ** 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. |
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. |
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> (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. |
Hello:
Thanks a lot for the replay, unfortunately it keeps waiting for the enter..... any ideas :( |
You can use:
Code:
char 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 |
Here's how to do it with ncurses:
Code:
#include <ncurses.h> |
All times are GMT -5. The time now is 06:47 PM. |