Jump to content

Detect how long key was pressed?


Recommended Posts

Ok i want to make a script that will write a script for me. It will just use the Left, Right, Up, Down arrows. But lets say i push left down for 5 seconds then lift it, i want it to produce:

Send("{Left Down}")
Sleep(5000)
Send("{Left Up}")

Also lets say i hold the Up arrow down then 1 second later push the right key down for 10 seconds then release both it will produce:

Send("{Up Down}")
Sleep(1000)
Send("{Right Down}")
Sleep(10000)
Send("{Right Up}")
Send("{Up Up}")

Can anyone help me on such a script?

Link to comment
Share on other sites

I'm not sure if you're talking about a keylogger or a macro creator so....

Keylogger: Use the forum search. This topic has apparently been covered and shouldn't be covered anymore here as it gives Autoit a bad name.

Macro Creator: Apparently there is one packaged with SciTe.

Either case, you may want to check out _IsPressed in the UDFs of the Beta help file.

Link to comment
Share on other sites

I'm not sure if you're talking about a keylogger or a macro creator so....

Keylogger: Use the forum search. This topic has apparently been covered and shouldn't be covered anymore here as it gives Autoit a bad name.

Macro Creator: Apparently there is one packaged with SciTe.

Either case, you may want to check out _IsPressed in the UDFs of the Beta help file.

.... no... SciTe wont work cuz it doesnt count the wait times... It just crams all the key pushes in one line send line...

And no this is not a key logger, im making a bot for a game and i hate to manually do it.

Edited by blizzedout
Link to comment
Share on other sites

  • Moderators

Oxin8 is on the right path:

While 1
Local $DownArrow = ''
If _IsPressed('28') Then
    Local $Time = TimerInit()
    Do
    ;;
    Until Not _IsPressed('28')
    $DownArrow = "Send('{" & "DOWN}')" & @CRLF & 'Sleep(' & Round(TimerDiff($Time)) & ')' & @CRLF & "Send('{" & "UP}')"
EndIf
    If $DownArrow <> '' Then MsgBox(0, 'Test', $DownArrow)
WEnd


    
Func _IsPressed($s_hexKey, $v_dll = 'user32.dll')
    Local $a_R = DllCall($v_dll, "int", "GetAsyncKeyState", "int", '0x' & $s_hexKey)
    If Not @error And BitAND($a_R[0], 0x8000) = 0x8000 Then Return 1
    Return 0
EndFunc

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Oxin8 is on the right path:

While 1
Local $DownArrow = ''
If _IsPressed('28') Then
    Local $Time = TimerInit()
    Do
;;
    Until Not _IsPressed('28')
    $DownArrow = "Send('{" & "DOWN}')" & @CRLF & 'Sleep(' & Round(TimerDiff($Time)) & ')' & @CRLF & "Send('{" & "UP}')"
EndIf
    If $DownArrow <> '' Then MsgBox(0, 'Test', $DownArrow)
WEnd
    
Func _IsPressed($s_hexKey, $v_dll = 'user32.dll')
    Local $a_R = DllCall($v_dll, "int", "GetAsyncKeyState", "int", '0x' & $s_hexKey)
    If Not @error And BitAND($a_R[0], 0x8000) = 0x8000 Then Return 1
    Return 0
EndFunc
Thanks for starting me off on the right foot :lmao:

I really thing Scite should fix their AU3Record so it does sleep times, with not just the keyboard but also mouse clicks.

Link to comment
Share on other sites

  • Moderators

Thanks for starting me off on the right foot :lmao:

I really thing Scite should fix their AU3Record so it does sleep times, with not just the keyboard but also mouse clicks.

They've done alot... More so for a 'Free' language than anything else I can think of...

You might want to replace 'Round' with 'Int'... Just a thought.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Moderators

Follow the example and look at _IsPressed() in the Beta help file for the other keys.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Follow the example and look at _IsPressed() in the Beta help file for the other keys.

Ok well i still cant think of a way to interlace them all, like i have this right now:

$file = FileOpen("test.txt", 1)
While 1
; When the right arrow is pressed
    Local $RightArrow = ''
    If _IsPressed('27') Then
        Local $Time = TimerInit()
        Do
