Capturing control-key sequences

This is a discussion on Capturing control-key sequences within the C++ Programming forums, part of the General Programming Boards category; In a Linux system, how would you go about capturing Control-C (or any other control sequence)? Is it possible with ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    271

    Capturing control-key sequences

    In a Linux system, how would you go about capturing Control-C (or any other control sequence)? Is it possible with C++ or would I need to make C function calls?

    I found something like this for Win32, but I couldn't find anything for Linux. I'm assuming it's going to be a headache, but I'd like to see how much of a headache it will be. And no ncurses, please.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,500
    Remove windows.h, replace the call to Sleep() with a call to sleep(), and remove the final system("pause") and you should be there.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Thanks, worked like a charm.

    One thing though, I compiled it in debug mode with g++, and when I went through the thing with gdb, it would not step into
    Code:
      signal( SIGINT, ctrlc_handler );
    Also, my loop was set up like this:
    Code:
      while(!ctrlc_pressed){
        std::cout << ">>> ";
        std::getline(std::cin, str_dump);
        std::cout << str_dump << std::endl;
      }
    And obviously, when I try to check what happens when I press "^C", it ends up triggering the SIGINT for gdb and not my program. So is there a way to get inside what's happening? Or do I just have to read "signal.h"?

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by MSDN, via Salem
    This can cause a single-thread application such as UNIX
    God, I love MSDN.


    Anyway. You can't step into the signal() call because it's a system function, and there's no source for it. But it doesn't immediately call the signal handler anyway, so what do you want to step inside for?

    To send a signal to the process instead of GDB, set a breakpoint, and when you're in GDB you can use one of its commands to send arbitrary signals to the debugged process. Look in the help for the exact command.
    Last edited by CornedBee; 10-15-2007 at 04:51 PM.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Thanks. Anyway, the wikipedia entry on signal.h helped.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    And obviously, when I try to check what happens when I press "^C", it ends up triggering the SIGINT for gdb and not my program. So is there a way to get inside what's happening? Or do I just have to read "signal.h"?
    You can send a signal to your program with the GDB command signal. ^C is where I press CTRL-C.
    Code:
    $ cat ctrlc.c
    #include <stdio.h>
    #include <signal.h>
    
    static int quit = 0;
    
    void ctrlc(int sig);
    
    int main(void) {
        signal(SIGINT, ctrlc);
    
        while(!quit) {}
    
        puts("Exiting gracefully");
        return 0;
    }
    
    void ctrlc(int sig) {
        puts("Caught SIGINT");
        quit = 1;
    }
    $ gcc -g ctrlc.c -o ctrlc
    $ ./ctrlc 
    asdf
    Caught SIGINT
    Exiting gracefully
    $ gdb ./ctrlc 
    ...
    (gdb) run
    Starting program: /home/dwk/c/ctrlc 
    ^C
    Program received signal SIGINT, Interrupt.
    main () at ctrlc.c:11
    11          while(!quit) {}
    (gdb) signal SIGINT
    Continuing with signal SIGINT.
    Caught SIGINT
    Exiting gracefully
    
    Program exited normally.
    (gdb) q
    $
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 09:00 AM
  2. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  3. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21