Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 04-28-2007, 05:39 AM   #1
LQ Newbie
Registered: Mar 2007
Distribution: slackware
Posts: 9

Rep: Reputation: 0
Keyboard interrupt handler and more

I need to serve keyboard interrupts in my application. I have done fair share of searching and I generally know how to register interrupt handler with kernel. But I don't know how to save/restore previous interrupt handler so when my application finishes computer remains usable. Also I need to have a function called on regular time intervals. I heard there is a kernel function for that but I don't know which one it is. Thank you in advance for any help.
Old 04-29-2007, 04:44 PM   #2
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
Originally Posted by john_crichton
I need to serve keyboard interrupts in my application. ... Also I need to have a function called on regular time intervals.
Hope this helps:
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

static void handler(int); /* signal handler */

int main(int argc, char* argv[]) {
    int x = 0;
    /* register the handler for keyboard interrupt */
    if (signal(SIGINT, handler) == SIG_ERR)
        fprintf(stderr, "Couldn't handle SIGINT.\n");
    /* register the handler for alarm signal */
    if (signal(SIGALRM, handler) == SIG_ERR)
        fprintf(stderr, "Couldn't handle SIGALRM.\n");
        /* set the alarm to go off in three seconds */
    while (1) {
        fprintf(stderr, "%d ", x++);
    return 0;

static void handler(int signum) {
    if (signum == SIGINT) {
        printf("Got an interrupt signal.\n");
    else if (signum == SIGALRM) {
        fprintf(stderr, "ALARM! ");

Last edited by taylor_venable; 04-29-2007 at 04:53 PM.
Old 04-29-2007, 04:47 PM   #3
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,575

Rep: Reputation: 181Reputation: 181
Register an interrupt handler from a kernel module - that's doable, but from an user-space application? I rather does not work this way (of course, depending on the OS).

In general, you would need to store old handler address and then restore it later (if you know where to write the address, it should be rather easy).

I don't know what you're doing and what's the reason you need your own interrupt handler (what with different types of keyboards?), but it may be a much better idea to get raw access to keyboard from the application.
Old 04-29-2007, 04:48 PM   #4
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,575

Rep: Reputation: 181Reputation: 181
One more note: the code taylor_venable posted is not about interrupt handling, but signal handling, what is a different issue. The original poster may be, however, making the same mistake.
Old 04-30-2007, 02:17 AM   #5
LQ Newbie
Registered: Aug 2006
Location: India
Distribution: suse,mandriva
Posts: 24

Rep: Reputation: 15
i can tell how i have implemented my keypad to get detected

request kernel for an interrupt in ur driver for keyboard
otherwise kernel will ignore the interrupt
request_irq(interrupt number,function to get executed on irq...)

write an irq routine that will get executed on recieve of interrupt (function what i have mentioned above) write it as short as possible

read the buffer on recieve of interrupt "copy_to_user" on completion of line
Old 07-16-2009, 11:30 AM   #6
LQ Newbie
Registered: Sep 2007
Posts: 1

Rep: Reputation: 0
Request keyboard interrupt handler for application

Can anyone send me the code for key board interrupt handling. I need to open the keyboard device and handle the keyboard interrupts and send my application the received key information. Thanks a lot.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
control is not going to interrupt handler when interrupt comes in serial driver sateeshalla Linux - Kernel 1 05-04-2006 10:43 AM
Linux Interrupt Handler lucky6969b Linux - Software 1 12-01-2005 12:55 PM
<0>Kernel panic: Aiee, killing interrupt handler! In interrupt handler - not syncing mrb Linux - Newbie 2 01-09-2005 10:47 AM
Why this interrupt handler does not work? opereira Programming 1 03-04-2004 02:37 PM
Timer Interrupt Handler opereira Programming 1 03-03-2004 05:57 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:31 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration