Executing a function only once when pressing a key

This is a discussion on Executing a function only once when pressing a key within the C Programming forums, part of the General Programming Boards category; I have a function that checks if a key is pressed using GetKeyState (), and writes it into a structure. ...

  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
    32,854
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 07:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  3. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  4. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 09:56 AM
  5. Registery with api?
    By ismael86 in forum Windows Programming
    Replies: 1
    Last Post: 05-14-2002, 11:28 AM

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