Linux Keyboard Interrupts for Pascal, C, and C++
Example program: ttyio_test.cpp
/*
ttyio_test.cpp: a simple program that tests the locally written
ttyiofunction library. For more information about the
Carleton ttyiofunction library contact mtie@carleton.edu.
For information about how to build your own ttyiofunctions
refer to the book
"Advanced C Programming for Displays"
by
Marc J. Rochkind
to compile ttyio_test.cpp:
g++ -o ttyio_test ttyio_test.cpp -lttyiofunctions
-mtie 3/27/01
*/
#include <ttyiofunctions.h>
#include <iostream.h>
void main()
{
char c='z';
//initializes the screen and sets it up to break
inittty();
ttycbreak();
while(c != 'q') //this is the event loop
{
//if there is a keypress
if( kbhit() )
{
//grab the character
cin >> c;
//display the character
cout << "the character entered was -" << c << "-"<< endl;
}
}
//restore the terminal to normal mode
ttynorm();
}
Functions
int inittty(void)
- This function must be called before any other function in the ttyio library can be used. It determines which terminal IO is to be associated with. inittty returns 1 on success and 0 on failure, with an error message on stderr.
int ttycbreak(void)
- This function sets the terminal (determined by inittty above) to cbreak mode and prevents automatic echoing of characters received from the keyboard. This allows other functions (such as getch() and kbhit() ) to do their work character-by-character, without the need for carriage-returns in between. This function also sets things up so that a call to read will not wait forever for user input, but rather will return immediately whether there has been keyboard activity or not.
int ttynorm(void)
- This function restores the original terminal settings. It should *definitely* be called before your program exits, or you may notice odd behavior of your terminal (For example, you will not see letters you type. If this happens, simply press enter twice, then type the single word "reset", then press enter again. Your terminal should then behave normally.).
int kbhit(void)
- This function returns 1 if there are character(s) in the terminal’s input queue, otherwise if returns 0. It does not remove any characters from the queue. For Pascal programmers, kbhit returns a boolean.
char getch(void)
- This function attempts to read one character from the terminal's input buffer. It will return the first character in the queue if any characters are present at all, otherwise it will return a null character.
Note: After ttycbreak is called, white space characters such as Tab, Enter, Esc, Space, and Shift can and will mess up your program. Warn the users not to use those keys!
Note to Pascal programmers: initty, ttycbreak, and ttynorm are NOT declared as functions in the gpc library. Treat them as procedures.
Note: ttyiofunctions is a locally written library for use by Carleton College students and staff. It is unsupported, and not available outside of Carleton College.
- CS Mini Courses
- go.carleton.edu/lynda
- Installing Python On Your Home Computer
- PIL (Python Imaging Library) for Python 2.x
- Installing Java on your home computer
- Setting your Java CLASSPATH variable
- Java Documentation
- Scanner class
- TeX and LaTeX
- Running programs in the background
- Compiling in Linux
- Beep Program
- Makefiles
- Keyboard Interrupts
- Installing Cygwin







