Jump to content

Newbie Help: Simple wait for key event


Recommended Posts

Hi, how do I make this script wait for the letter "q" and then execute the associated function?

; Press Esc to terminate script, Pause/Break to "pause"

Global $Paused
HotKeySet("{PAUSE}", "TogglePause")
HotKeySet("{ESC}", "Terminate")

;;;; Body of program would go here;;;;
While 1
    Sleep(100)
   ; wait for keyevent "q" then run q_func
WEnd
;;;;;;;;

Func TogglePause()
    $Paused = NOT $Paused
    While $Paused
        sleep(100)
        ToolTip('Script is "Paused"',0,0)
    WEnd
    ToolTip("")
EndFunc

Func q_func()
    Do something
EndFunc

Func Terminate()
    Exit 0
EndFunc
Link to comment
Share on other sites

The reason why I'm thinking about capturing the keyevent in the *body is so that I can toggle it on/off by pressing the PAUSE|BREAK key instead of exiting the script everytime I want to disable hotkeys.

In the code that I posted earlier there are two loops, one loop I want to script to wait for keyevents and a pause loop that basically does nothing and acts as a toggle off switch. If I put the HotKeySet statement out of these loops, the hotkeys will always be active unless I exit the script.

Would it be possible to setup a hotkey to assign a variable upon pressing a key? and in the *body do a SELECT...CASE statement. ie:

HotKeySet("{q}", "q_assign")

func q_assign()
  $variable = "q"
end func

; *body
While 1
    Sleep(100)
    SELECT CASE $variable
    CASE "q"
      q_func
    CASE "t"
      t_func
    END SELECT
WEnd

func q_func()
 ; do something here
  $variable = empty
end func
Link to comment
Share on other sites

Would it be possible to setup a hotkey to assign a variable upon pressing a key? and in the *body do a SELECT...CASE statement. ie:

HotKeySet("{q}", "q_assign")

func q_assign()
  $variable = "q"
end func

; *body
While 1
    Sleep(100)
    SELECT CASE $variable
    CASE "q"
      q_func
    CASE "t"
      t_func
    END SELECT
WEnd

func q_func()
; do something here
  $variable = empty
end func

<{POST_SNAPBACK}>

excellent effort....... this is what you want

$var = ""


HotKeySet("{q}", "q_func")



While 1
    Sleep(100)
    
    If $var = 1 Then
    ;do your thing
    EndIf
    
WEnd

func q_func()
    
    If $var = "" Then
        $var = 1
        Return
    EndIf
    
    If $var = 1 Then
        $var = ""
        Return
    EndIf
    
EndFunc

hope it helps

8)

NEWHeader1.png

Link to comment
Share on other sites

Thanks for the help but after some experimenting I found out that the pause function doesn't really work the way I want it to. I thought that once the TogglePause() was running, the other HotKeys were disabled.

I think it would be better to create a $toggle variable so I can use IF...ENDIF to control hotkeys directly. Something like this:

$toggle = ""
$q_var = ""

HotKeySet("{PAUSE}", "hotkeys_toggle")
HotKeySet("{q}", "q_toggle")

; *body
While 1
    Sleep(100)
    If $toggle = 1 Then
      If q_var = 1 Then
        q_func
        q_toggle; trigger off
      EndIf
    Else
      ToolTip('Hotkeys are disabled',0,0)
    EndIf
WEnd

func hotkeys_toggle()

    If $toggle= "" Then
        $toggle= 1
        Return
    EndIf

    If $toggle= 1 Then
        $toggle= ""
        Return
    EndIf

endfunc

func q_toggle()
 
  If $toggle = 1 Then
      If $q_var = "" Then
          $q_var = 1
          Return
      EndIf
  EndIf

  If $toggle = 1 Then
      If $q_var = 1 Then
          $q_var = ""
          Return
      EndIf
  EndIf

EndFunc

func q_func()
 ;write a function here
endfunc

So when this script is executed, hotkeys are disabled by default. Basically, everything revolves around $toggle in order to run. Hopefully this should work when I try it out in real world apps...

Link to comment
Share on other sites

or maybe this

Func TogglePause()
    $Paused = NOT $Paused
    While $Paused
        HotKeySet("{PAUSE}", "")
        HotKeySet("{q}", "")
        sleep(100)
        ToolTip('Script is "Paused"',0,0)
    WEnd
    HotKeySet("{PAUSE}", "hotkeys_toggle")
    HotKeySet("{q}", "q_toggle")
    ToolTip("")
EndFunc

?????

8)

NEWHeader1.png

Link to comment
Share on other sites

:evil: Ah yes! Make the scope of the hotkeyset settings within the pause function, it seems more elegant this way. So only the HotKeySet("{PAUSE}", "toggle_pause") code would lie outside of the scope of the main body and sub functions to act as the main toggle switch. Great thinking! :)
Link to comment
Share on other sites

:evil: Ah yes! Make the scope of the hotkeyset settings within the pause function, it seems more elegant this way. So only the HotKeySet("{PAUSE}", "toggle_pause") code would lie outside of the scope of the main body and sub functions to act as the main toggle switch. Great thinking!  :)

<{POST_SNAPBACK}>

Glad I could help

8)

NEWHeader1.png

Link to comment
Share on other sites

Im a noob at using .au3 and have never used any other programs like this, but ive wanted to add my help somewhere and i think i can here, but if im wrong please correct me :)

;Press Esc to terminate script, Pause/Break to "pause"

HotKeySet("{q}", "q_func")
HotKeySet("{PAUSE}", "TogglePause")
HotKeySet("{ESC}", "Terminate")

;;;; Body of program would go here;;;;
While 1
    Sleep(100)
WEnd
;;;;;;;;

Func TogglePause()
    $Paused = NOT $Paused
    While $Paused
        sleep(100)
        ToolTip('Script is "Paused"',0,0)
    WEnd
    ToolTip("")
EndFunc

Func q_func()
    Do something
EndFunc

Func Terminate()
    Exit 0
EndFunc

[quote]I don't like to think, Therefore I ChEaT[/quote]

Link to comment
Share on other sites

lineage,

As I said in an earlier post, I thought the pause function would disable all other hotkeys but it doesn't. If you press {PAUSE} and then {q}, q_func will still be executed. Likewise, {ESC} will terminate the script when the script is paused so {q} should not be at the same scope level of {ESC}.

Valuater solved my problem by resetting the functions to "" under paused mode and in a sense disables the hotkeys since there are no functions to call. Upon unpausing, the hotkeys are initialized back to the functions that I want to trigger.

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