view raw
Meenu Elizabeth Meenu Elizabeth - 6 months ago 44
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");


Refer to ASCII ctrl codes:

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()