2

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

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

1 Answers1

3

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

Community
  • 1
  • 1
artm
  • 17,291
  • 6
  • 38
  • 54
  • Hardly it would work. Normally, the program receives the characters from tty only when the user hits Enter. One needs ncurses or another library to detect keys pressed. – user31264 Nov 06 '16 at 02:24
  • @user31264 true, that terminal behaviour is OS dependent. – artm Nov 06 '16 at 02:37