Jump to content

Simple Hold Button X Seconds incl. Hotkey


Recommended Posts

If I twere to do it I might have a loop that check for a condition with say a Switch...Case... statement, have each HotKey combo set the variable being to checked to a value and have a loop to perform each desired action.  This is simpler, try this on for size.

Global $action = 0
Global $timer = 0
HotKeySet("^1","_RunAction1")  ;Ctrl + 1
HotKeySet("^2","_RunAction2")  ;Ctrl + 2
HotKeySet("{esc}","_Exit")

While 1
    $timer = 0
    sleep(100)
WEnd

Func _RunAction1()
   $action = 1
   While 1
    ToolTip("Running Action number 1")
     $timer = TimerInit()
    Do
        sleep(100)
        If $action <> 1 Then Return 0
    Until TimerDiff($timer) >= 2000

    $timer = TimerInit()
    ToolTip("Stopping Action number 1")
    Do
        sleep(100)
        If $action <> 1 Then Return 0
    Until TimerDiff($timer) >= 2000
   WEnd
EndFunc

Func _RunAction2()
   $action = 2
   While 1
    $timer = TimerInit()
    ToolTip("And here is Action number 2")
    Do
        sleep(100)
        If $action <> 2 Then Return 0
    Until TimerDiff($timer) >= 3000
 
    $timer = TimerInit()
    ToolTip("Now stopping Action number 2")
    Do
        sleep(100)
        If $action <> 2 Then Return 0
    Until TimerDiff($timer) >= 3000
   WEnd
EndFunc

Func _Exit()
   Exit
EndFunc
Edited by spudw2k
Link to comment
Share on other sites

I tried to explain how Exit's code works.  Let me try again with some more detail that may be helpful.  He has created a couple of hotkeys that both call the same function.  That function has a switch.  You should look up switch in the help file if you are not familiar with them.  It conditionally runs statements but instead of using "IF" it uses "Case".  The cases it is testing for in this example are which hotkey was pressed (this is done with the macro @HotKeyPressed).  Depending on which key is pressed it will either send the "y" key for 5 seconds (you can change this to 20 if that is your requirement) or it will exit.  CTRL+Y will start the timer and the while loop that will run while the difference in time is less than 5 seconds (i.e. it will run for 5 seconds).  He uses Notepad simply to demonstrate the whole thing working (you should run it if you have not already done so to see it working).  The "ControlSend lines are just sending text to notepad as part of the example.  They use the handle returned from when he opened notepad to write back to it.

At this point I see two solutions now offered that accomplish your objective.  You said you "raped Google" in your OP to try and figure this out.  I would suggest applying that same effort to trying to understand the examples provided.  If you don't understand a certain line or function start with the help file and then ask some specific questions if you can't figure it out.

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Link to comment
Share on other sites

  • 4 years later...

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...