CtrlSend Function

Post your working scripts, libraries and tools for AHK v1.1 and older
off
Posts: 176
Joined: 18 Nov 2022, 21:54

CtrlSend Function

30 Jul 2023, 21:27

This function i wrote just for easily doing task in background while doing something else in your system, without stealing focus from it.

As we all know, almost all game windows cant receive keys sent from ControlSend, which is sending keys to background.
And alot of people then suggesting to briefly setting the active window to always on top, then sending keys to the game window that actually get focused behind the always on top -ed window. And sending the keys via normal Send.

And so, i create this function just for people that want easy copy paste function. Just hoping will help a few ppl.

Code: Select all

/*
		CtrlSend Function (AHK V1)
		
		Similar to ControlSend, this Function allows
		to send keys to window without activating it 
		(kinda).
		
		Most useful to virtually send key to Background
		when idle / using program that doesn't need
		active control (example: Watching Youtube).
		
		
				/!\ IMPORTANT /!\
		
		Don't forget to add:
		
		#Include C:\Path\To\CtrlSend.ahk
		
		ontop of your scripts when using this
		function. Change the path to your actual
		file path.
		
		Or copy-paste the whole function to your
		bottomest of script just once.
		
				/!\ IMPORTANT /!\
		
		
				* Note *
		This function mainly used to
		do ControlSend to game windows
		that normally doesn't accept it.
		
		Like Roblox, etc.
		
		By using this function, i
		achieve to do some chat macro
		while watching videos on youtube.
		
		The downsides using this function
		are:
		
		- Our realtime input to system is
		paused when the funtion is called
		(by BlockInput).
		
		- Having multiple AlwaysOnTop
		windows may cause error (not
		tested for that until now).
		
		- Very slow, compared to
		original ControlSend. This funtion
		tries to do the same job with 
		primitive workaround.
		
		- Inefficient for large process.
				* Note *
*/

/*
	Function:			CtrlSend( Key, WinTitle, RestoreMode, DetectHiddenWindows )
	Description:		Appending "AlwaysOnTop" attribute to active window, and pass key to WinTitle.
	Params:				Key					>		Key / Strings that will be sent.
						WinTitle			>		Window name / title that will be
													receiving keys.
						RestoreMode			>		1 - Minimize WinTitle after Key sent (Default).
													2 - Keep WinTitle in background after Key sent.
													0 - Keep WinTitle active.
						DetectHiddenWindows	>		Off / 0 - Do not use DetectHiddenWindows.
													On / 1 - Use DetectHiddenWindows (Default).
	Example:
						F1::
						CtrlSend( "This is a string ", "ahk_exe notepad.exe", 2, "On" )
						Return
	
	Example (Sending singular key) :
						F2::
						CtrlSend( "%", "ahk_exe notepad.exe", 2, "On" )
						Return
						
	Example (Getting returned error code):
						F3::
						MsgBox, % CtrlSend( "Test String{Enter}Hehe", "ahk_exe notepad.exe", 2, 5 )
						Return
*/

/*
	Returned Error Codes:
		- invalidDHW		>	Passed DetectHiddenWindows invalid.
		- invalidTitle		>	Passed WinTitle is either not existed / hidden from system / invalid.
		- invalidKey		>	Passed Key is either invalid / unknown.
		- invalidRestore	>	Passed RestoreMode is invalid.
		- success			> 	Funtion is successfully executed.
*/

;##################################################################################################################

CtrlSend( Key, WinTitle, RestoreMode := 1, DetectHiddenWindows := 1 ) {
	; DetectHiddenWindows settings.
	If !( DetectHiddenWindows ~= "iAD)0|1|Off|On" ) {
		Return "invalidDHW"
	}
	Else {
	DetectHiddenWindows, % StrReplace( StrReplace( DetectHiddenWindows, 0, "Off" ), 1, "On" )
	}
	; If WinTitle doesn't exist
	If ( !WinExist( WinTitle ) ) {
		Return "invalidTitle"
	}
	
	; Else WinTitle is exist
	Else {
	; Prevent any interaction while processing.
		BlockInput, On
		
	; Add "AlwaysOnTop" attribute to active window.
		WinSet, AlwaysOnTop, On, A
		
	; Takes active window title to variable (will be used to disable "AlwaysOnTop" on the window later).
		WinGetTitle, restoreWindow, A
		
	; Activate the targeted WinTitle.
		WinActivate, % WinTitle
		
	; Send keys to activated WinTitle.
		; If Key param only has 1 character length*, try to send "{Key}" instead of String.
		; *Note: by 1 character length, listed single character key is a-zA-Z0-9~!@#$%^&*()-_=+[{]}\|;:',<.>/?
		; *Note: all single key listed above is sent normally, doesn't need to escaping character.
		; /!\ Note that ` and " keys are not included in the list. /!\
		If (StrLen( Key ) = 1) {
			Send, % "{" . Key . "}"
		}
		
		; If Key is blank / unknown / not recognized character.
		Else If ( Key = "" ) {
			Return "invalidKey"
		}
		
		; Else if Passed Key is literal string (Forced Key still able to sent via "{Key}" format).
		; Example:		CtrlSend( "This is a string {Enter}{Esc}", "ahk_exe notepad.exe" )
		Else {
		Send, % Key
		}
		
	; Restoring WinTitle by RestoreMode settings.
		; Minimize the targeted WinTItle to background, and Activating restoreWindow's title (RestoreMode = 1).
		If ( RestoreMode = 1 ) {
			WinMinimize, % WinTitle
			WinActivate, % restoreWindow
		}
		
		; Activating restoreWindow's title, and keep WinTitle opened in background (RestoreMode = 2).
		Else If ( RestoreMode = 2 ) {
			WinActivate, % restoreWindow
		}
		
		; Make WinTitle topmost window, and leaving previously "AlwaysOnTop" window in background (RestoreMode = 0).
		Else If ( RestoreMode = 0 ) {
			WinSet, AlwaysOnTop, Off, % restoreWindow
			WinSet, Top,, % WinTitle
		}
		
		Else {
			Return "invalidRestore"
		}
		
	; Remove "AlwaysOnTop" attribute to stored restoreWindow title.
		WinSet, AlwaysOnTop, Off, % restoreWindow
		
	; Re-enable input and other interaction.
		BlockInput, Off
	}
Return "success"
}
The downsides using this function are:

- Our realtime input to system is paused when the funtion is called (by BlockInput).
- Having multiple AlwaysOnTop windows may cause error (not tested for that until now).
- Very slow, compared to original ControlSend. This funtion tries to do the same job with primitive workaround.
- Inefficient for large process.

Examples
My Creations
IMG2HotString - Send image file easily with your hotstring!
CtrlSend - A small solution for sending keys to window in background that doesn't accept ControlSend's key
ControlProcess

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 60 guests