ControlSend not sending to the non-topmost window - autohotkey

Is it possible to make ControlSend send / click to a background window?
This code finds a window by id / handler, but sends a click / keystroke only if the window is the topmost. ControlSend skips its action if the window is not topmost. ControlClick force brings window to top and then clicks.
#Singleinstance
DetectHiddenWindows, On
#SingleInstance Force
#MaxThreadsPerHotkey, 2
SendMode Input ; Recommended for new scripts
SetControlDelay -1
PostClick(x,y,win="A") {
lParam := x & 0xFFFF | (y & 0xFFFF) << 16
PostMessage, 0x201, , %lParam%, , %win% ;WM_LBUTTONDOWN
PostMessage, 0x202, , %lParam%, , %win% ;WM_LBUTTONUP
}
RunAsAdmin() {
Loop, %0% {
param := %A_Index% ; Fetch the contents of the variable whose name is contained in A_Index.
params .= A_Space . param
}
ShellExecute := A_IsUnicode ? "shell32\ShellExecute":"shell32\ShellExecuteA"
if not A_IsAdmin
{
If A_IsCompiled
DllCall(ShellExecute, uint, 0, str, "RunAs", str, A_ScriptFullPath, str, params , str, A_WorkingDir, int, 1)
Else
DllCall(ShellExecute, uint, 0, str, "RunAs", str, A_AhkPath, str, """" . A_ScriptFullPath . """" . A_Space . params, str, A_WorkingDir, int, 1)
ExitApp
}
}
RunAsAdmin()
F1::
Loop
{
; http://particle-clicker.web.cern.ch/particle-clicker/
WinGet, WinID, ID, Particle Clicker - Google Chrome
; Send keystrokes only if tab active and topmost, suspend if focus lost, continue when gain focue
; ControlSend, ahk_parent, {Space}, ahk_id %WinID%
; send clicks only if tab not mininised, if tab on background - force bring it to front
; ControlClick, x799 y449, ahk_id %WinID%
; same as above
; ControlClick, x799 y449, ahk_id %WinID%,,,,NA
; same as above
; Controlclick x799 y449,ahk_id %WinID%,,Left,1,NA
; same as above
PostClick(799,449,"Particle Clicker - Google Chrome")
sleep 1000
}
return
F12::ExitApp

Related

How to activate a Script only if a specific Window is active?

I have a problem with a Script which should just do the following:
- If a specific Window becomes active
- SetCapslockState, On
- If the Windows lost focus
- SetCapslockState, Off
I've tried:
#If WinActive("ahk_class blahblah")
SetCapslockState, On
and/or
#If !WinActive("ahk_class Chrome_WidgetWin_1")
SetCapslockState, Off
But it don't work.
Also I've tried:
WinWaitActive, (mytitleofwindow)
if ErrorLevel
{
SetCapslockState, On
return
}
else
It don't work either, else I would not ask for help here... hihihi
I hope someone can help me! :)
Instead of a loop (which takes up CPU cycles), you can also use Hardware Handles. See example:
#SingleInstance Force
#installKeybdHook
#Persistent
Menu, Tray, Tip, Medical Alert
SetKeyDelay, 50
Menu, Tray, Icon , Shell32.dll, 145, 1
TrayTip, Medical Alert, Started, 1
Gui +LastFound
hWnd := WinExist()
DllCall( "RegisterShellHookWindow", UInt,Hwnd )
MsgNum := DllCall( "RegisterWindowMessage", Str,"SHELLHOOK" )
OnMessage( MsgNum, "ShellMessage" )
Return
ShellMessage( wParam ) ; Run on Window switch
{
If (wParam = 4) ; If Window Changed
{
WinGetActiveTitle, Title
if instr(Title, "Past Medical History") OR instr(Title, "Allergies Verified") ; TESTED WITH: if instr(Title, "NotePad")
MsgBox, 1, Allergies Verified, Please verify patient allergies
}
}
Return
The key is to combine WinWait[Not]Active with a Loop.
Loop {
WinWaitActive, mytitleofwindow
SetCapslockState, On
WinWaitNotActive, mytitleofwindow
SetCapslockState, Off
}

AHK Script using PostClick and PostMessage

