Meenu Elizabeth Meenu Elizabeth - 1 month ago 10
C Question

Print something when Ctrl-L is pressed in C

How would I go about implementing something like the following in C:

if (isPressed("ctrl-L")==true)
print("Hello, world");

Answer

Refer to ASCII ctrl codes:

http://academic.evergreen.edu/projects/biophysics/technotes/program/ascii_ctrl.htm

Ctrl-L is 0xOC. So you need to check the return from getchar to see if Ctrl-L is pressed. Something along the line:

system ("/bin/stty raw"); // avoid the need to press Enter
int c = getchar();
if( c == 0x0C )
{
    // isPressed( "Ctrl-L" );
    printf("Hello, world");   
}

Note getchar() usually requires Enter. So if you want the effect immediately after Ctrl-L then you need to modify the terminal effect. Details can be found here: How to avoid press enter with any getchar()