;;
        Until Not _IsPressed('27')
        $RightArrow = "Send('{" & "Right Down}')" & @CRLF & 'Sleep(' & Int(TimerDiff($Time)) & ')' & @CRLF & "Send('{" & "Right Up}')"
    EndIf
    If $RightArrow <> '' Then
        FileWriteLine($file, $RightArrow)
    EndIf

; When the up arrow is pressed
    Local $UpArrow = ''
    If _IsPressed('26') Then
        Local $Time = TimerInit()
        Do
;;
        Until Not _IsPressed('26')
        $UpArrow = "Send('{" & "Up Down}')" & @CRLF & 'Sleep(' & Int(TimerDiff($Time)) & ')' & @CRLF & "Send('{" & "Up Up}')"
    EndIf
    If $UpArrow  <> '' Then
        FileWriteLine($file, $UpArrow )
    EndIf
    
; When the left arrow is pressed
    Local $LeftArrow = ''
    If _IsPressed('25') Then
        Local $Time = TimerInit()
        Do
;;
        Until Not _IsPressed('25')
        $LeftArrow = "Send('{" & "Left Down}')" & @CRLF & 'Sleep(' & Int(TimerDiff($Time)) & ')' & @CRLF & "Send('{" & "Left Up}')"
    EndIf
    If $LeftArrow  <> '' Then
        FileWriteLine($file, $LeftArrow )
    EndIf
WEnd



Func _IsPressed($s_hexKey, $v_dll = 'user32.dll')
    Local $a_R = DllCall($v_dll, "int", "GetAsyncKeyState", "int", '0x' & $s_hexKey)
    If Not @error And BitAND($a_R[0], 0x8000) = 0x8000 Then Return 1
    Return 0
EndFunc;==>_IsPressed

But when press multiple arrows i want it to produce:

Send("{Up Down}")
Sleep(1000)
Send("{Right Down}")
Sleep(10000)
Send("{Right Up}")
Send("{Up Up}")

Not:

Send("{Up Down}")
Sleep(1000)
Send("{Up Up}")
Send("{Right Down}")
Sleep(10000)
Send("{Right Up}")
Edited by blizzedout
Link to comment
Share on other sites

Ok check this out, this will work with a combo if arrows but first the right arrow needs to be pressed down while others are pushed.

Ill be making it for all arrows tho.

This will be useful for ingame things like world of warcraft scripts like moving around and stuff.

$file = FileOpen("test.txt", 1)
$Right = 0
$Up = 0
$Left = 0

While 1
; When the right arrow is pressed
    If _IsPressed('27') Then
        FileWriteLine($file, 'Send("{Right Down}")')
        Local $RightArrowTime = TimerInit()
        Do
            If _IsPressed('26') Then
                FileWriteLine($file, 'Send("{Up Down}")')
                Local $UpArrowTime = TimerInit()
                Do
                ;;
                Until Not _IsPressed('26')
                If Not _IsPressed('26') Then
                    $Up = Int(TimerDiff($UpArrowTime))
                    FileWriteLine($file, '' & 'Sleep(' & Int(TimerDiff($UpArrowTime)) & ')')
                    FileWriteLine($file, 'Send("{Up Up}")')
                EndIf
            EndIf
            If _IsPressed('25') Then
                FileWriteLine($file, 'Send("{Left Down}")')
                Local $LeftArrowTime = TimerInit()
                Do
                ;;
                Until Not _IsPressed('25')
                If Not _IsPressed('25') Then
                        $Left = Int(TimerDiff($LeftArrowTime))
                    FileWriteLine($file, '' & 'Sleep(' & Int(TimerDiff($LeftArrowTime)) & ')')
                    FileWriteLine($file, 'Send("{Left Up}")')
                EndIf
            EndIf
        Until Not _IsPressed('27')
        If Not _IsPressed('27') Then
                $Right = Int(TimerDiff($RightArrowTime))
                
                $RightTime = $Right - $Left - $Up

            FileWriteLine($file, '' & 'Sleep(' & $RightTime & ')')
            FileWriteLine($file, 'Send("{Right Up}")')
        EndIf
    EndIf
WEnd



Func _IsPressed($s_hexKey, $v_dll = 'user32.dll')
    Local $a_R = DllCall($v_dll, "int", "GetAsyncKeyState", "int", '0x' & $s_hexKey)
    If Not @error And BitAND($a_R[0], 0x8000) = 0x8000 Then Return 1
    Return 0
EndFunc  ;==>_IsPressed
Link to comment
Share on other sites

