Thread: Executing a function only once when pressing a key

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    61

    Executing a function only once when pressing a key

    I have a function that checks if a key is pressed using GetKeyState (), and writes it into a structure.
    I also have a function which checks that structure, and starts a function depending on which key-combination you press.
    Both functions are located in the main loop.

    For most of the actions, the function has to be called all the time while the keys are pressed (like when you press the upArrow key to move in a FPS)

    But for some actions it is necessary that the function is only called once when the key-combination is pressed. (like when you shoot a gun in a FPS, you don't want to fire 60 bullets per second (depending on your framerate) )

    So far I haven't found a solution to do this yet.
    It would be best if the windowProcedure isn't required to do this since it gives problems in fullscreen mode.

    Thanks in advance.

  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
    You need to track the up/down state of each key as well.

    For example, moving happens while the key is pressed.

    Shooting happens once for each down/up transition.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  5. Registery with api?
    By ismael86 in forum Windows Programming
    Replies: 1
    Last Post: 05-14-2002, 10:28 AM