I'm trying to write a simple AHK script to type a few characters and make a few clicks at specific coordinates in a background window, but I can't seem to get it to work, and I'm not finding a lot of information on how to properly use PostClick.
I was able to get this to work using Click when the window is active. Also tried using ControlClick, which seemed to work, except the click was happening on the location where the mouse was rather than the coordinates provided (and also only when the window was activated again).
Can I please have some help / advice on how to get this to work?
Here's my current script:
DetectHiddenWindows On
S:: ;Character to start the loop
pid = 19164 ;Application PID
ControlGet, clickVar, Hwnd , , ahk_pid %pid% ;Specify control for which program
BreakLoop = 0
Loop, 3 {
if (BreakLoop = 1)
break
Sleep 1000
PostClick(clickVar,1055,525)
{
ControlSend,, 3, ahk_pid %pid%
lParam := x & 0xFFFF | (y & 0xFFFF) << 16
PostMessage, 0x201, , %lParam%, , ahk_pid %pid% ;WM_LBUTTONDOWN
PostMessage, 0x202, , %lParam%, , ahk_pid %pid% ;WM_LBUTTONUP
}
Sleep 1500
}
E::
BreakLoop = 1
return
Thanks ahead of time for the help.
I am going to assume you are attempting to use this function.
I am unsure how this function works, but I think what you want is something like:
DetectHiddenWindows On
S:: ;Character to start the loop
pid = 19164 ;Application PID
ControlGet, clickVar, Hwnd , , ahk_pid %pid% ;Specify control for which program
WinGetTitle, clickTitle, ahk_pid %pid%
WinGetClass, clickClass, ahk_pid %pid%
BreakLoop = 0
Loop, 3
{
if (BreakLoop = 1)
break
Sleep 1000
PostClick(1055,525,clickClass, clickTitle)
ControlSend,, 3, ahk_pid %pid%
Sleep 1500
}
E::
BreakLoop = 1
return
PostClick(x, y, class, title)
{
lParam := x & 0xFFFF | (y & 0xFFFF) << 16
PostMessage, 0x201, 1, %lParam%, %class%, %title% ;WM_LBUTTONDOWN
PostMessage, 0x202, 0, %lParam%, %class%, %title% ;WM_LBUTTONUP
}
You need to place the function somewhere in the same file or at least accessible by the current file. You can do #include <script name here>.ahk at the top of your file and place that function as a new file in the same directory if you want.
The function you are trying to use takes in an x and a y for where to click in that window. Then it also takes in a class and title in order to know which window to actually use. Hope this works or helps you in some way.

AHK Opening last opened windows explorer window if active, else starting new

This code figures out the current explorer windows open,
I would like to open the first in the list, and if the list is empty open a
new explorer instead.
I hope to the open/activate the either window at the current mouse position
#e::
list := ""
numberOfwindows := ""
wins := ""
WinGet, id, list, ahk_class CabinetWClass ahk_exe explorer.exe
Loop, %id%
{
numberOfwindows := A_Index
this_ID := id%A_Index%
WinGetTitle, title, ahk_id %this_ID%
wins .= A_Index A_Space title ? A_Index A_Space title "`n" : ""
}
MsgBox, number of explorer windows = %numberOfwindows%`n`n%wins%
return
This solves it. - however it can be optimized, anyone have any suggestions ?
#e::
list := ""
numberOfwindows := ""
wins := ""
WinGet, id, list, ahk_class CabinetWClass ahk_exe explorer.exe
Loop, %id%
{
numberOfwindows := A_Index
this_ID := id%A_Index%
WinGetTitle, title, ahk_id %this_ID%
if (A_Index = 1) { ; if it's the first index of the loop
;MsgBox %title%
win = %title% ; store the title in " win "
}
wins .= A_Index A_Space title ?½½ A_Index A_Space title "`n" : ""
}
IfWinNotExist ahk_class CabinetWClass
{
Run C:\Windows\explorer.exe
win := File Explorer
WinWait, %win% ahk_class CabinetWClass
WinMove, mxpos_new , mypos_new
WinActivate
}
;MsgBox, number of explorer windows = %numberOfwindows%`n`n%wins%
; above msgbox displays number and the names of the windows.
;~ ; we now know the win
; and its title, exe and class.
; we want it's current position.
WinGetPos, X, Y, Width, Height,%win% ahk_class CabinetWClass
;MsgBox, %X%, %Y%, %Width%, %Height%
; and we want the mouse position.
CoordMode, Mouse, Screen ; Coordinates are relative to the desktop (entire screen).
MouseGetPos, mxpos , mypos,
;MsgBox, %mxpos%, %mypos%
mxpos_new := mxpos - (Width / 2)
mypos_new := mypos - (Height / 2)
;MsgBox, %mxpos% %mypos% %Width% %Height% %mxpos_new% %mypos_new%
; activate that specific window
WinWait, %win% ahk_class CabinetWClass
WinMove, mxpos_new , mypos_new
WinActivate
return

Autohotkey: Activate foremost of X monitor

