Meenu Elizabeth Meenu Elizabeth - 1 year ago 90
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 Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download