Tuesday, July 14, 2009

Keypresses in C++ (linux)?

I am writing a simple text editor in C++ mainly as practice programming. It currently has a command-line like interface, where commands are entered then return/enter is pressed. For scrolling, that is really slow, so I want to have an interface that accepts just keypresses, but without waiting for you to press return. Something like emacs but without strung-together commands.





How would this be implemented in C++? It must work with Linux, not necessarily with Windows.

Keypresses in C++ (linux)?
I'm not even going to think this answer through. I don't need to. It's your editor.





kbhit() is part of conio.h. We Linux users have curses.h or ncurses.h. It's a similar input/output library with considerable differences which dates back to the venerable days of mainframes and (occasionally) minicomputers. It's still useful though.





Here is a guide to programming in ncurses:





http://www.apmaths.uwo.ca/~xli/ncurses.h...





Granted it's C rather than C++ but it still should be useful.





EDIT: I just noticed my recent reinstall of Debian didn't include curses.h. To compile with it I had to type:





"sudo apt-get install libncurses5-dev". I got the man pages and everything. If you are using Ubuntu or some other debian derivative you will definitely have to do that, but in my gentoo and Slackware boxes it's installed by default. So check with your system (type "yum search ncurses" in Fedora).





EDIT:





http://invisible-island.net/ncurses/ncur...





Is an FAQ about Ncurses from the developers. It does mention C++. (LATER:) and I awoke and remembered curses.h is used in the "make menuconfig" command when you recompile the kernel. That means if you're on a Debian-derived system like Ubuntu and you don't have it, it should be downloaded when you download the "build-essential" package. Since make, automake and other useful advanced tools are part of that, if you're on *buntu and you're doing a text editor and you haven't downloaded build-essential, do. My face is beet red.
Reply:Try kbhit().





if(khbit())


{


char Letter = getch();


}


That sees if there is information in the keyboard buffer and then uses getch() to retrieve the letter. You should be able to put it in a loop so it can get more key presses.

thank you cards

No comments:

Post a Comment