I can't make a script to work only on specific window

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Stark78
Posts: 61
Joined: 03 Oct 2013, 03:17

I can't make a script to work only on specific window

26 Dec 2013, 08:46

I have a script that sends keystrokes when the mouse touch the edges of the screen and i want to make the script to work only when Firefox is the active window. I tried with SetTitleMatchMode 2 but i couldn't make it work.
This is the original script:

Code: Select all

#SingleInstance, Force

actionDelay := 10
   ; if the mouse is at the given edge of the screen
   ; and at the same time, the mouse doesn't leave that edge
   ; during the process of waiting, after how many ms do we trigger
   ; an action?
   ;
refreshRate := 100
   ; after how many ms we reread the position of the mouse

oldMon := "", oldEdge := "", Count := 0
Loop,
{
   MouseIsOnEdgeOfMon(mon, edge)
   
   ; first condition - mouse is on edge
   if(edge != "none")
   {
      ; remember the edge
      oldMon := mon, oldEdge := edge
   }
   else ; if edge = "none"
   {
      ; reset the edge memory
      oldMon := "", oldEdge := ""
   }
   
   ; if mouse still on the same edge, increase the counter
   if(mon = oldMon && edge = oldEdge)
   {
      ; increase the counter
      Count++
   }
   else   ; if the mouse on a different edge
   {
      Count := 0                  ; reset the counter
      oldMon := mon, oldEdge := mon   ; reset the data about the previous edge
      actionInvoked := false         ; reset the trigger info for the new edge
   }
   ; second condition - mouse is still on the _same_ edge
   ; a given amount of time has elapsed
   if(mon = oldMon && edge = oldEdge && Count*refreshRate > actionDelay)
   {
      ; third condition - the mouse stayed on a particular edge
      ; the whole time, _and_ the action was not yet called for this edge
      if(actionInvoked = false)
      {
         actionAllowedToRun := true
      }
      else
      {
         actionAllowedToRun := false
      }
   }
   else   ; delay time not sufficient
   {
      ; do nothing - just wait
   }
   
   ; trigger the action
   if(actionAllowedToRun = true) {
      if      (mon = 1 && edge = "Left") {
         ;Send, {Your Key}
      }else if(mon = 1 && edge = "Right") {
          ;Send, {Your Key}
      }else if(mon = 1 && edge = "Top") {
         ;Send, {Your Key}
      }else if(mon = 1 && edge = "Bottom") {
         ;Send, {Your Key}
      }else if(mon = 2 && edge = "Left") {
         
      }else if(mon = 2 && edge = "Right") {
         
      }else if(mon = 2 && edge = "Top") {
         
      }else if(mon = 2 && edge = "Bottom") {
         
      }
      
      actionInvoked := true
      actionAllowedToRun := false
   }
   Sleep , % refreshRate
}
return

MouseIsOnEdgeOfMon(ByRef mon, ByRef edge, PixOffset = 5) {
   ; settings
   static SM_CMONITORS := 80
   CoordMode, Mouse, Screen
    SysGet, monCount, % SM_CMONITORS
    Loop, % monCount
        SysGet, mon%A_Index%, Monitor, %A_Index%

   MouseGetPos, x, y ; relative to primary monitor
   Loop, % monCount
   {
      i := A_Index

      ; top edge
      if( mon%i%Left <= x && x <= mon%i%Right)
         && (mon%i%Top <= y && y <= (mon%i%Top + PixOffset) )
            return, 1, ErrorLevel := 0, mon := i, edge := "Top"
      ; bottom edge
      else if(mon%i%Left <= x && x <= mon%i%Right)
         && ( (mon%i%Bottom - PixOffset) <= y && y <= mon%i%Bottom)
            return, 1, ErrorLevel := 0, mon := i, edge := "Bottom"
      ; left edge
      else if(mon%i%Left <= x && x <= (mon%i%Left + PixOffset) )
         && (mon%i%Top <= y && y <= mon%i%Bottom)
            return, 1, ErrorLevel := 0, mon := i, edge := "Left"
      ; right edge
      else if( (mon%i%Right - PixOffset) <= x && x <= mon%i%Right)
         && (mon%i%Top <= y && y <= mon%i%Bottom)
            return, 1, ErrorLevel := 0, mon := i, edge := "Right"
      ; no edge
      else if(mon%i%Left <= x && x <= mon%i%Right)
         && (mon%i%Top <= y && y <= mon%i%Bottom)
            return, 0, ErrorLevel := 0, mon := i, edge := "none"
   }
   return, "", ErrorLevel := 1 ; shouldn't happen
}  

This is the script that i tried with SetTitleMatch 2

Code: Select all

#SingleInstance, Force

actionDelay := 10
   ; if the mouse is at the given edge of the screen
   ; and at the same time, the mouse doesn't leave that edge
   ; during the process of waiting, after how many ms do we trigger
   ; an action?
   ;
refreshRate := 100
   ; after how many ms we reread the position of the mouse

