Sign in to follow this  
Followers 0
MattyGibbard

Check to see if a key is held down

5 posts in this topic

Hi there. I am making a game at the moment, which is turning out pretty well. I'm going along nicely untill I hit a problem. I found that if the player holds down the arrow key, he moves really fast. I was single press movement. At the moment, I'm using a function.

Is there a UDF or a function that checks to see if a key has been held down for x ammount of time?

Thank you for your time.

Share this post


Link to post
Share on other sites



#include <Misc.au3>

$dll = DllOpen("user32.dll")

While 1
    Sleep(0)
    If _IsPressed("28", $dll) Then; Arrow Down
        $timer = TimerInit()
        while _IsPressed("28", $dll)
            sleep(0)
        WEnd
        ConsoleWrite("Arrow Down held for " & TimerDiff($timer) & " ms" & @crlf)
    EndIf

    If _IsPressed("1B", $dll) Then; ESC
        MsgBox(0,"_IsPressed", "ESC Key Pressed")
        ExitLoop
    EndIf
WEnd
DllClose($dll)

Share this post


Link to post
Share on other sites

Cheers KaFu, that worked fine. Thanks for your time.

Share this post


Link to post
Share on other sites

_IsPressed only check to see if this key is currently down and not the least significant bit that tells if the key was pressed since the last call to GetAsyncKeyState. You can modify _IsPressed to do that for you or better write your own function to test for both and return, say, 2 if it was pressed previously or 1 if it's pressed or 0 otherwise.

Share this post


Link to post
Share on other sites

_IsPressed only check to see if this key is currently down and not the least significant bit that tells if the key was pressed since the last call to GetAsyncKeyState. You can modify _IsPressed to do that for you or better write your own function to test for both and return, say, 2 if it was pressed previously or 1 if it's pressed or 0 otherwise.

KaFu's script does work, but it pauses the players movement. It's exactly what I wanted, but I still want the player movement. Possibly using an AdLibEnable, I may be able to run the movement at the same time. I'll let you know how I did it once I have finally worked it out. Also, expect the source for my game up soon.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0