Jump to content

Breaking a loop


James
 Share

Recommended Posts

Hi,

Don't you find it really annoying when you accidentally press the insert button and end up typing over all that work you just spent hours on?

Well, I was trying to fix that exact problem! I made this program which beeps if you press the Insert button. There is a tray options which allows you to turn on or off the program. However, I can turn it on but not off. I have spent 30 mins working on this and can't work out why...

#include <Constants.au3>
Opt("TrayMenuMode",1)
; 45 = Insert

Global $_IPack = "icons.icl", $Yup = False

TraySetIcon($_IPack, 9); 9 = Red Off | 7 = Green On

$TurnOn = TrayCreateItem("Turn ITK On")
$TurnOff = TrayCreateItem("Turn ITK Off")
TrayCreateItem("")
; $Settings = TrayCreateItem("Settings")
$About = TrayCreateItem("About ITK")
$Exit = TrayCreateItem("Exit")
TraySetState()

While 1
    $msg = TrayGetMsg()
    Select
        Case $msg = $TurnOff
            ITK(0)
        Case $msg = $TurnOn
            ITK(1)
        Case $msg = 0
            ContinueLoop
        Case $msg = $Exit
            $Yup = False
            If $Yup = False Then ExitLoop
    EndSelect
WEnd

Func ITK($io_switch)
    Select
        Case $io_switch = 0
            TraySetIcon($_IPack, 9)
            $Yup = False
        Case $io_switch = 1
            TraySetIcon($_IPack, 7)
            $Yup = True
            While $Yup = True
                Do 
                    If _IsPressed(Hex("45")) Then Beep(500)
                Until $Yup = False
            WEnd
        Case Else
            MsgBox(32, @ScriptName, "Invalid command parameter!")
    EndSelect
EndFunc

Func _IsPressed($sHexKey, $vDLL = 'user32.dll')
    Local $a_R = DllCall($vDLL, "int", "GetAsyncKeyState", "int", '0x' & $sHexKey)
    If Not @error And BitAND($a_R[0], 0x8000) = 0x8000 Then Return 1
    Return 0
EndFunc  ;==>_IsPressed

I know it is that While Loop but I cannot break it!

Thanks,

James

Link to comment
Share on other sites

Set the loop like this

Global $Active = 0

to turn the loop on type $Active = 1

While $active

blah...

blah...

WENd

to stop it...put anywhere:

$Active = 0

Does that help?

So like this:

#include <Constants.au3>
Global $Active = 1
Opt("TrayMenuMode",1)
; 45 = Insert

Global $_IPack = "icons.icl", $Yup = False

TraySetIcon($_IPack, 9); 9 = Red Off | 7 = Green On

$TurnOn = TrayCreateItem("Turn ITK On")
$TurnOff = TrayCreateItem("Turn ITK Off")
TrayCreateItem("")
; $Settings = TrayCreateItem("Settings")
$About = TrayCreateItem("About ITK")
$Exit = TrayCreateItem("Exit")
TraySetState()

While $active
    $msg = TrayGetMsg()
    Select
        Case $msg = $TurnOff
            $active = 0
        Case $msg = $TurnOn
            $active = 1
        Case $msg = 0
            ContinueLoop
        Case $msg = $Exit
            $Yup = False
            If $Yup = False Then ExitLoop
    EndSelect
WEnd

Func ITK($io_switch)
    Select
        Case $io_switch = 0
            TraySetIcon($_IPack, 9)
            $Yup = False
        Case $io_switch = 1
            TraySetIcon($_IPack, 7)
            $Yup = True
            While $Yup = True
                Do
                    If _IsPressed(Hex("45")) Then Beep(500)
                Until $Yup = False
            WEnd
        Case Else
            MsgBox(32, @ScriptName, "Invalid command parameter!")
    EndSelect
EndFunc

Func _IsPressed($sHexKey, $vDLL = 'user32.dll')
    Local $a_R = DllCall($vDLL, "int", "GetAsyncKeyState", "int", '0x' & $sHexKey)
    If Not @error And BitAND($a_R[0], 0x8000) = 0x8000 Then Return 1
    Return 0
EndFunc;==>_IsPressed

:) at myself...got the wrong idea...so you wanna stop the ITK() loop?

Hmm...Make a While/Wend loop and use the While $Active and $Active = 0 and $Active = 1! that may help...I'll check in to it now...

Wow james...you have stumped me on this one!

Edited by Swift
Link to comment
Share on other sites

Here is whats happening:

You click "Turn ITK On" which sends the script into the Do...Until loop. Permanently. There is no way to break out of the loop because the TrayGetMsg() isn't running while stuck in said loop. You should switch to TrayOnEventMode.

Link to comment
Share on other sites

I tried this:

#include <Constants.au3>
Opt("TrayMenuMode", 1)
; 45 = Insert
Opt("TrayOnEventMode",1)

Global $_IPack = "icons.icl", $Yup = 0

TraySetIcon($_IPack, 9); 9 = Red Off | 7 = Green On

$TurnOn = TrayCreateItem("Turn ITK On")
TrayItemSetOnEvent(-1,"TrayOn")
$TurnOff = TrayCreateItem("Turn ITK Off")
TrayItemSetOnEvent(-1,"TrayOff")
TrayCreateItem("")
; $Settings = TrayCreateItem("Settings")
;$About = TrayCreateItem("About ITK")
$Exit = TrayCreateItem("Exit")
TrayItemSetOnEvent(-1,"MyExit")
TraySetState()

While 1
    Sleep(100)
WEnd

