Jump to content

Recommended Posts

Posted

I'm trying to figure out if there is a way to have a hotkey work only while a window is active. Does anyone have a snippit of code that does this? I currently use hotkeyset using a ctrl + # to run my shortcuts. I wanted to use the function keys, but only while the window that I want to work with is the one that has focus.

Posted (edited)

heres an idea

Dim $runner = 0
HotKeySet("{F8}", "runner")



While 1
    
    If $runner = 1 And Not WinExists(" my program") Then
        HotKeySet("{F8}", "runner")
        $runner = 0
    EndIf
    Sleep(3000)
WEnd

Func runner()
    Run(" My program.exe")
    HotKeySet("{F8}", "do something else")
    $runner = 1
EndFunc

8)

EDIT

made it so that it doesn't continually set hot key

Edited by Valuater

NEWHeader1.png

Posted

You might also be able to do it this way if I understand you correctly:

Opt('WinWaitDelay', 0)

Local $Win = 'My window title'
Local $Hotkey = '{F6}'

; Ensure that the window exists before continuing
WinWait($Win)

While WinExists($Win)
    WinWaitActive($Win)
    HotkeySet($Hotkey, 'ProcessHotkey')
    WinWaitNotActive($Win)
    HotkeySet($Hotkey)
WEnd

Func ProcessHotkey()
    ; ...
EndFunc

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...