Jump to content

How to toggle a hotkey


Recommended Posts

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

Link to comment
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

 

Link to comment
Share on other sites

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.
Link to comment
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
Link to comment
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

Link to comment
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

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