SendInput specify hWnd / improved ControlSend

Get help with using AutoHotkey and its commands and hotkeys
User avatar
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

SendInput specify hWnd / improved ControlSend

16 Sep 2017, 09:08

I have this script that half works to send Ctrl+C to Notepad/Internet Explorer. It works, but then if I reload the script it ceases to work.

The final goal is to be able to send keys where SendInput does work, but where ControlSend doesn't work. Thanks for reading.

Code: Select all

;this script works but if I reload the script
;it then ceases to work correctly
q:: ;send ctrl+c without using Send/SendInput
;e.g. tested on Notepad (Windows 7) and Internet Explorer 11
;same results, work correctly until reload
WinGet, hWnd, ID, A
ControlGetFocus, vCtlClassNN, % "ahk_id " hWnd
if !(vCtlClassNN = "")
	ControlGet, hWnd, Hwnd,, % vCtlClassNN, % "ahk_id " hWnd

vTIDAhk := DllCall("kernel32\GetCurrentThreadId", "UInt")
VarSetCapacity(PBYTE1, 256, 0)
DllCall("user32\GetKeyboardState", "Ptr",&PBYTE1)

vTID := DllCall("user32\GetWindowThreadProcessId", "Ptr",hWnd, "Ptr",0, "UInt")
DllCall("user32\AttachThreadInput", "UInt",vTIDAhk, "UInt",vTID, "Int",1)
VarSetCapacity(PBYTE2, 256, 0)
NumPut(0x80, &PBYTE2, 0x11, "UChar") ;VK_CONTROL := 0x11
DllCall("user32\SetKeyboardState", "Ptr",&PBYTE2)

ControlSend,, c, % "ahk_id " hWnd

DllCall("user32\AttachThreadInput", "UInt",vTIDAhk, "UInt",vTID, "Int",0)
DllCall("user32\SetKeyboardState", "Ptr",&PBYTE1)
Some related threads:
AutoHotkey source code: Send/ControlSend (modifier keys) - AutoHotkey Community
PostMessage plz help - AutoHotkey Community
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask For Help”

Who is online

Users browsing this forum: adrian88888888, Bing [Bot], pwright2, wer, william_ahk and 33 guests