Sign in to follow this  
Followers 0
CodeMaster Rapture

Tired of your hotkeys mucking up other apps?

13 posts in this topic

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

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Ahh , behold the awesome power of GUIRegisterMsg(). :) This should prove quite useful until accelerator keys are added to Autoit. Thanks for sharing.

Edited by SolidSnake

HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.

Share this post


Link to post
Share on other sites

Good solution. Thanks!


Só o que posso lhe dizer, bom é quando faz mal!My work:Au3Irrlicht - Irrlicht for AutoItMsAgentLib - An UDF for MSAgentAu3GlPlugin T2 - A 3D plugin for AutoIt...OpenGl Plugin - The old version of Au3GlPlugin.MAC Address Changer - Changes the MAC AddressItCopter - A dragonfly R/C helicopter simulator[center] VW Bug user[/center]Pinheiral (Pinewood) city: http://pt.wikipedia.org/wiki/Pinheiral

Share this post


Link to post
Share on other sites

I'll be looking into other methods of hotkey setups (i.e. keyboard hooking) that is not as taxing on the CPU. Glad yall enjoy this.

-CMR

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

great solution i use IsPressed()

but IsPressed() allows you to hold the key down which isnt what i wanted...

Nice Idea though...


[center][/center]

Share this post


Link to post
Share on other sites

#8 ·  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

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

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]

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Even better! :)

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
Sign in to follow this  
Followers 0