How do I activate the foremost window in a given monitor? suppose I have two monitors, one with an editor, and one with different apps, such as chrome and slack. I want to bind a key that will activate the foremost window in monitor two, be it slack or chrome, and one for the editor, for easy manuvering.
The foremost window in a given monitor is the currently active window
or (if the currently active window is on the other monitor) the last active window of this monitor.
; activate the editor:
F1::
WinActivate, ahk_exe notepad.exe ; replace notepad with the ahk_exe of the editor
return
; activate the last active window in the right monitor:
F2::
WinGetPos, X,,,, A
If (X > 1920) ; replace 1920 with the width of the left monitor
return ; do nothing
; otherwise:
WinGet, id, list
Loop, %id%
{
this_ID := id%A_Index%
WinGet, exStyle, exStyle, ahk_id %this_ID%
If !(exStyle & 0x100)
continue
WinGetTitle, title, ahk_id %this_ID%
If (title = "")
continue
WinGetPos, X,,,, ahk_id %this_ID%
If (X > 1920) ; replace 1920 with the width of the left monitor
{
WinActivate, ahk_id %this_ID%
break
}
}
return

Resize window after taskbar hidden

Below hotkey script hides/displays taskbar when lWin&H is keyed :
LWin & h::
if toggle := !toggle
{
WinHide ahk_class Shell_TrayWnd
WinHide Start ahk_class Button
}
else
{
WinShow ahk_class Shell_TrayWnd
WinShow Start ahk_class Button
}
return
Script is taken from a comment at http://lifehacker.com/taskbar-control-hides-and-unhides-the-windows-taskbar-w-1573974951
But when the taskbar is hidden the space which occupied the taskbar is unusable : windows do not drag to this area, newly opened program do not occupy this space.
Can script be modified so that when taskbar is hidden the entire screen area is usable ?
Here is a way that sets the work area to include the taskbar space to.
LWin & h::
if toggle := !toggle
{
WinHide ahk_class Shell_TrayWnd
WinHide Start ahk_class Button
SysGet, Mon, Monitor
SetWorkArea(MonLeft, MonTop, MonRight, MonBottom)
}
else
{
WinShow ahk_class Shell_TrayWnd
WinShow Start ahk_class Button
SysGet, Mon, MonitorWorkArea
SetWorkArea(MonLeft, MonTop, MonRight, MonBottom)
}
return
SetWorkArea(left,top,right,bottom) ; set main monitor work area ; windows are not resized!
{
VarSetCapacity(area, 16)
NumPut(left, area, 0, "UInt") ; left
NumPut(top, area, 4, "UInt") ; top
NumPut(right, area, 8, "UInt") ; right
NumPut(bottom,area,12, "UInt") ; bottom
DllCall("SystemParametersInfo", "UInt", 0x2F, "UInt", 0, "UPtr", &area, "UInt", 0) ; SPI_SETWORKAREA
}
Hope it helps
Instead of using autohotkey I use this utility :
http://www.itsamples.com/taskbar-hider.html
This appears to work (just tested on Windows7) :
lWin & h::
;#NoEnv
#NoTrayIcon
;#SingleInstance force
DetectHiddenWindows, Off ;for IfWinExist
VarSetCapacity( APPBARDATA, 36, 0 )
;------------------------------------------------------------
; Fetch current hidden/showing status
IfWinNotExist, ahk_class Shell_TrayWnd
TaskbarAndStartToggleState = 0 ;Currently [color=darkred]hidden[/color] (not showing)
Else
TaskbarAndStartToggleState = 1 ;Currently [color=darkred]non-hidden[/color] (showing)
;------------------------------------------------------------
Gosub +z ;Toggle the taskbar/SM state
;------------------------------------------------------------
Exit
;------------------------------------------------------------
+z::
TaskbarAndStartToggleState := Func(TaskbarAndStartToggleState)
Return
Func(TaskbarAndStartToggleState)
{
Global APPBARDATA
If TaskbarAndStartToggleState = 0
{
NumPut( (ABS_ALWAYSONTOP := 0x2), APPBARDATA, 32, "UInt" ) ;Enable "Always on top" [color=darkred](& disable auto-hide)[/color]
DllCall( "Shell32.dll\SHAppBarMessage", "UInt", ( ABM_SETSTATE := 0xA ), "UInt", &APPBARDATA )
WinShow ahk_class Shell_TrayWnd
Return 1 ;Now showing
}
If TaskbarAndStartToggleState = 1
{
NumPut( ( ABS_AUTOHIDE := 0x1 ), APPBARDATA, 32, "UInt" ) ;Disable "Always on top" [color=darkred](& enable auto-hide to hide Start button)[/color]
DllCall( "Shell32.dll\SHAppBarMessage", "UInt", ( ABM_SETSTATE := 0xA ), "UInt", &APPBARDATA )
WinHide ahk_class Shell_TrayWnd
;WinHide ahk_class Shell_TrayWnd ;[color=darkred]don't need this 2nd one?[/color]
Return 0 ;Now hidden
}
}
return
Slightly modified from :
http://www.autohotkey.com/board/topic/25932-trying-to-toggle-autohide-taskbar-with-keystroke-in-vista/page-2
Update : This script causes unexpected behavior when using multiple desktops. The taskbar is hidden but when toggle to restore just windows icon is display. Hiding the taskbar and unhiding the taskbar in taskbar properties appears to fix this, but this makes the script unusable if using multiple desktops.