Jump to content
Sign in to follow this  
XLimas

How long key is pressed?

Recommended Posts

XLimas

Does anyone have any idea how to make a script which will check what key is pressed and how long it's pressed?

Share this post


Link to post
Share on other sites
omikron48

Not sure, I think checking for all key presses is tantamount to keylogging, or at least one step from it.

You can check for specific keys using _IsPressed though. And I think the timer functions are what you need to check for press duration. Not sure since I've never needed to use timers before.

Share this post


Link to post
Share on other sites
Yashied

#Include <Misc.au3>

Global $Pressed = False

While 1
    Sleep(1)
    If _IsPressed('1B') Then
        If $Pressed Then
            If TimerDiff($Timer) > 5000 Then
                Exit
            EndIf
        Else
            $Timer = TimerInit()
            $Pressed = 1
        EndIf
    Else
        If $Pressed Then
            ConsoleWrite(Round(TimerDiff($Timer) / 1000, 2) & ' seconds' & @CR)
            $Pressed = 0
        EndIf
    EndIf
WEnd

Share this post


Link to post
Share on other sites
Tvern

Alternatively you could do something like:

Global $timer, $diff, $key
GUIRegisterMsg($WM_KEYDOWN,_keydown)
GUIRegisterMsg($WM_KEYUP,_keyup)

Func _keydown($hWndGUI, $MsgID, $WParam, $LParam)
   $timer = TimerInit()
   $key = $WParam
EndFunc

Func _keyup($hWndGUI, $MsgID, $WParam, $LParam)
    If $key = $WParam Then
       $diff = TimerDiff($Timer)
   Endif
EndFunc

(not sure if $WParam is the one that reports the key pressed) You could have it keep track of multiple keys by making $key an array instead.

If you want the key to register outside of your GUI then I can only think of registerring every key as hotkey, although I know there are better ways.

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.