Ok here is my code:

$file = FileOpen("testing.au3", 1)
$Right = 0
$Up = 0
$Left = 0

While 1
; When the Up arrow is pressed
    If _IsPressed('26') Then
        FileWriteLine($file, 'Send("{Up Down}")')
        Local $UpArrowTime = TimerInit()
        Do
            If _IsPressed('27') Then
                FileWriteLine($file, 'Send("{Right Down}")')
                Local $RightArrowTime = TimerInit()
                Do
                    If _IsPressed('25') Then
                        FileWriteLine($file, 'Send("{Left Down}")')
                        Local $LeftArrowTime = TimerInit()
                        Do
                        Until Not _IsPressed('25')
                        If Not _IsPressed('25') Then
                            $Left = Int(TimerDiff($LeftArrowTime))
                            FileWriteLine($file, '' & 'Sleep(' & Int(TimerDiff($LeftArrowTime)) & ')')
                            FileWriteLine($file, 'Send("{Left Up}")')
                        EndIf
                    EndIf
                Until Not _IsPressed('27')
                If Not _IsPressed('27') Then
                    $Right = Int(TimerDiff($RightArrowTime))
                    FileWriteLine($file, '' & 'Sleep(' & Int(TimerDiff($RightArrowTime)) & ')')
                    FileWriteLine($file, 'Send("{Right Up}")')
                EndIf
            EndIf
            If _IsPressed('25') Then
                FileWriteLine($file, 'Send("{Left Down}")')
                Local $LeftArrowTime = TimerInit()
                Do
                    If _IsPressed('27') Then
                        FileWriteLine($file, 'Send("{Right Down}")')
                        Local $RightArrowTime = TimerInit()
                        Do
                    ;;
                        Until Not _IsPressed('27')
                        If Not _IsPressed('27') Then
                            FileWriteLine($file, '' & 'Sleep(' & Int(TimerDiff($RightArrowTime)) & ')')
                            FileWriteLine($file, 'Send("{Right Up}")')
                        EndIf
                    EndIf
                Until Not _IsPressed('25')
                If Not _IsPressed('25') Then
                    $Left = Int(TimerDiff($LeftArrowTime))
                    FileWriteLine($file, '' & 'Sleep(' & Int(TimerDiff($LeftArrowTime)) & ')')
                    FileWriteLine($file, 'Send("{Left Up}")')
                EndIf
            EndIf
        Until Not _IsPressed('26')
        If Not _IsPressed('26') Then
            $Up = Int(TimerDiff($UpArrowTime))
            $UpTime = $Up - $Left - $Right
            FileWriteLine($file, '' & 'Sleep(' & $UpTime & ')')
            FileWriteLine($file, 'Send("{Up Up}")')
        EndIf
    EndIf
    Sleep(100)
    

Wend
Func _IsPressed($s_hexKey, $v_dll = 'user32.dll')
    Local $a_R = DllCall($v_dll, "int", "GetAsyncKeyState", "int", '0x' & $s_hexKey)
    If Not @error And BitAND($a_R[0], 0x8000) = 0x8000 Then Return 1
    Return 0
EndFunc ;==>_IsPressed

It works fine, but when i hold down the UP arrow then push the right arrow and let go of the right then push the right again the sleep time is off, any way to make it right.

What i use to get that Up release sleep time is $UpTime = $Up - $Left - $Right

Link to comment
Share on other sites

  • Moderators

In your

Do
                        Until Not _IsPressed('25'); or '27' or whatever

Statement, you'll have use an 'If/Then' statement, killing the initial timer for whatever you set... so If Not _IsPressed() Then $TimerKilled = Int(TimerInit($WhateverINamedTheTimer)), this won't affect the key that is being pressed at the time... so keep in mind you'll need a few different timers.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

In your

Do
                        Until Not _IsPressed('25'); or '27' or whatever

Statement, you'll have use an 'If/Then' statement, killing the initial timer for whatever you set... so If Not _IsPressed() Then $TimerKilled = Int(TimerInit($WhateverINamedTheTimer)), this won't affect the key that is being pressed at the time... so keep in mind you'll need a few different timers.

But that wouldnt add the first time to the sum.

Link to comment
Share on other sites

  • Moderators

But that wouldnt add the first time to the sum.

Your not serious are you?

First Variable + Second Variable = Total Sum

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...