Keyboard interrupts

Discussion in 'C++' started by Victor Bazarov, Mar 28, 2005.

  1. Cyber wrote:
    > I want to catch any event on a PC by the interrupts (C++ under Linux).
    > My first step is to catch everything from the keyboard. I have:
    > [...]
    > It do not works... If this part can work, after, I will add more signal
    > definition (for other events, mouse, etc...);
    >
    > If anyone have an idea ?


    There is no way to do what you want using only Standard C++. Please
    ask your platform-specific question in a newsgroup dedicated to your
    platform (comp.os.linux.development.apps is probably it). They should
    have an idea or two.

    V
    Victor Bazarov, Mar 28, 2005
    #1
    1. Advertising

  2. Victor Bazarov

    Cyber Guest

    Hello !

    I want to catch any event on a PC by the interrupts (C++ under Linux).
    My first step is to catch everything from the keyboard. I have:

    ---------------------------------------------------

    #include <iostream>
    #include <csignal>
    using namespace std;

    void the_keyboard (int sig)
    {
    cout << "handling signal no. " << sig << endl;
    }

    main ()
    {
    int cont = 1;
    while (cont) {
    signal (1, the_keyboard); // catch interrupt No.1 (keyboard)
    }
    }

    ---------------------------------------------------

    It do not works... If this part can work, after, I will add more signal
    definition (for other events, mouse, etc...);

    If anyone have an idea ?

    Thanks,

    Cyber
    Cyber, Mar 28, 2005
    #2
    1. Advertising

  3. Victor Bazarov

    Phlip Guest

    Cyber wrote:

    > I want to catch any event on a PC by the interrupts (C++ under Linux).


    Your questions is about Linux, not C++, so you'd get much better results on
    a Linux newsgroup.

    > while (cont) {
    > signal (1, the_keyboard); // catch interrupt No.1 (keyboard)
    > }


    > It do not works... If this part can work, after, I will add more signal
    > definition (for other events, mouse, etc...);


    What are you _really_ trying to do? If you want to affect keystrokes, you
    should program to the layer of Linux that handles them the way you need. For
    example, if you use X Windows (via Gnome, KDE, or the various others), then
    you should research how their keyboard handlers work, and how to extend
    them.

    --
    Phlip
    http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces
    Phlip, Mar 28, 2005
    #3
  4. Victor Bazarov wrote:

    > There is no way to do what you want using only Standard C++. Please
    > ask your platform-specific question in a newsgroup dedicated to your
    > platform (comp.os.linux.development.apps is probably it). They should
    > have an idea or two.


    However we must keep in mind that C++ provides <csignal>.




    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
    Ioannis Vranos, Mar 28, 2005
    #4
  5. Ioannis Vranos wrote:
    > Victor Bazarov wrote:
    >
    >> There is no way to do what you want using only Standard C++. Please
    >> ask your platform-specific question in a newsgroup dedicated to your
    >> platform (comp.os.linux.development.apps is probably it). They should
    >> have an idea or two.

    >
    >
    > However we must keep in mind that C++ provides <csignal>.


    We must. Does it have anything to do with a keyboard?
    Victor Bazarov, Mar 28, 2005
    #5
  6. Cyber wrote:

    > Hello !
    >
    > I want to catch any event on a PC by the interrupts (C++ under Linux).
    > My first step is to catch everything from the keyboard. I have:
    >
    > ---------------------------------------------------
    >
    > #include <iostream>
    > #include <csignal>
    > using namespace std;
    >
    > void the_keyboard (int sig)
    > {
    > cout << "handling signal no. " << sig << endl;
    > }
    >
    > main ()
    > {
    > int cont = 1;
    > while (cont) {
    > signal (1, the_keyboard); // catch interrupt No.1 (keyboard)
    > }
    > }
    >
    > ---------------------------------------------------
    >
    > It do not works... If this part can work, after, I will add more signal
    > definition (for other events, mouse, etc...);
    >
    > If anyone have an idea ?



    signal assigns a function handler to a signal, so you should use it once in the above.

    Interrupt values are system-specific. Here is a program that handles ctrl-c by displaying
    its int value:



    #include <iostream>
    #include <csignal>


    void sigint_handler(int sig)
    {
    std::cout << "handling signal no. " << sig << "\n";
    }


    int main ()
    {
    using namespace std;

    signal(SIGINT, sigint_handler);

    while(true)
    ;
    }



    Of course you can displays the system-specific values of each of the standard ones with
    this way:


    #include <iostream>
    #include <csignal>

    int main ()
    {
    using namespace std;

    cout<<"SIGABRT: "<<SIGABRT<<"\n"
    <<"SIGFPE: "<<SIGFPE<<"\n"
    <<"SIGILL: "<<SIGILL<<"\n"
    <<"SIGINT: "<<SIGINT<<"\n"
    <<"SIGSEGV: "<<SIGSEGV<<"\n"
    <<"SIGTERM: "<<SIGTERM<<"\n";
    }


    which in my system are:

    C:\c>temp
    SIGABRT: 22
    SIGFPE: 8
    SIGILL: 4
    SIGINT: 2
    SIGSEGV: 11
    SIGTERM: 15

    C:\c>



    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
    Ioannis Vranos, Mar 28, 2005
    #6
  7. Here is what the last draft of C90 was saying:


    "4.7 SIGNAL HANDLING <signal.h>

    The header <signal.h> declares a type and two functions and defines
    several macros, for handling various signals (conditions that may be
    reported during program execution).

    The type defined is

    sig_atomic_t

    which is the integral type of an object that can be accessed as an
    atomic entity, even in the presence of asynchronous interrupts.

    The macros defined are

    SIG_DFL
    SIG_ERR
    SIG_IGN

    which expand to distinct constant expressions that have type
    compatible with the second argument to and the return value of the
    signal function, and whose value compares unequal to the address of
    any declarable function; and the following, each of which expands to a
    positive integral constant expression that is the signal number
    corresponding to the specified condition:

    SIGABRT abnormal termination, such as is initiated by the abort function

    SIGFPE an erroneous arithmetic operation, such as zero divide or an
    operation resulting in overflow

    SIGILL detection of an invalid function image, such as an illegal
    instruction

    SIGINT receipt of an interactive attention signal

    SIGSEGV an invalid access to storage

    SIGTERM a termination request sent to the program

    An implementation need not generate any of these signals, except as
    a result of explicit calls to the raise function. Additional signals
    and pointers to undeclarable functions, with macro definitions
    beginning, respectively, with the letters SIG and an upper-case letter
    or with SIG_ and an upper-case letter,/97/ may also be specified by
    the implementation. The complete set of signals, their semantics, and
    their default handling is implementation-defined; all signal values
    shall be positive.


    4.7.1 Specify signal handling

    4.7.1.1 The signal function

    Synopsis

    #include <signal.h>
    void (*signal(int sig, void (*func)(int)))(int);

    Description

    The signal function chooses one of three ways in which receipt of
    the signal number sig is to be subsequently handled. If the value of
    func is SIG_DFL , default handling for that signal will occur. If the
    value of func is SIG_IGN , the signal will be ignored. Otherwise,
    func shall point to a function to be called when that signal occurs.
    Such a function is called a signal handler .

    When a signal occurs, if func points to a function, first the
    equivalent of signal(sig, SIG_DFL); is executed or an
    implementation-defined blocking of the signal is performed. (If the
    value of sig is SIGILL, whether the reset to SIG_DFL occurs is
    implementation-defined.) Next the equivalent of (*func)(sig); is
    executed. The function func may terminate by executing a return
    statement or by calling the abort , exit , or longjmp function. If
    func executes a return statement and the value of sig was SIGFPE or
    any other implementation-defined value corresponding to a
    computational exception, the behavior is undefined. Otherwise, the
    program will resume execution at the point it was interrupted.

    If the signal occurs other than as the result of calling the abort
    or raise function, the behavior is undefined if the signal handler
    calls any function in the standard library other than the signal
    function itself or refers to any object with static storage duration
    other than by assigning a value to a static storage duration variable
    of type volatile sig_atomic_t . Furthermore, if such a call to the
    signal function results in a SIG_ERR return, the value of errno is
    indeterminate.

    At program startup, the equivalent of

    signal(sig, SIG_IGN);

    may be executed for some signals selected in an implementation-defined
    manner; the equivalent of

    signal(sig, SIG_DFL);

    is executed for all other signals defined by the implementation.

    The implementation shall behave as if no library function calls the
    signal function.

    Returns

    If the request can be honored, the signal function returns the
    value of func for the most recent call to signal for the specified
    signal sig . Otherwise, a value of SIG_ERR is returned and a positive
    value is stored in errno .

    Forward references: the abort function ($4.10.4.1).


    4.7.2 Send signal

    4.7.2.1 The raise function

    Synopsis

    #include <signal.h>
    int raise(int sig);

    Description

    The raise function sends the signal sig to the executing program.

    Returns

    The raise function returns zero if successful, nonzero if unsuccessful."




    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
    Ioannis Vranos, Mar 28, 2005
    #7
  8. Victor Bazarov

    Stephen Howe Guest

    > However we must keep in mind that C++ provides <csignal>.

    Please develope your ideas.
    Where do we go with the fact that C++ provides <csignal>?

    Stephen Howe
    Stephen Howe, Mar 29, 2005
    #8
  9. Stephen Howe wrote:

    > Please develope your ideas.
    > Where do we go with the fact that C++ provides <csignal>?



    That the question is about inside the limits of ISO C++.




    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
    Ioannis Vranos, Mar 29, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. D.Sn

    Interrupts in VB (API)

    D.Sn, Jan 12, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    2,748
    Willy Denoyette [MVP]
    Jan 12, 2004
  2. Ciaran
    Replies:
    12
    Views:
    6,031
    Frank van Eijkelenburg
    Feb 27, 2004
  3. guru10
    Replies:
    1
    Views:
    1,176
  4. John Ladasky

    Keyboard interrupts, Idle vs. SciTE

    John Ladasky, Nov 2, 2006, in forum: Python
    Replies:
    4
    Views:
    455
    John Ladasky
    Nov 3, 2006
  5. Alex Young
    Replies:
    6
    Views:
    176
    Alex Young
    Oct 5, 2006
Loading...

Share This Page