Page 1 of 1

#If and variable Hotkey key

Posted: 27 Jan 2014, 04:38
by potkon
I can't figure out how to have variable Hotkey key and use #If
For example, in code below, variable Hotkey key set to "F1", and context-sensitivity depends on active window (IfWinActive)

[code]MyHotkey := "F1"

Hotkey, IfWinActive, ahk_class Shell_TrayWnd
Hotkey, %MyHotkey%, MyHotkeySub, on
Hotkey, IfWinActive
return

MyHotkeySub:
MsgBox,,, % A_ThisHotkey, 1
return[/code]
Now I want to have context-sensitivity which depends on Window under mouse - so I have to use #If
I know how to do it without having variable Hotkey key;

[code] #If MouseIsOver("ahk_class Shell_TrayWnd")
F1::
MsgBox,,, % A_ThisHotkey, 1
return
#If

MouseIsOver(WinTitle) {
MouseGetPos,,, Win
return WinExist(WinTitle . " ahk_id " . Win)
}[/code]

But how to do it with variable Hotkey key, like in first example (MyHotkey := "F1")?
This not-working example will give you idea of what I want:

[code] MyHotkey := "F1" ; variable Hotkey key
return

#If MouseIsOver("ahk_class Shell_TrayWnd")
%MyHotkey%:: ; variable Hotkey key
MsgBox,,, % A_ThisHotkey, 1
return
#If

MouseIsOver(WinTitle) {
MouseGetPos,,, Win
return WinExist(WinTitle . " ahk_id " . Win)
}[/code]
How to do that?

Re: #If and variable Hotkey key

Posted: 27 Jan 2014, 04:40
by potkon
P.S. Why BBCode doesn't work? I put my code in [code][/code] tags... :(

Re: #If and variable Hotkey key

Posted: 27 Jan 2014, 12:32
by LazyMan
potkon wrote:P.S. Why BBCode doesn't work? I put my code in tags... :(
I think BBCode does not work with guest accounts.

Re: #If and variable Hotkey key

Posted: 27 Jan 2014, 13:25
by LazyMan
Hotkey wrote:Expression must match exactly the text of an existing #If Expression

Code: Select all

MyHotkey := "F1"
Hotkey, If, MouseIsOver("ahk_class Shell_TrayWnd")
Hotkey, %MyHotkey%, MyHotkeySub, on
Hotkey, If
Return

#If MouseIsOver("ahk_class Shell_TrayWnd")
   MyHotkeySub:
      MsgBox, , , % A_ThisHotkey, 1
   Return
#If

MouseIsOver(WinTitle) {
   MouseGetPos, , , Win
   Return WinExist(WinTitle " ahk_id " Win)
}

Re: #If and variable Hotkey key

Posted: 28 Jan 2014, 04:16
by potkon
Thank you so much! :)

[SOLVED]