Page 1 of 1

Hotkey within IF clause?

Posted: 24 Aug 2019, 01:58
by scriptor2016
Hi, how do I create a new hotkey inside an if clause? For example, this is my code:

Code: Select all

#Persistent
SetTimer, WatchCursor, 100
return

WatchCursor:

SetTitleMatchMode, 2
MouseGetPos, , , id, control
WinGetTitle, title, ahk_id %id%
WinGetClass, class, ahk_id %id%


if title contains MyWindow  
{
RButton::
Msgbox, Right Button
}
else
{
tooltip, keep searching...
}
return
 
How do I do this so that the RButton will show the messagebox *only* when the wintitle under the cursor contains MyWindow?

Re: Hotkey within IF clause?

Posted: 24 Aug 2019, 02:56
by Odlanir

Code: Select all

#if (title ~= "MyWindow") ; ~= is a shorthand for RegExMatch
   RButton::
      MsgBox, Right Button
   return
#If

Re: Hotkey within IF clause?

Posted: 24 Aug 2019, 03:09
by scriptor2016
thanks Odlanir!!! This will be perfect :)

Re: Hotkey within IF clause?

Posted: 24 Aug 2019, 13:39
by scriptor2016
well that's strange. This was working perfectly last night but today it just stopped working.

Right clicking acts as normal now, it does not show the messagebox.


Is there anything wrong with this code?

Code: Select all


#if (title = "Untitled - Notepad") ; ~= is a shorthand for RegExMatch
   RButton::
      MsgBox, Right Button
   return
#If

Re: Hotkey within IF clause?

Posted: 24 Aug 2019, 13:44
by gregster
The ~ of ~= is missing in your code.

( ~= is a shorthand for RegExMatch )

Re: Hotkey within IF clause?

Posted: 24 Aug 2019, 13:50
by scriptor2016
believe it or not, it still isn't working

it was running perfectly last night and this morning it just stopped . I've closed all other scripts to troubleshoot and no dice - a right click just acts normally when ontop of the untitled notepad window.

here it is again,

Code: Select all

#if (title ~= "Untitled - Notepad") ; ~= is a shorthand for RegExMatch
   RButton::
      MsgBox, Right Button
   return
#If

Re: Hotkey within IF clause?

Posted: 24 Aug 2019, 14:09
by gregster
It is believable. title still needs to be determined somehow (you used a timer in your original script) - I don't see how you do it in your new script.

Please post your complete script, if in doubt. (If this is already your complete script, it cannot work - that's clear.)

You could try:

Code: Select all

#if IsWintitle("Untitled - Notepad")
   RButton::
      MsgBox, Right Button
   return
#If

IsWintitle(title){
	SetTitleMatchMode, 2			; or some other mode 
	MouseGetPos, , , id
	WinGetTitle, wintitle, ahk_id %id%
	return (title = wintitle) ? true : false	; or just:      return (title = wintitle)
}

Re: Hotkey within IF clause?

Posted: 25 Aug 2019, 02:40
by scriptor2016
gregster - thanks - its working now.

I think I changed something along the lines overnight which caused this to stop working. I've got it going now. I'll revisit this topic if I run into other problems :)