Docfxit

How to toggle a hotkey

10 posts in this topic

I have this script with two hotkeys.  The F4 hotkey works fine.  The F7 hotkey doesn't work.

#include <MsgBoxConstants.au3>
AutoItSetOption("TrayIconDebug", 1) ;0-off
; Set so that tray displays current line number

HotKeySet("{F4}", "ExitProg")
HotKeySet("{F7}", "StartStop")

$On = True

    While $On = True
    Local $iFreeSpace = DriveSpaceFree("E:" & "\") ; Find the free disk space of the home drive, generally this is the C:\ drive.
    If $iFreeSpace < 1000 Then ;Less than 1gb
        $megs = $iFreeSpace * .1024
        MsgBox($MB_SYSTEMMODAL, "", "Free Space: " & $megs & " MB")
        Run("C:\Batch\CleanTemp.bat")
    EndIf
        Sleep(7200000) ; Two hours
    WEnd

Func StartStop()
    If $On = False Then
        $On = True
    Else
        $On = False
    EndIf
EndFunc

Func ExitProg()
    Exit
EndFunc

How can I fix it so the F7 hotkey works?

Thank you,

Docfxit

Share this post


Link to post
Share on other sites



Perhaps something like this:

#include <MsgBoxConstants.au3>
AutoItSetOption("TrayIconDebug", 1) ;0-off
; Set so that tray displays current line number

HotKeySet("{F4}", "ExitProg")
HotKeySet("{F7}", "StartStop")

$On = True

While True
    If $On Then
        Local $iFreeSpace = DriveSpaceFree("E:" & "\") ; Find the free disk space of the home drive, generally this is the C:\ drive.
        If $iFreeSpace < 1000 Then ;Less than 1gb
            $megs = $iFreeSpace * .1024
            MsgBox($MB_SYSTEMMODAL, "", "Free Space: " & $megs & " MB")
            Run("C:\Batch\CleanTemp.bat")
        EndIf
    EndIf

    Sleep(7200000) ; Two hours
WEnd

Func StartStop()
    $On = Not $On
EndFunc

Func ExitProg()
    Exit
EndFunc

 

Share this post


Link to post
Share on other sites

It may be working, but it will take 2 hours to see it.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

You are going to have to change your loop to something much smaller, maybe a few hundred milliseconds. (See the help file on HotKeySet)

Start a timer when you you run the batch file and then check the timer for a minimum two hour differential at the If $iFreeSpace block.

TimerInit/TimerDiff may work or you may have to use macros to get the current time and subtract from last time the batch file was run.

Edited by blakel
Be careful with this as the batch file is likely to take longer to run than you would want to check for user input and put it tnto a strange loop with a lot of disk usage.

Share this post


Link to post
Share on other sites

That's the problem exactly.  My logic is wrong because it takes 2hrs to see it.

The F4 works instantly.

The F7 may work if I waited 2hrs.

Danp2

I like your coding.  I think it's still waiting .

Thanks,

Docfxit

 

Share this post


Link to post
Share on other sites

Try this

include <MsgBoxConstants.au3>
AutoItSetOption("TrayIconDebug", 1) ;0-off
; Set so that tray displays current line number

HotKeySet("{F4}", "ExitProg")
HotKeySet("{F7}", "StartStop")

$On = True
Adlibregister("cleanup",7200000)
While True
WEnd
Func cleanup()
If $On Then
        Local $iFreeSpace = DriveSpaceFree("E:" & "\") ; Find the free disk space of the home drive, generally this is the C:\ drive.
        If $iFreeSpace < 1000 Then ;Less than 1gb
            $megs = $iFreeSpace * .1024
            MsgBox($MB_SYSTEMMODAL, "", "Free Space: " & $megs & " MB")
            Run("C:\Batch\CleanTemp.bat")
        EndIf
    EndIf
EndFunc

Func StartStop()
    $On = Not $On
EndFunc

Func ExitProg()
    Exit
EndFunc

Share this post


Link to post
Share on other sites

You should still put a call to Sleep in your While loop. Otherwise, you are eating too many cpu cycles.

Share this post


Link to post
Share on other sites

I added a Sleep in the While loop. 

I changed the hotkey just in case the F7 was used by something else.

I added a tooltip to see if the hot key was recognized.

The tooltip stays open all the time.  Even when I press Shift-Alt-d

The script exits immediately when I press F4

#include <MsgBoxConstants.au3>
AutoItSetOption("TrayIconDebug", 1) ;0-off
; Set so that tray displays current line number

HotKeySet("{F4}", "ExitProg")
HotKeySet("+!d", "StartStop") ; Shift-Alt-d

$On = True
Adlibregister("cleanup",7200000)
While True
    Sleep(500)
     ToolTip('Script is "True"', 500, 300)
 WEnd
 ToolTip("")
Func cleanup()

If $On Then
        Local $iFreeSpace = DriveSpaceFree("E:" & "\") ; Find the free disk space of the home drive, generally this is the C:\ drive.
        If $iFreeSpace < 1000 Then ;Less than 1gb
            $megs = $iFreeSpace * .1024
            MsgBox($MB_SYSTEMMODAL, "", "Free Space: " & $megs & " MB")
            Run("C:\Batch\CleanTemp.bat")
        EndIf
    EndIf
EndFunc

Func StartStop()
    $On = Not $On
EndFunc

Func ExitProg()
    Exit
EndFunc

Why does the F4 work immediately and the Shift-Alt-d doesn't work at all?

 

Thanks,

Docfxit

Share this post


Link to post
Share on other sites

ToolTill inside the loop

#include <MsgBoxConstants.au3>
AutoItSetOption("TrayIconDebug", 1) ;0-off
; Set so that tray displays current line number

HotKeySet("{F4}", "ExitProg")
HotKeySet("+!d", "StartStop") ; Shift-Alt-d

$On = True
Adlibregister("cleanup",7200000)
While True
    Sleep(500)
     ToolTip('Script is "' & $On & '"', 500, 300)
 WEnd

Func cleanup()

If $On Then
        Local $iFreeSpace = DriveSpaceFree("E:" & "\") ; Find the free disk space of the home drive, generally this is the C:\ drive.
        If $iFreeSpace < 1000 Then ;Less than 1gb
            $megs = $iFreeSpace * .1024
            MsgBox($MB_SYSTEMMODAL, "", "Free Space: " & $megs & " MB")
            Run("C:\Batch\CleanTemp.bat")
        EndIf
    EndIf
EndFunc

Func StartStop()
    $On = Not $On
EndFunc

Func ExitProg()
    Exit
EndFunc

 

If you want to cleanup as soon as shift+alt+d is pressed then call cleanup()  in StartStop function

Share this post


Link to post
Share on other sites

That works great.

Thank you very much,

 

Docfxit

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