Jump to content

how to start/stop a function use the same key(about HotKey.au3)


waynew
 Share

Recommended Posts

#Include <HotKey.au3>

Global Const $VK_ESCAPE = 0x1B
Global Const $VK_F12 = 0x7B
Global $testFlg

_HotKeyAssign($VK_F12, 'test', $HK_FLAG_DEFAULT)

; Assign "CTRL-ESC" with Quit()
_HotKeyAssign(BitOR($CK_CONTROL, $VK_ESCAPE), 'Quit')

While 1
    Sleep(10)
WEnd

Func Quit()
    Exit
EndFunc   ;==>Quit


Func test()
    $testFlg = Not $testFlg
    If Not $testFlg Then Beep()
    Local $io = 0
    While $testFlg
        Sleep(1000)
        $io += 1
        ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $io = ' & $io & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
    WEnd
EndFunc

i want to start or stop the function "test" use the same key ,but it seemed don't work,what's wrong.

HotKey.au3

Edited by waynew
Link to comment
Share on other sites

  • Developers

Ensure the func test() ends to be able to fire it again with the same HotKey and do the testing for $testFlg in the mainloop.

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

@waynew

Your code design is wrong. You should not stop the interrupting function - Test(). Here's one way to fix this.

#Include <HotKey.au3>

Global Const $VK_ESCAPE = 0x1B
Global Const $VK_F12 = 0x7B

Global $testFlg = False, $io

_HotKeyAssign($VK_F12, 'Test', $HK_FLAG_NOOVERLAPCALL)
_HotKeyAssign(BitOR($CK_CONTROL, $VK_ESCAPE), 'Quit')

While 1
    Sleep(10)
WEnd

Func Quit()
    Exit
EndFunc   ;==>Quit

Func Test()
    $testFlg = Not $testFlg
    If $testFlg Then
        $io = 0
        AdlibRegister('Debug', 1000)
    Else
        AdlibUnRegister('Debug')
        Beep()
    EndIf
EndFunc   ;==>Test

Func Debug()
    $io += 1
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $io = ' & $io & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
EndFunc   ;==>Debug
Link to comment
Share on other sites

from the helpfile

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

Global $Paused
HotKeySet("{PAUSE}", "TogglePause")
HotKeySet("{ESC}", "Terminate")
HotKeySet("+!d", "ShowMessage")  ;Shift-Alt-d

;;;; 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 Terminate()
    Exit 0
EndFunc

Valik Note Added 19 October 2006 - 08:38 AMAdded to warn level I just plain don't like you.

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