Thread: Interrupt Question

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    1

    Interrupt Question

    I'm just messing around with a program right now. I want to set up the program in a while loop so it will continually run, but I want it to stop when I press a certain key. How do I set up an interrupt to do this?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    State your OS and Compiler.

    See the FAQ entry on reading key presses without waiting for a newline.
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    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.

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    How do I set up an interrupt to do this?
    In XP, you don't

    In DOS, you hook the keyboard interrupt, but this would work just fine.


    Code:
    while (!kbhit())
    {
      ..run your loop
    }
    I don't know if it works in console b/c frankly I've never coded console programs. I went from DOS to DX to MFC.
    Last edited by VirtualAce; 08-27-2006 at 12:39 PM.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    kbhit() works in my somewhat old version of Dev-C++; it's in <conio.h>.

    [edit] If you want the loop to end only if a certain key is pressed (and not just any key), you might try something like this:
    Code:
    while(!kbhit() || getche() != ' ') {
    
    }
    [/edit]
    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.

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I checked MSVC 2005 and kbhit() is in there. Originally I thought it was only in dos.h but I was wrong.

  6. #6
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    you could look into signals maybe in linux. They might be useful.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Signals for keyboard? Ok....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  2. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  4. interrupt handler functions in Visual C++ 6.0
    By scromer in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 01-07-2002, 07:06 PM
  5. Sound card interrupt problems
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 12-05-2001, 04:38 AM