samposs

Only run when space held down

5 posts in this topic

Hello I would like to know how to make it so a script will only run when a specific key is held down (in my specific case the space button). Any help is much appreciated!

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

HotKeySet("{SPACE}","_Run")
while 1

    WEnd
Func _Run()

    ;......something to do here
    MsgBox("","","I ran")
Exit
endfunc

ok space held down.... hmmmm 

#include <Misc.au3>
;HotKeySet("{SPACE}","_Run")
$sHexKey=20


while 1
$pressed=_IsPressed($sHexKey)

if $pressed=1 then
    $timer=TimerInit()
EndIf

while $pressed=1
$pressed=_IsPressed($sHexKey)



if $timer<>0 and $pressed=1 Then
    $diff=TimerDiff($timer)
    if $diff > 1000 Then
        _Run()
        ExitLoop
   elseif $pressed<>1 then 
        exitloop
    Else
        sleep(100)
        EndIf
        EndIf
    WEnd

WEnd





Func _Run()

    ;......something to do here
    MsgBox("","","I ran")
Exit
    EndFunc

 

Edited by markyrocks
duh

Share this post


Link to post
Share on other sites
1 hour ago, markyrocks said:
HotKeySet("{SPACE}","_Run")
while 1

    WEnd
Func _Run()

    ;......something to do here
    MsgBox("","","I ran")
Exit
endfunc

ok space held down.... hmmmm 

#include <Misc.au3>
;HotKeySet("{SPACE}","_Run")
$sHexKey=20


while 1
$pressed=_IsPressed($sHexKey)

if $pressed=1 then
    $timer=TimerInit()
EndIf

while $pressed=1
$pressed=_IsPressed($sHexKey)



if $timer<>0 and $pressed=1 Then
    $diff=TimerDiff($timer)
    if $diff > 1000 Then
        _Run()
        ExitLoop
   elseif $pressed<>1 then 
        exitloop
    Else
        sleep(100)
        EndIf
        EndIf
    WEnd

WEnd





Func _Run()

    ;......something to do here
    MsgBox("","","I ran")
Exit
    EndFunc

 

thanks for the reply! Hmmm it starts the script but when i release space the script doesnt stop, any ideas?

Share this post


Link to post
Share on other sites
#include <Misc.au3>
Local $hDLL = DllOpen("user32.dll")
While Not _IsPressed("1B",$hDLL) ; esc key to end this demo
    If _IsPressed("20",$hDLL) Then ; space key to whatever
        ToolTip("the time is "&@HOUR&":"&@MIN&":"&@SEC&"."&@MSEC)
        Sleep(10)
        ToolTip("")
    EndIf
WEnd

 

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

24 minutes ago, samposs said:

thanks for the reply! Hmmm it starts the script but when i release space the script doesnt stop, any ideas?

its doing that bc its just waiting for the next time that you need to press and hold it to run again... if you want it to end after one press then add and exit in there where it needs to exit

 

edit

what i posted seems to work perfectly.  i actually removed the "exit" from the run function and it works as intended over and over.  So maybe whatever your executing doesn't properly terminate itself at the end, or its not returning to the start of the script.  

Edited by markyrocks

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