Interrupts from a keyboard?

This is a discussion on Interrupts from a keyboard? within the C++ Programming forums, part of the General Programming Boards category; Hi all, Is it possible to set up an interrupt so that the program stops and executes an interrupt function ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    5

    Interrupts from a keyboard?

    Hi all,

    Is it possible to set up an interrupt so that the program stops and executes an interrupt function whenever someone hits enter?

    I don't know much about interrupts, but everything I've seen always refers to hardware, and as far as I can tell, a keyboard is hardware... but no one ever seems to use them.

    (I also want to know how to set them up in C for microcontroller programming, which isn't made obvious anywhere that I can see, but the C and Cpp boards are seperate, so this is a bonus question...)

    Cheers!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The keyboard controller (which is part of the PC motherboard) will indeed issue interrupts - at least twice for every key that is pressed (once for the "key down", and again for the " key up" - some keys also have more than one key-code [an extension byte followed by the actual key code], which makes it 4 interrupts for extension byte, key down, extension byte and key up)

    It is a slightly different matter, however, to get that interrupt to directly interact with your application. In Windows or Linux, you'd certainly not do that with any great ease.

    There are ways to POLL if the keyboard has any data available, and if not, go on and do something else. Check the FAQ on "how do I read user input without having to hit enter" (or some title close to that). With some slight tweaks, you should be able to make that code do what you want.

    In all modern OS's, interrupts are handled by a device driver, which runs inside the kernel-space. User-code only gets to see what has been process.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,160
    Quote Originally Posted by Eldarion View Post
    I don't know much about interrupts, but everything I've seen always refers to hardware, and as far as I can tell, a keyboard is hardware... but no one ever seems to use them.
    They are used by the operating system, not user code.

    (I also want to know how to set them up in C for microcontroller programming, which isn't made obvious anywhere that I can see, but the C and Cpp boards are seperate, so this is a bonus question...)
    On a microcontroller where there is no "operating system," you receive interrupts directly. Usually, it's a simple as placing the address of your interrupt handler into a special location, then enabling the corresponding interrupts. The details are given in the manual for the microcontroller, and vary quite a bit.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    5
    Is there any way of looking in the buffer and seeing if any keys have been pressed, and then moving on? My problem with normal input functions is that they wait until there is some input before moving on, but my program doesn't know if there will be any input, and will want to be checking a timer, then checking to see if there is input, and so on until the timer runs out, or the user says to stop timing.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, it's possible.

    If you are using Windows, follow the example here:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    Code:
      if (WaitForSingleObject(stdIn, INFINITE) == WAIT_OBJECT_0)
      {
        DWORD num;
        ReadConsole(stdIn, &ch, 1, &num, NULL);
    
      }
    If you change INFINITE to, for example, 0, it will wait zero amount of time for the stdIn to have a character available.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hardware interrupts
    By Sentral in forum Tech Board
    Replies: 5
    Last Post: 02-23-2009, 05:46 PM
  2. Detecting keyboard and mouse in linux
    By sameer.nalwade in forum C Programming
    Replies: 3
    Last Post: 11-22-2008, 03:24 AM
  3. Keyboard port using other that a keyboard
    By antoinelac in forum C++ Programming
    Replies: 4
    Last Post: 06-12-2008, 02:46 PM
  4. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 09:00 AM
  5. Game Design Topic #2 - Keyboard or Mouse?
    By TechWins in forum Game Programming
    Replies: 4
    Last Post: 10-08-2002, 03:34 PM

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