Find the answer to your Linux question:
Results 1 to 6 of 6
hi.. im trynig to write a program that will catch when the user types in ctrl+d and i ctrl+L...so when im waiting to input and catch it using fgets() or ...
  1. #1
    Just Joined!
    Join Date
    Oct 2007
    Posts
    60

    CTRL+D and CTRL+L signal handling

    hi..
    im trynig to write a program that will catch when the user types in ctrl+d and i ctrl+L...so when im waiting to input and catch it using fgets() or some other function, i want to check if its ctrl+l or ctrl+d, how can i do that?..coz strcmp() can't check for such things..at least i dont think so..
    and if these two keys generated a signal, then i should handle them using signal() function, but i dont know what signals they generated..i.e. ctrl+c generates a SIGINT..but i dont know what ctrl+d and ctrl+L generate..
    any help would be appreciated..thanks..

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    If you're using fgets() or another standard I/O function to get your input, you won't see the ^L until the user enters some sort of end of line character.

    Further, neither ^D nor ^L ordinarily generates a signal.

    If you wish to have more fine-grained control, there are other functions you should use instead for your keyboard input. Do this at the command line:
    Code:
    man tcgetattr
    man tcsetattr
    man 2 read
    If you do not have man pages installed on your system, google for this:
    Code:
    man tcgetattr linux
    man tcsetattr linux
    man read linux
    Hope this helps.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Just Joined!
    Join Date
    Oct 2007
    Posts
    60
    i know how to use the termios library, but the problem is that even if i use the termios functions, i still don't know what to check for..i mean ok so i use the termios to read a string, and then i want to check if this string is ctrl+l or ctrl+d, what are their hex values?

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    They're individual characters, not strings.

    ^A is 0x01.
    ^B is 0x02.

    And so on through the English alphabet.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  5. #5
    Just Joined!
    Join Date
    Oct 2007
    Posts
    60
    ok ctrl+L turned up to be 0x0C, and i tried using scanf, any stdin function to get the char and check if its ctrl+L and it worked using its hex value, but ctrl+D sends some kind of signal to terminate the process im working in..
    i.e. the execution of this program works fine:
    Code:
    #include <stdio.h>
    
    int main()
    {
    	char c;
    	printf("Write: ");
    	scanf("%c", &c);
    	if(c == 0x0C) printf("ctrl+L was entered\n");
    	return 1;
    }
    but the execution of this doesnt..
    Code:
    #include <stdio.h>
    
    int main()
    {
    	char c;
    	printf("Write: ");
    	scanf("%c", &c);
    	if(c == 0x04) printf("ctrl+D was entered\n");
    	return 1;
    }
    ctrl+d sends some kind of signal to terminate the process..but i thought it only sent a signal to kill a terminal process when its running...so how can ctrl+d be checked?..by signal()?..and if by signal(), whats the signal ctrl+d generates?..thanks..

  6. #6
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    how can ctrl+d be checked?
    Don't use standard input. Use the functions I mentioned here.
    --
    Bill

    Old age and treachery will overcome youth and skill.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...