Thread: Keyboard input (help a novice!)

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    3

    Keyboard input (help a novice!)

    Hi. First post; so...hello.

    Also, I thought this might be a good place to resolve a question that's been bugging me since I began working with C++. I was told by all my references (mainly a high school teacher (that was actually a volleyball coach, but that's another story....)) that cin.get() and cin.ignore() were usefull as ways to hold the program for the enter key. The problem is that these tools seem to be outliving the usefullness I know them for. What I need is a function that continues the program once the enter key is pressed down, but will not barge through to the next pause if the key remains down. For example:

    Code:
        Astat[3]=+50;             cout<<"HP increases by 50,";cin.ignore();
        Astat[4]=+3;              cout<<"Strength increases by 3,";cin.ignore();
        Astat[5]=+4;              cout<<"Defence increases by 4,";cin.ignore();
    the preceding code will flash by in the blink of an eye if 'Enter' is held down. I have a notion that the problem is that if enter is held longer than what the keyboard's typematic buffer rate is set to in the bios, a battery of instances of 'Enter' being pressed follows and ruins said code. Is this what's happening? Clearly I'm in over my head, but the book gets so boring sometimes.... Also there's nothing about this in any of my books.

    Thanks,
    Tails

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You need platform specific code. The get() and ignore() functions are part of standard C++ that works on all platforms, but paying attention to things like whether the key is down but not back up is something that differs on different platforms.

    Tell us what OS (and version) and compiler (and version) you are using and somebody should be able to steer you in the right direction.

    If you just want to do regular console applications, though, you might consider just getting over that little nuisance. In most operating systems, pressing a key and holding it down actually sends multiple key presses to the application, so trying to get around that goes against what a user might expect.

    Finally, another solution might be to sleep execution for a small time between each output just to slow it down no matter what. Sleep is also platform-specific, although it is a little more common and information can be found with a simple search.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  4. #4
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Who wrote those tutorials?
    They use GETCH()??
    I don't like compiler-specific tutorials. In fact, I hate them.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I don't like compiler-specific tutorials. In fact, I hate them.
    Pay attention.
    Reading a key without waiting IS compiler specific, there is no choice in the matter.
    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.

  6. #6
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    It isn't when you choose to use platform specific functions
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3
    I'm going to poke around and see what I can come up with on sleep, thanks for the help.

    I'm using Windows XP pro, and Dev C++ 4.9.9.2.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Keyboard Input
    By CaliJoe in forum C++ Programming
    Replies: 3
    Last Post: 05-08-2009, 09:51 AM
  2. problem with keyboard input
    By fighter92 in forum Game Programming
    Replies: 6
    Last Post: 03-20-2009, 09:41 AM
  3. Keyboard input in a Dialog
    By ksarkar in forum C++ Programming
    Replies: 1
    Last Post: 05-20-2005, 05:39 AM
  4. Intercepting keyboard input
    By EvBladeRunnervE in forum Windows Programming
    Replies: 3
    Last Post: 01-08-2004, 09:03 AM
  5. Need help with Console Keyboard Input
    By pawelx2 in forum Game Programming
    Replies: 5
    Last Post: 05-30-2002, 11:03 PM