Jump to content

Menu Item clicks take ten seconds before executing


UberNuss
 Share

Recommended Posts

It takes about 10 seconds for the program to exit, 9 seconds to turn off Caps Lock using a

menu item control, and 15 seconds for Browse dialog to appear when changing the .wav file.

Maybe the problem has to do with the styles and extended styles used for the window, or loop logic,

or the use of AdlibEnable or SoundPlay.

Within the While loop, the code to be executed when the user clicks on a context menu item

of the "Merlin" pic control, is greatly delayed in its execution for an unknown reason.

#NoTrayIcon
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
;** AutoIt_Wrapper BOILERPLATE is as follows:
;** #AutoIt3Wrapper_Icon=C:\My Documents\My Pictures\0 Icons\CoffeeMugSq3.ico
#AutoIt3Wrapper_Res_Comment=Keyboard Caps Lock state monitoring
#AutoIt3Wrapper_Res_Description=Notifies user when Caps Lock is turned on
#AutoIt3Wrapper_Res_Fileversion=0.9.0.9
#AutoIt3Wrapper_Res_FileVersion_AutoIncrement=y;(Y/N/P) AutoIncrement FileVersion. default=N; P=Prompt for to increase the version number.
#AutoIt3Wrapper_Res_Language=English (United States)
#AutoIt3Wrapper_Res_LegalCopyright=© Maxwell J. Clevett 2009. All Rights Reserved.
;** #AutoIt3Wrapper_UseAnsi=(Y/N)
;** #AutoIt3Wrapper_Run_Debug_Mode=(Y/N)
;** #AutoIt3Wrapper_Change2CUI=(Y/N)
;** #AutoIt3Wrapper_res_requestedExecutionLevel=(None/asInvoker/highestAvailable/requireAdministrator) default=None
;**
;** #AutoIt3Wrapper_Res_Icon_Add=squirrel.ico   ; Filename of ICO to be added. 1
;** #AutoIt3Wrapper_Res_Icon_Add=bear.ico       ; Filename of ICO to be added. 2
;** #AutoIt3Wrapper_Res_File_Add=               ; Filename[,Section [,ResName]] to be added. 3
;** #AutoIt3Wrapper_Res_File_Add=               ; Filename[,Section [,ResName]] to be added. 4
;**   Use this way - TraySetIcon ( [iconfile [, iconID] )  TraySetIcon(@ScriptFullPath, -1) - squirrel.ico
;**   Use this way - TraySetIcon ( [iconfile [, iconID] )  TraySetIcon(@ScriptFullPath, -2) - bear.ico
;**
; %date% = PC date in short date format
; %longdate% = PC date in long date format
; %time% = PC timeformat
://////=__=://///= the AutoIt Team
://////=__=,,,
://////=__=
://////=__=
://////=__=
://////=__=.=
://////=__=
://////=__=.
://////=__=.=
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

; Author: Squirrely2

; Issues:
;   It takes about 10 seconds for the program to exit, 9 seconds to turn off Caps Lock using a
;       menu item control, and 15 seconds for Browse dialog to appear when changing the .wav file.

#cs
It takes about 10 seconds for the program to exit, 9 seconds to turn off Caps Lock using a menu item control, and 15 seconds for Browse dialog to appear when changing the .wav file.

Maybe the problem has to do with the styles and extended styles used for the window, or loop logic, or the use of AdlibEnable or SoundPlay.

Within the While loop, the code to be executed when the user clicks on a context menu item of the "Merlin" pic control, is greatly delayed in its execution for an unknown reason.
#ce

#Region Initialize

Sleep(280)
_Singleton("SquirrelWare_CapsLockPoller")

Opt('MustDeclareVars', 1)
Opt("SendCapslockMode", 0)
Opt("TrayMenuMode", 1)
TraySetIcon("shell32.dll", 48);TraySetIcon("info");blank
TraySetToolTip(StringTrimRight(@ScriptName, 4))

Global Const $WS_POPUP = 0x80000000
Global Const $WS_EX_LAYERED = 0x00080000; I guess this creates a transparent window
Global Const $WS_EX_TOOLWINDOW = 0x00000080; keeps the gui from appearing on the taskbar.
Global Const $WS_EX_TOPMOST = 0x00000008
Global Const $GUI_CHECKED = 1
Global Const $GUI_EVENT_CLOSE = -3
Global Const $GUI_WS_EX_PARENTDRAG = 0x00100000

Global Const $VK_CAPITAL = 0x14
Global Const $s_IniFile = @ScriptDir & "\" & StringTrimRight(@ScriptName, 4) & ".ini"
Global Const $s_ShortCut = @StartupDir & "\" & StringTrimRight(@ScriptName, 4) & ".lnk"

Global $i_MSG, $i_TrayMsg, $i_Delay, $i_LeftPos, $i_RepeatItem, $i_LockSwitch = 0
Global $s_WavFile
Global $h_User32, $h_Gui, $pic_Merlin
Global $v_CapsAreLocked, $v_Exiting = False
Global $av_Ret[2]
Global $av_mi_RepeatFreq[8][2] = [[0, 0],[0, 10],[0, 20],[0, 40],[0, 60],[0, 120],[0, 180],[0, 300]]

$i_LeftPos = Round(@DesktopWidth * 0.62)
$h_User32 = DllOpen("user32.dll")
Sleep(180)
$s_WavFile = IniRead($s_IniFile, "WavSound", "Filepath", @WindowsDir & "\media\chimes.wav")
$i_RepeatItem = Number(IniRead($s_IniFile, "RepeatFrequency", "Item", "0"))
Sleep(680)

#EndRegion Initialize

#Region Create_Gui_and_Pic

; The best size of the following gui is dependent upon the size of its image.
$h_Gui = GUICreate("Merlin", 67, 69, $i_LeftPos, 1, $WS_POPUP, _
        BitOR($WS_EX_TOOLWINDOW, $WS_EX_LAYERED, $WS_EX_TOPMOST))

Sleep(160)

$pic_Merlin = GUICtrlCreatePic(@SystemDir & "\oobe\images\merlin.gif", _
        0, 0, 0, 0, -1, $GUI_WS_EX_PARENTDRAG)

#EndRegion Create_Gui_and_Pic

#Region Build_MenuS

; context menu
Global Const $m_Menu = GUICtrlCreateContextMenu($pic_Merlin)
Global Const $mi_EndCapsLock = GUICtrlCreateMenuItem("Caps Lock Off", $m_Menu)
GUICtrlCreateMenuItem("", $m_Menu)
Global Const $m_Sound_SubMenu = GUICtrlCreateMenu("Sound", $m_Menu)
$av_mi_RepeatFreq[0][0] = GUICtrlCreateMenuItem("No Repeat Sound", $m_Sound_SubMenu, -1, 1)
For $i = 1 To UBound($av_mi_RepeatFreq) - 1
    If $av_mi_RepeatFreq[$i][1] > 61 Then
        $av_mi_RepeatFreq[$i][0] = GUICtrlCreateMenuItem("Repeat every " _
                 & Round($av_mi_RepeatFreq[$i][1] / 60) & " minutes.", $m_Sound_SubMenu, -1, 1)
    Else
        $av_mi_RepeatFreq[$i][0] = GUICtrlCreateMenuItem("Repeat every " _
                 & $av_mi_RepeatFreq[$i][1] & " seconds.", $m_Sound_SubMenu, -1, 1)
    EndIf
Next
GUICtrlCreateMenuItem("", $m_Menu)
Global Const $mi_RunAtBootup = GUICtrlCreateMenuItem("Run at Bootup", $m_Menu, -1, 1)
Global Const $mi_DONTRunAtBootup = GUICtrlCreateMenuItem("Don't Run at Bootup", $m_Menu, -1, 1)
GUICtrlCreateMenuItem("", $m_Menu)
Global Const $mi_SoundFileItem = GUICtrlCreateMenuItem("Change WAV file", $m_Menu)
Global Const $mi_ExitItem = GUICtrlCreateMenuItem("Exit", $m_Menu)

; tray menu
Global Const $trayMI_Exit = TrayCreateItem("Exit CapsLockPoller")

#EndRegion Build_MenuS

#Region Loop_init

GUICtrlSetState($av_mi_RepeatFreq[$i_RepeatItem][0], $GUI_CHECKED)
If FileExists($s_ShortCut) Then
    GUICtrlSetState($mi_RunAtBootup, $GUI_CHECKED)
Else
    GUICtrlSetState($mi_DONTRunAtBootup, $GUI_CHECKED)
EndIf

GUICtrlSetTip($pic_Merlin, "Drag to move Merlin.", "Caps Lock is On - " & @ScriptName)

TraySetState()
TrayTip("CapsLockPoller is up and running.", "Merlin should appear while Caps Lock is on.", 12, 1)

$i_Delay = $av_mi_RepeatFreq[$i_RepeatItem][1] * 1000

#EndRegion Loop_init

While 1
    $i_TrayMsg = TrayGetMsg()

    ; Case the function Is_CapsLock_On failed or the user exited the script via contextmenu OR tray
    If $v_Exiting Or ($i_TrayMsg = $trayMI_Exit) Then ExitLoop

    $v_CapsAreLocked = Is_CapsLock_On()

    ; Case capslock is newly turned OFF
    If $i_LockSwitch = 1 And Not $v_CapsAreLocked Then
        GUISetState(@SW_HIDE, $h_Gui)
        If $i_Delay <> 0 Then AdlibDisable()
        $i_LockSwitch = 0
    EndIf

    Select
        ; Case capslock is newly turned ON
        Case $v_CapsAreLocked And ($i_LockSwitch = 0)
            $i_LockSwitch = 2
            ContinueCase

        ; Case capslock is ON & preparation has NOT YET been made for polling the contextmenu
        Case $v_CapsAreLocked And ($i_LockSwitch = 2)
            $i_LockSwitch = 1
            GUISetState(@SW_SHOWNOACTIVATE, $h_Gui)
            If $i_Delay <> 0 Then AdlibEnable("MakeSound", $i_Delay)
            MakeSound()
            ContinueCase

        ; Case capslock is ON & preparation has already been made for polling the contextmenu
        Case $v_CapsAreLocked And ($i_LockSwitch = 1)
            $i_MSG = GUIGetMsg()
            
            Switch $i_MSG
                Case $mi_ExitItem;, $GUI_EVENT_CLOSE
                    MakeSound()
                    ExitLoop

                Case $mi_EndCapsLock
                    MakeSound()
                    Send("{CapsLock toggle}")

                Case $mi_SoundFileItem
                    MakeSound()
                    Browse_SoundFile()

                Case $mi_DONTRunAtBootup
                    MakeSound()
                    FileDelete($s_ShortCut)
                    Sleep(900)

                Case $mi_RunAtBootup
                    MakeSound()
                    FileCreateShortcut(@ScriptFullPath, $s_ShortCut, @ScriptDir, "", _
                            "Notifies user if keyboard Caps Lock is on", "shell32.dll", "{F12}", 47)
                    Sleep(900)

            EndSwitch
            
            ; For if a sound-repeat-interval menuitem is clicked
            For $i = 0 To UBound($av_mi_RepeatFreq) - 1
                If $i_MSG = $av_mi_RepeatFreq[$i][0] Then
                    MakeSound()
                    Change_Frequency($i)
                    ExitLoop 1; applies to the For...Next loop only
                EndIf
            Next
            
    EndSelect
    ;Sleep(484)
WEnd

#Region Exit_Module

DllClose($h_User32)
Sleep(400)
IniWrite($s_IniFile, "WavSound", "Filepath", $s_WavFile)
IniWrite($s_IniFile, "RepeatFrequency", "Item", String($i_RepeatItem))
Sleep(1380)
Exit

#EndRegion Exit_Module

Func Is_CapsLock_On()
    ; Author:   GaryFrost - 12 September 2005 - orig name: _Get_CapsLock
    ; Modified: MRCreator, Squirrely2

    $av_Ret = DllCall($h_User32, "long", "GetKeyState", "long", $VK_CAPITAL); Gary's code
    ;$av_Ret = DllCall($h_User32, "short", "GetKeyState", "int", $VK_CAPITAL); MrCreator's code
    If @error Then
        $v_Exiting = True; exit if this function is not working
        MsgBox(262208, "Caps Lock Poller - Program Error", "Click OK to exit program.")
    Else
        Return ($av_Ret[0] <> 0)
    EndIf
EndFunc   ;==>Is_CapsLock_On

Func MakeSound()
    SoundPlay($s_WavFile)
EndFunc   ;==>MakeSound

Func Change_Frequency($iRF)
    If $i_RepeatItem = $iRF Then Return; user clicked on the current setting
    $i_RepeatItem = $iRF; set the global to user's choice to prepare for eventual ini-writes

    $i_Delay = $av_mi_RepeatFreq[$i_RepeatItem][1] * 1000
    AdlibDisable()
    If $i_Delay <> 0 Then AdlibEnable("MakeSound", $i_Delay)
EndFunc   ;==>Change_Frequency

Func Browse_SoundFile()
    Local $sOld_WAV, $iStrPos, $sWAV_Name, $sDIR

    $sOld_WAV = $s_WavFile
    $iStrPos = StringInStr($s_WavFile, "\", 0, -1)
    $sWAV_Name = StringMid($s_WavFile, ($iStrPos+1))
    $sDIR = StringLeft($s_WavFile, ($iStrPos-1))
    $s_WavFile = FileOpenDialog("Choose a .WAV Sound", $sDIR, "WAV files (*.wav)", 2, $sWAV_Name)
    If @error Then $s_WavFile = $sOld_WAV
EndFunc   ;==>Browse_SoundFile

Func _Singleton($sOccurenceName, $iFlag = 0, $h_Colonel = "kernel32.dll")
    Local Const $ERROR_ALREADY_EXISTS = 183
    Local $handle, $lastError, $pSecurityAttributes = 0

    $handle = DllCall($h_Colonel, "int", "CreateMutex", "ptr", $pSecurityAttributes, _
            "long", 1, "str", $sOccurenceName)
    $lastError = DllCall($h_Colonel, "int", "GetLastError")
    If $lastError[0] = $ERROR_ALREADY_EXISTS And $iFlag = 0 Then Exit -1
    Return $handle[0]
EndFunc   ;==>_Singleton
Edited by Squirrely2

Das Häschen benutzt Radar.

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