samposs Posted August 28, 2016 Share Posted August 28, 2016 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! Link to comment Share on other sites More sharing options...
markyrocks Posted August 28, 2016 Share Posted August 28, 2016 (edited) HotKeySet("{SPACE}","_Run") while 1 WEnd Func _Run() ;......something to do here MsgBox("","","I ran") Exit endfunc ok space held down.... hmmmm expandcollapse popup#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 August 28, 2016 by markyrocks duh Spoiler "I Believe array math to be potentially fatal, I may be dying from array math poisoning" Link to comment Share on other sites More sharing options...
samposs Posted August 28, 2016 Author Share Posted August 28, 2016 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 expandcollapse popup#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? Link to comment Share on other sites More sharing options...
argumentum Posted August 28, 2016 Share Posted August 28, 2016 #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 Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting. Link to comment Share on other sites More sharing options...
markyrocks Posted August 28, 2016 Share Posted August 28, 2016 (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 August 28, 2016 by markyrocks Spoiler "I Believe array math to be potentially fatal, I may be dying from array math poisoning" Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now