Jump to content

Is checking the active window to run the event with a function the way to go?


Loc
 Share

Go to solution Solved by Danp2,

Recommended Posts

I am writing a tool that uses Hotkeyset to run events or show another gui by checking for winactive but when using guicreate's variable name it doesn't work as well as its window name, when hotkey duplicates it does may be faulty. Is there a better way than this?

While 1
;~ If WinActive($LoginName) Then
If WinActive($Login) Then
HotKeySet("{enter}" , '_Login')
;____ACTIVE LOGIN
;~ ElseIf WinActive($ToolName) Then
ElseIf WinActive($Tool) Then
_Next($Defaul)
_PauSe()
_Set()
_Check()
HotKeySet("^a" , '_Admin'); show gui
HotKeySet("{F1}" , '_Help'); show gui
_Search()
_GUICtrlHover($Tool, $BT, $BTCM, $LbOver)
_GUICtrlSetOnEventRightClick()
;____ACTIVE Tool
;~ ElseIf WinActive($AddName) Then
ElseIf WinActive($Add) Then
_Add()
;~ ElseIf WinActive($KeyName) Then
ElseIf WinActive($Key) Then
HotKeySet("{enter}" , "_KeySet"); show gui
;____ACTIVE KEY
Else
HotKeySet("{enter}")
HotKeySet("{F1}")
HotKeySet("^a")
EndIf
If WinActive($Calculator) Then
_SetHotkeyCa()
Else
_UnSethotkeyCa()
EndIf
;____ACTIVE Calculator
WEnd

Here is my in-loop test code

Link to comment
Share on other sites

What's really sad to me is that you haven't done a good job of describing the problem. Also, the code you posted isn't complete so that we can actually run it to observe the issue. 😞

FWIW, your code would make more sense to me if you did something like this --

$hActive = WinGetHandle("[Active]")

; Clear prior hotkeys here

Switch $hActive
Case $hWindow1
    ; Set hotkeys here
Case $hWindow2
    ; Set hotkeys here
Case $hWindow3
    ; Set hotkeys here
EndSwitch

Also, it would be wise to save the last active window handle and compare it to the current one. If they are the same, then you can skip the whole process of clearing / setting the hotkeys.

Link to comment
Share on other sites

26 minutes ago, Danp2 said:

What's really sad to me is that you haven't done a good job of describing the problem. Also, the code you posted isn't complete so that we can actually run it to observe the issue. 😞

 

I'm sorry, I was just thinking simply check the active window then sethotkey and run the function :(

Link to comment
Share on other sites

Ok... but when you make statements like the following, you need to back them up with proof --

On 7/23/2021 at 12:31 AM, Loc said:

checking for winactive but when using guicreate's variable name it doesn't work as well as its window name

Your statement basically says that WinActive isn't working correctly when you pass it a window handle. I suspect that any issues you are encountering is due to poor coding on your part rather than a bug in AutoIt. If you think I'm wrong, then provide an short reproducer that we can run to observe the problem.

Link to comment
Share on other sites

I did it your way. Why did I set the delete hotkey button in the old GUI but when I went to the new gui, the key was still usable? Fix it, put hotkey on new gui but a bit much code, is there another way?

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <ListViewConstants.au3>

Global $FormLogin, $FormAdd, $ID, $PASS, $InputID, $InputPass, $InputYear, $ListViewadd

$FormLogin = GUICreate("Login Form", 309, 168, -1, -1)
GUISetFont(12, 400, 0, "Courier New")
GUICtrlCreateLabel("ID", 24, 32, 24, 22)
$ID = GUICtrlCreateInput("", 64, 28, 209, 26)
GUICtrlCreateLabel("PASS", 16, 80, 44, 22)
$PASS = GUICtrlCreateInput("", 64, 76, 209, 26, BitOR($GUI_SS_DEFAULT_INPUT,  $ES_PASSWORD))
$ButtonLogin = GUICtrlCreateButton("Login", 112, 128, 89, 33)
GUISetState(@SW_SHOW)

While 1
$hActive = WinGetHandle("[Active]")
; Clear prior hotkeys here
HotKeySet('{enter}')
HotKeySet('{F1}')
Switch $hActive
    Case $FormLogin
        HotKeySet('{enter}', '_AddID')
        HotKeySet('{F1}', '_TestKey')
    ; Set hotkeys here
;~ Case $hWindow2
    ; Set hotkeys here
;~ Case $hWindow3
    ; Set hotkeys here
EndSwitch
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $ButtonLogin
            _AddID()

    EndSwitch
WEnd

Func _TestKey()
MsgBox(0,'Test', 'successful')
EndFunc

Func _AddID()
;~ HotKeySet('{enter}')
GUIDelete($FormLogin)
$FormAdd = GUICreate("Form Add", 588, 354, -1, -1)
GUISetFont(12, 400, 0, "Courier New")
GUICtrlCreateLabel("ID", 16, 8, 24, 22)
$InputID = GUICtrlCreateInput("", 48, 6, 121, 26)
GUICtrlCreateLabel("PASS", 184, 8, 44, 22)
$InputPass = GUICtrlCreateInput("", 232, 6, 121, 26)
GUICtrlCreateLabel("Year", 360, 8, 44, 22)
$InputYear = GUICtrlCreateInput("", 416, 6, 161, 26)
$ListViewadd = GUICtrlCreateListView("ID|PASS|YEAR", 15, 56, 561, 281)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 200)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 200)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 2, 150)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd
EndFunc

 

Link to comment
Share on other sites

  • Solution
  • No, I never said you couldn't run it in a loop.
  • You have to "unset" a hotkey to prevent it from continuing to function
  • I was suggesting that you create a function that would remove all previously defined hotkeys. That way you could call this function from anywhere in your script instead of adding more HotKeySet statements.
  • In your original post, you referenced windows such as $Login, $Add, & $Calculator. Because you only included a portion of your code, I assumed that these were windows associated with external programs. Now that you've posted additional code, my initial assumption appears to be wrong.
  • Because we have a language barrier, it is important that you provide enough details for others to understand what you are saying.
  • For example, you said "Fix it, put hotkey on new gui but a bit much code, is there another way? " and I have no way of knowing which portion of the code is troubling you.

Hope this helps. 🙂

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