Func ITK($io_switch)
    Select
        Case $io_switch = 0
            $Yup = 0
            TraySetIcon($_IPack, 9)
        Case $io_switch = 1
            TraySetIcon($_IPack, 7)
            $Yup = 1 
            While $Yup
                If _IsPressed(Hex("45")) Then Beep(500)
            WEnd
        Case Else
            MsgBox(32, @ScriptName, "Invalid command parameter!")
    EndSelect
EndFunc  ;==>ITK

Func _IsPressed($sHexKey, $vDLL = 'user32.dll')
    Local $a_R = DllCall($vDLL, "int", "GetAsyncKeyState", "int", '0x' & $sHexKey)
    If Not @error And BitAND($a_R[0], 0x8000) = 0x8000 Then Return 1
    Return 0
EndFunc  ;==>_IsPressed

Func TrayOff()
    ITK(0)
EndFunc

Func TrayOn()
    ITK(1)
EndFunc

Func MyExit()
    Exit
EndFunc

Doesn't work though :)

Link to comment
Share on other sites

Hi,

Don't you find it really annoying when you accidentally press the insert button and end up typing over all that work you just spent hours on?

Well, I was trying to fix that exact problem! I made this program which beeps if you press the Insert button. There is a tray options which allows you to turn on or off the program. However, I can turn it on but not off. I have spent 30 mins working on this and can't work out why...

James

If _IsPressed(Hex("45")) Then 
                Beep(500,100)
                $yup = 0
                endif

?

Mads3n.dk
Link to comment
Share on other sites

Nothing there. No beep, however I can I turn ITK off and exit!

Woops:

#include <Constants.au3>
Opt("TrayMenuMode", 1)
; 45 = Insert
Opt("TrayOnEventMode", 1)

Global $_IPack = "icons.icl", $Yup = 0

TraySetIcon($_IPack, 9); 9 = Red Off | 7 = Green On

$TurnOn = TrayCreateItem("Turn ITK On")
TrayItemSetOnEvent(-1, "TrayOn")
$TurnOff = TrayCreateItem("Turn ITK Off")
TrayItemSetOnEvent(-1, "TrayOff")
TrayCreateItem("")
; $Settings = TrayCreateItem("Settings")
$About = TrayCreateItem("About ITK")
TrayItemSetOnEvent(-1, "About")
$Exit = TrayCreateItem("Exit")
TrayItemSetOnEvent(-1, "MyExit")
TraySetState()

While 1
    Sleep(100)
WEnd

Func ITK($io_switch)
    Select
        Case $io_switch = 0
            $Yup = 0
            TraySetIcon($_IPack, 9)
        Case $io_switch = 1
            TraySetIcon($_IPack, 7)
            $Yup = 1
            While $Yup = 1
            If _IsPressed(Hex("45")) Then
                Beep(500)
                $Yup = 0
            EndIf
            WEnd
        Case Else
            MsgBox(32, @ScriptName, "Invalid command parameter!")
    EndSelect
EndFunc  ;==>ITK

Func _IsPressed($sHexKey, $vDLL = 'user32.dll')
    Local $a_R = DllCall($vDLL, "int", "GetAsyncKeyState", "int", '0x' & $sHexKey)
    If Not @error And BitAND($a_R[0], 0x8000) = 0x8000 Then Return 1
    Return 0
EndFunc  ;==>_IsPressed

Func TrayOff()
    ITK(0)
EndFunc  ;==>TrayOff

Func TrayOn()
    ITK(1)
EndFunc  ;==>TrayOn

Func MyExit()
    Exit
EndFunc  ;==>MyExit

Func About()
    MsgBox(0, "Insert Hotkey", "ITK is a product of James-Brooks.net Copyright 2008.")
EndFunc  ;==>About

No, that only works once.

Edited by JamesB
Link to comment
Share on other sites

strange.. the script seems to work fine here

aah.. wait a sec, it doesnt.. i see.. ill see what i can do

edit:

how about this:

#include <Constants.au3>
Opt("TrayMenuMode",1)
; 45 = Insert

Global $_IPack = "icons.icl", $Yup = False

TraySetIcon($_IPack, 9); 9 = Red Off | 7 = Green On

$TurnOn = TrayCreateItem("Turn ITK On")
$TurnOff = TrayCreateItem("Turn ITK Off")
TrayCreateItem("")
; $Settings = TrayCreateItem("Settings")
$About = TrayCreateItem("About ITK")
$Exit = TrayCreateItem("Exit")
TraySetState()

While 1
    $msg = TrayGetMsg()
    Select
        Case $msg = $TurnOn
            TraySetIcon($_IPack, 7)
            $Yup = True
            While $Yup = True
                    If _IsPressed(Hex("45")) Then Beep(500,50)
                    $msg = TrayGetMsg()
                    Select
                    Case $msg = $TurnOff
                    ExitLoop
                    Case $msg = $Exit
                    $Yup = False
                    If $Yup = False Then Exit
                    EndSelect
            WEnd
        Case $msg = 0
            ContinueLoop
        Case $msg = $Exit
            $Yup = False
            If $Yup = False Then Exit
    EndSelect
WEnd

Func _IsPressed($sHexKey, $vDLL = 'user32.dll')
    Local $a_R = DllCall($vDLL, "int", "GetAsyncKeyState", "int", '0x' & $sHexKey)
    If Not @error And BitAND($a_R[0], 0x8000) = 0x8000 Then Return 1
    Return 0
EndFunc ;==>_IsPressed
Edited by mads3n
Mads3n.dk
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...