oldMon := "", oldEdge := "", Count := 0
Loop,
{
   MouseIsOnEdgeOfMon(mon, edge)
   
   ; first condition - mouse is on edge
   if(edge != "none")
   {
      ; remember the edge
      oldMon := mon, oldEdge := edge
   }
   else ; if edge = "none"
   {
      ; reset the edge memory
      oldMon := "", oldEdge := ""
   }
   
   ; if mouse still on the same edge, increase the counter
   if(mon = oldMon && edge = oldEdge)
   {
      ; increase the counter
      Count++
   }
   else   ; if the mouse on a different edge
   {
      Count := 0                  ; reset the counter
      oldMon := mon, oldEdge := mon   ; reset the data about the previous edge
      actionInvoked := false         ; reset the trigger info for the new edge
   }
   ; second condition - mouse is still on the _same_ edge
   ; a given amount of time has elapsed
   if(mon = oldMon && edge = oldEdge && Count*refreshRate > actionDelay)
   {
      ; third condition - the mouse stayed on a particular edge
      ; the whole time, _and_ the action was not yet called for this edge
      if(actionInvoked = false)
      {
         actionAllowedToRun := true
      }
      else
      {
         actionAllowedToRun := false
      }
   }
   else   ; delay time not sufficient
   {
      ; do nothing - just wait
   }
   
   ; trigger the action
   if(actionAllowedToRun = true) {
      if      (mon = 1 && edge = "Left") {
         ;Send, {Your Key}
      }else if(mon = 1 && edge = "Right") {
          SetTitleMatchMode 2
          #IfWinActive, Firefox
          Send {Your Key}
          #IfWinActive
      }else if(mon = 1 && edge = "Top") {
         ;Send, {Your Key}
      }else if(mon = 1 && edge = "Bottom") {
         ;Send, {Your Key}
      }else if(mon = 2 && edge = "Left") {
         
      }else if(mon = 2 && edge = "Right") {
         
      }else if(mon = 2 && edge = "Top") {
         
      }else if(mon = 2 && edge = "Bottom") {
         
      }
     
      actionInvoked := true
      actionAllowedToRun := false
   }
   Sleep , % refreshRate
}
return

MouseIsOnEdgeOfMon(ByRef mon, ByRef edge, PixOffset = 5) {
   ; settings
   static SM_CMONITORS := 80
   CoordMode, Mouse, Screen
    SysGet, monCount, % SM_CMONITORS
    Loop, % monCount
        SysGet, mon%A_Index%, Monitor, %A_Index%

   MouseGetPos, x, y ; relative to primary monitor
   Loop, % monCount
   {
      i := A_Index

      ; top edge
      if( mon%i%Left <= x && x <= mon%i%Right)
         && (mon%i%Top <= y && y <= (mon%i%Top + PixOffset) )
            return, 1, ErrorLevel := 0, mon := i, edge := "Top"
      ; bottom edge
      else if(mon%i%Left <= x && x <= mon%i%Right)
         && ( (mon%i%Bottom - PixOffset) <= y && y <= mon%i%Bottom)
            return, 1, ErrorLevel := 0, mon := i, edge := "Bottom"
      ; left edge
      else if(mon%i%Left <= x && x <= (mon%i%Left + PixOffset) )
         && (mon%i%Top <= y && y <= mon%i%Bottom)
            return, 1, ErrorLevel := 0, mon := i, edge := "Left"
      ; right edge
      else if( (mon%i%Right - PixOffset) <= x && x <= mon%i%Right)
         && (mon%i%Top <= y && y <= mon%i%Bottom)
            return, 1, ErrorLevel := 0, mon := i, edge := "Right"
      ; no edge
      else if(mon%i%Left <= x && x <= mon%i%Right)
         && (mon%i%Top <= y && y <= mon%i%Bottom)
            return, 0, ErrorLevel := 0, mon := i, edge := "none"
   }
   return, "", ErrorLevel := 1 ; shouldn't happen
}  
I hope someone can help me with this!
User avatar
Chef
Posts: 50
Joined: 14 Nov 2013, 13:01

Re: I can't make a script to work only on specific window

26 Dec 2013, 11:22

Put SetTitleMatchMode, 2 on top of your script and the if block something like this...

Code: Select all

else if(mon = 1 && edge = "Right")
{
	ifwinActive, Firefox
	{
		; <Action goes here>
	}
}
I'd prefer something like this...

Code: Select all

else if(mon = 1 && edge = "Right")
{
	ifwinActive, ahk_exe firefox.exe
	{
		; <Action goes here>
	}
}
No need for SetTitleMatchMode then.
Stark78
Posts: 61
Joined: 03 Oct 2013, 03:17

Re: I can't make a script to work only on specific window

26 Dec 2013, 12:33

Thanks, both ways are working.

I am trying, also, to do something else. How can i do this: When certain page is opened and the mouse touch the right edge to send one keystroke and when another page is opened and the mouse touch the right edge again to send a different keystroke. For example, when Google page is opened to send ctrl+alt+1 and when yahoo page is opened to send ctrl+alt+2.
Basically, i want to send different keystrokes when the mouse touch the same edge of screen depend of what page is opened. I guess this can be done with SetTitleMatchMode 2, so it can detect the window title but i am not sure how to implement it.
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: I can't make a script to work only on specific window

27 Dec 2013, 21:27

Can you not do it like like this:

Code: Select all

    else if(mon = 1 && edge = "Right")
    {
        ifwinActive, ahk_exe firefox.exe
        {
            ; <Action goes here>
        }
        ifwinActive, ahk_exe thunderbird.exe
        {
            ; <Action goes here>
        }
        ifwinActive, and so on...?
    }

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada, vikasgandhi and 317 guests