Getting input. Too many times

This is a discussion on Getting input. Too many times within the Windows Programming forums, part of the Platform Specific Boards category; Hi again. I have created a program and part of it is to see how many times the user presses ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    76

    Getting input. Too many times

    Hi again. I have created a program and part of it is to see how many times the user presses a certain key to check keys I am using the following function.
    Code:
    inline bool keyDown(int vkCode)
    {
    	if (GetAsyncKeyState(vkCode) & 0x8000)
    		return 1;
    	else
    		return 0;
    }
    My problem is simple I have locked my program down to 30 frames a second the trouble with this is, that the user holds the actual key down more than a 30th of a second so the counter counts that they have pressed the key more than once.
    Now to stop this I tried slowing down the program but i had to slow it down to 5 frames per second before it stopped doing it. Now as you can imagine a program running at 5 frames per second is just far to slow. I was wondering if there was another way to tackle this problem.

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Maybe I'm wrong but if you had a main function in your program that was called each time the main loop runs, you could put code inside that function to keep it at 30fps (using GetTickCount() in some way, or even Sleep() ) then have you keyDown function called within that loop, shouldn't it only detect a keypress once per loop, which would be locked at 30fps?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    76
    Yeah but because the code loops 30 tiems a second. Say if the key is pressed for one of those loops then it gets added to the counter but say its still pressed for the next loop well Then it still qualifys and it adds another 1 to the counter. So if the key is held down for more than 30 mili seconds then it counts as 2 instead of 1. Thats the problem

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Ah I see what you mean...can't you detect keyup as well? I think all you have to do is return 0 where you return 1 in keydown function and return 1 where you return 0...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input class project (again)
    By Elysia in forum C++ Programming
    Replies: 41
    Last Post: 02-13-2009, 09:52 AM
  2. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 01:20 PM
  3. Trouble with a lab
    By michael- in forum C Programming
    Replies: 18
    Last Post: 12-06-2005, 10:28 PM
  4. invalid input
    By sworc66 in forum C Programming
    Replies: 8
    Last Post: 11-11-2003, 04:41 AM
  5. input files in C
    By LWisne in forum C Programming
    Replies: 4
    Last Post: 09-30-2002, 06:24 AM

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