Jump to content

Recommended Posts

Posted

Hey all,

I was messing around with my GUI template and came up with a slick method of enabling/disabling hotkeys depending on the window state:

Global Const $WM_ACTIVATEAPP = 0x001C
Global Const $GUI_Main_Title = "My GUI 1.00"

;--- Other GUI declarations and creations ---

GUIRegisterMsg($WM_ACTIVATEAPP,"_Toggle_Hotkeys")

Func _Toggle_Hotkeys($hWnd = 0, $Msg = 0, $wParam = 0, $lParam = 0) ;Parameters are optional
    If (WinActive($GUI_Main_Title)) Then
        HotKeySet("{F1}","_Help")
        HotKeySet("{F2}","_Help_About")
        HotKeySet("{F3}","_Help_OtherStuff")
    Else
        HotKeySet("{F1}","")
        HotKeySet("{F2}","")
        HotKeySet("{F3}","")
    EndIf
EndFunc   ;==>_Toggle_Hotkeys

Func _GUI_Exit()
    GUIRegisterMsg($WM_ACTIVATEAPP,"")
    Exit
EndFunc   ;==>_GUI_Exit

While 1
    $GUI_Msg = GUIGetMsg()
    Switch ($GUI_Msg)
        Case $GUI_EVENT_CLOSE
            _GUI_Exit()
    EndSwitch
WEnd

Now, the beauty of this code is that instead of slapping _Toggle_Hotkeys into my main loop, it will only trigger when my GUI is hidden/shown. I do it this way to reduce the number of uneeded function calls I can (thus reducing overhead). If you like, you can just use _Toggle_Hotkeys without the GUIRegisterMsg(), but you'll need to throw it in your main loop somewhere.

-CMR

  • 6 months later...
Posted

Very awesome. This should be in the helpfile..

#include <GUIConstantsEx.au3>

#region GUICreation ; You can discard this region
$GUI = GUICreate("Hotkey tester", 200, 100)
$Output = GUICtrlCreateLabel("",5,5,190,90)
GUISetState()
#endregion

GUIRegisterMsg(0x001C, "_ToggleHotkeys") ; Register the keys to toggle when something activates or deactives the app.. ; WM_ACTIVATEAPP = 0x001C
_ToggleHotkeys() ; Initialise the hotkeys this way

#region Main Loop ; You can discard this region
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd
#endregion

Func _ToggleHotkeys($lHwnd = 0, $lMsg = 0, $wParam = 0, $lParam = 0) ; IMPORTANT FUNCTION
    If WinActive($GUI) Then
        HotKeySet("{F5}", "_Function")
        ; Add more hotkeys here
    Else
        HotKeySet("{F5}")
        ; Add more hotkeys here
    EndIf
EndFunc

Func _Function() ; This is a test function, that can be removed
    GUICtrlSetData($Output, Random(0,10))
EndFunc
Posted (edited)

I do this, which works well, and doesn't tie up the CPU much at all. The example below is what I use for a time stamper / print. It allows for the hotkeyset key to be assigned quickly without much fuss to the function.

Func HotKeyFunc()   
                If WinActive("Work Order", "http://www.autoitscripts.com") Then
        Switch @HotKeyPressed
            Case "{F9}" 
                $sLongDayName = _DateDayOfWeek(@WDAY)
                $stamp = _NowTime()
                $stamp2 = _NowDate()
                send("---------------------------")
                Send("{ENTER}")
                Send(@UserName & ": " & $sLongDayName & " " & $stamp2 & ", " & $stamp & ": ")
            Case "{PRINTSCREEN}"
                $active_window = _IEAttach("Work Order") 
                _IEAction($active_window, "printdefault")
        EndSwitch
    Else
        HotKeySet(@HotKeyPressed)
        Send(@HotKeyPressed)
        HotKeySet(@HotKeyPressed, "HotKeyFunc")
           EndFunc 
    EndIf
Edited by Volly
Posted

Hey all,

I was messing around with my GUI template and came up with a slick method of enabling/disabling hotkeys depending on the window state:

Now, the beauty of this code is that instead of slapping _Toggle_Hotkeys into my main loop, it will only trigger when my GUI is hidden/shown. I do it this way to reduce the number of uneeded function calls I can (thus reducing overhead). If you like, you can just use _Toggle_Hotkeys without the GUIRegisterMsg(), but you'll need to throw it in your main loop somewhere.

-CMR

VERY nice solution. Thanks.  :)

I will use it definitelly!

Posted

i've already used it

and it's not necessary to use winactive

because in the message, the wparam is for saying ("activate", "desactivate")

here is the modified code

#include <GUIConstantsEx.au3>

#region GUICreation ; You can discard this region
$GUI = GUICreate("Hotkey tester", 200, 100)
$Output = GUICtrlCreateLabel("",5,5,190,90)
GUISetState()
#endregion

GUIRegisterMsg(0x001C, "_ToggleHotkeys") ; Register the keys to toggle when something activates or deactives the app.. ; WM_ACTIVATEAPP = 0x001C
_ToggleHotkeys() ; Initialise the hotkeys this way

#region Main Loop ; You can discard this region
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd
#endregion

Func _ToggleHotkeys($lHwnd = 0, $lMsg = 0, $wParam = 0, $lParam = 0) ; IMPORTANT FUNCTION
   If $wParam <> 0 Then
        HotKeySet("{F5}", "_Function")
        ; Add more hotkeys here
    Else
        HotKeySet("{F5}")
        ; Add more hotkeys here
    EndIf
EndFunc

Func _Function() ; This is a test function, that can be removed
    GUICtrlSetData($Output, Random(0,10))
EndFunc

-- Arck System _ Soon -- Ideas make everything

"La critique est facile, l'art est difficile"

Projects :

[list] [*]Au3Service : Run your exe as service V3 / Updated 29/07/2013 Get it Here [/list]
Posted

I do this, which works well, and doesn't tie up the CPU much at all. The example below is what I use for a time stamper / print. It allows for the hotkeyset key to be assigned quickly without much fuss to the function.

Func HotKeyFunc()   
                If WinActive("Work Order", "http://www.autoitscripts.com") Then
        Switch @HotKeyPressed
            Case "{F9}" 
                $sLongDayName = _DateDayOfWeek(@WDAY)
                $stamp = _NowTime()
                $stamp2 = _NowDate()
                send("---------------------------")
                Send("{ENTER}")
                Send(@UserName & ": " & $sLongDayName & " " & $stamp2 & ", " & $stamp & ": ")
            Case "{PRINTSCREEN}"
                $active_window = _IEAttach("Work Order") 
                _IEAction($active_window, "printdefault")
        EndSwitch
    Else
        HotKeySet(@HotKeyPressed)
        Send(@HotKeyPressed)
        HotKeySet(@HotKeyPressed, "HotKeyFunc")
           EndFunc 
    EndIf
The point of this thread is that hotkeys muck up other apps, even when you use the method that you use. For example, when playing a game that blocks simulated input, certain keys simply do not work anymore. The way that is described in this thread prevents that.

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
×
×
  • Create New...