Window Switching

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
richardyager
Posts: 7
Joined: 31 May 2016, 18:14
Contact:

Window Switching

31 May 2016, 19:00

I want to apologize in advance, I am new to AHK, and my question may be trivial.

To prefix, I am using the latest copy from the website, and have installed the 64 bit version.

I am attempting to open 2 seperate notepads and simply bounce back and forth between them and type numbers. I know this sounds simple, I want to be able to have multiple other notepads running and only switch between the two handled by the script while ignoring the others. Following is the code I thought would accomplish this, but it seems to fail.

My thinking was that As I run the instances of Notepad that I could retain the Process IDs for each then use those to access the specific windows, I was expecting to see two instances of Notepad, one with odd numbers 135 and the other with even numbers 246 displayed. Instead I see two instances of notepad and all numbers 1,2,3,4,5,6 displayed in one and the other blank.

Following is my code, any assistance you can render would be of great help.

Code: Select all

#NoEnv  			; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  			; Enable warnings to assist with detecting common errors.
SendMode Input  		; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  	; Ensures a consistent starting directory.



; This key will create a pair of notepads and type in them seperately.
^d::						; Control D

	Run Notepad.exe, , , Pad1PID		; Create first Notepad 		
	Run Notepad.exe, , , Pad2PID		; Create second Notepad

	
	
	WinWait ahk_pid %Pad1PID%, , 3		; Activate the first Notepad
		Send, 1				; 1

	WinWait ahk_pid %Pad2PID%, , 3		; Activate the second Notepad
		Send, 2				; 2

	WinWait ahk_pid %Pad1PID%, , 3		; Activate the first Notepad
		Send, 3				; 3

	WinWait ahk_pid %Pad2PID%, , 3		; Activate the second Notepad
		Send, 4				; 4

	WinWait ahk_pid %Pad1PID%, , 3		; Activate the first Notepad
		Send, 5				; 5

	WinWait ahk_pid %Pad2PID%, , 3		; Activate the second Notepad
		Send,6				; 6

Return 						; Done
Last edited by richardyager on 31 May 2016, 21:08, edited 1 time in total.
User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

Re: Window Switching

31 May 2016, 20:05

Your code tag is a little broken.

I don't see an issue with your current solution, except for the fact that WinWait does NOT activate a window. See WinActivate. Replace just those commands and you should be good, if I understand what you're trying to do correctly.
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
richardyager
Posts: 7
Joined: 31 May 2016, 18:14
Contact:

Re: Window Switching

31 May 2016, 21:09

Awesome, thank you. i knew it had to be something simple.
richardyager
Posts: 7
Joined: 31 May 2016, 18:14
Contact:

Re: Window Switching

31 May 2016, 21:32

Ok, So I tried what you said, and it sorta works at first it was only putting the 4,6 on one window and the 3,5 on the other skipping the 1 and 2.
So I modified the code to include a WinWaitActive and the new result is... 1,2,4,6 on one window and the 3,5 on the other.

Am I misusing the WinWaitActive command?

Thank you again for your assistance.

Code: Select all

#NoEnv  			; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  			; Enable warnings to assist with detecting common errors.
SendMode Input  		; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  	; Ensures a consistent starting directory.



; This key will create a pair of notepads and type in them seperately.
^d::						; Control D

	Run Notepad.exe, , , Pad1PID		; Create first Notepad 		
	Run Notepad.exe, , , Pad2PID		; Create second Notepad

	
	
	WinActivate ahk_pid %Pad1PID%, , 3		; Activate the first Notepad
		WinWaitActive ahk_pid %Pad1PID%, , 0	; Wait Until It Is The Active Window
		SendInput, 1				; 1

	WinActivate ahk_pid %Pad2PID%, , 3		; Activate the second Notepad
		WinWaitActive ahk_pid %Pad2PID%, ,0	; Wait Until It Is The Active Window
		SendInput, 2				; 2

	WinActivate ahk_pid %Pad1PID%, , 3		; Activate the first Notepad
		WinWaitActive ahk_pid %Pad1PID%, , 0	; Wait Until It Is The Active Window
		SendInput, 3				; 3

	WinActivate ahk_pid %Pad2PID%, , 3		; Activate the second Notepad
		WinWaitActive ahk_pid %Pad2PID%, , 0	; Wait Until It Is The Active Window
		SendInput, 4				; 4

	WinActivate ahk_pid %Pad1PID%, , 3		; Activate the first Notepad
		WinWaitActive ahk_pid %Pad1PID%, , 0	; Wait Until It Is The Active Window
		SendInput, 5				; 5

	WinActivate ahk_pid %Pad2PID%, , 3		; Activate the second Notepad
		WinWaitActive ahk_pid %Pad2PID%, , 0	; Wait Until It Is The Active Window
		SendInput,6				; 6

Return 						; Done
richardyager
Posts: 7
Joined: 31 May 2016, 18:14
Contact:

Re: Window Switching

31 May 2016, 21:44

Here is a simplified version of the code, but It is still getting the same result.

Code: Select all

#NoEnv  			; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  			; Enable warnings to assist with detecting common errors.
SendMode Input  		; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  	; Ensures a consistent starting directory.



; This function will send the supplied text to the specified window.
SendToPID( PID, Text ) {
	WinActivate ahk_pid %PID%, , 3			; Activate the Window Specified by the PID
		WinWaitActive ahk_pid %PID%, , 0	; Wait Until It Is The Active Window
		SendInput, %Text%			; Send the Supplied text
}


; This key will create a pair of notepads and type in them seperately.
^d::						; Control D

	Run Notepad.exe, , , Pad1PID		; Create first Notepad 		
	Run Notepad.exe, , , Pad2PID		; Create second Notepad

	SendToPID(Pad1PID,1)			; Send 1 to Pad 1
	SendToPID(Pad2PID,2)			; Send 2 to Pad 2
	SendToPID(Pad1PID,3)			; Send 3 to Pad 1
	SendToPID(Pad2PID,4)			; Send 4 to Pad 2
	SendToPID(Pad1PID,5)			; Send 5 to Pad 1
	SendToPID(Pad2PID,6)			; Send 6 to Pad 2

Return 						; Done
User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

Re: Window Switching

01 Jun 2016, 01:59

I can reproduce your problem, and I'm not entirely sure why (didn't investigate very thoroughly), but I instead wrote you another working function. This does not rely on the window(s) to be active.

Code: Select all

#NoEnv  			; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  			; Enable warnings to assist with detecting common errors.
SendMode Input  		; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  	; Ensures a consistent starting directory.



; This function will send the supplied text to the specified window.
SendToPID( PID, Text ) {
	WinActivate ahk_pid %PID%, , 3			; Activate the Window Specified by the PID
		WinWaitActive ahk_pid %PID%, , 0	; Wait Until It Is The Active Window
		SendInput, %Text%			; Send the Supplied text
    sleep 1000
}
sendToPIDControl(pid,text,ctrlName=""){
    controlSend,% ctrlName,% text,% "ahk_pid " pid
}

; This key will create a pair of notepads and type in them seperately.
^d::						; Control D

	Run Notepad.exe, , , Pad1PID		; Create first Notepad 		
	Run Notepad.exe, , , Pad2PID		; Create second Notepad

    sendToPIDControl(Pad1PID,1,"Edit1")
    sendToPIDControl(Pad2PID,2,"Edit1")
    sendToPIDControl(Pad1PID,3,"Edit1")
    sendToPIDControl(Pad2PID,4,"Edit1")
    sendToPIDControl(Pad1PID,5,"Edit1")
    sendToPIDControl(Pad2PID,6,"Edit1")
/*
	SendToPID(Pad1PID,1)			; Send 1 to Pad 1
	SendToPID(Pad2PID,2)			; Send 2 to Pad 2
	SendToPID(Pad1PID,3)			; Send 3 to Pad 1
	SendToPID(Pad2PID,4)			; Send 4 to Pad 2
	SendToPID(Pad1PID,5)			; Send 5 to Pad 1
	SendToPID(Pad2PID,6)			; Send 6 to Pad 2
*/ 
Return 						; Done
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
User avatar
waetherman
Posts: 112
Joined: 05 Feb 2016, 17:00

Re: Window Switching

01 Jun 2016, 06:45

I'd also like to know the answer! I had a hard time with activating Windows, so I invented my own brute methods like this one:

Code: Select all

#NoEnv  			; Recommended for performance and compatibility with future AutoHotkey releases.
#WinActivateForce
; #Warn  			; Enable warnings to assist with detecting common errors.
SendMode Input  		; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  	; Ensures a consistent starting directory.

; ====================

SetWinDelay, 10 ;DEFAULT IS 100 SO BE CAREFUL!

; ====================

; This function will send the supplied text to the specified window.
SendToPID( PID, Text ) {
	
; ================

	Loop, 200 {
		WinActivate ahk_pid %PID%,
		Sleep, 10
		WinGet, nextWindow, PID, A
		if ( nextWindow = PID ) {
			break
		}
	} 
	
; ===================

	SendInput, %Text%			; Send the Supplied text
}
 
 
; This key will create a pair of notepads and type in them seperately.
^d::						; Control D
 
	Run Notepad.exe, , , Pad1PID		; Create first Notepad 		
	Run Notepad.exe, , , Pad2PID		; Create second Notepad
 
	SendToPID(Pad1PID,1)			; Send 1 to Pad 1
	SendToPID(Pad2PID,2)			; Send 2 to Pad 2
	SendToPID(Pad1PID,3)			; Send 3 to Pad 1
	SendToPID(Pad2PID,4)			; Send 4 to Pad 2
	SendToPID(Pad1PID,5)			; Send 5 to Pad 1
	SendToPID(Pad2PID,6)			; Send 6 to Pad 2
 
Return 						; Done
Edit: oh, that Loop, 200 is stupid, I just use a little bit bigger function which increases the delay geometrically and has a given timeout (usually < 10 seconds). I don't know what is the best way - is it good to spam winActivates, or should you give them time (even after they ended maybe an activated window needs some time, and so you should wait with another winActivate).
Image
richardyager
Posts: 7
Joined: 31 May 2016, 18:14
Contact:

Re: Window Switching

01 Jun 2016, 19:23

I tried both of your solution and I am getting nothing, only the two notepads run but no next at all sent to them. :shock:

Little More information I am running Windows 10.
User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

Re: Window Switching

01 Jun 2016, 19:44

richardyager wrote:Little More information I am running Windows 10.
And there's where you've lost my help; my code works fine on Windows 7, but I don't have Windows 10, or access to it currently.
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
User avatar
waetherman
Posts: 112
Joined: 05 Feb 2016, 17:00

Re: Window Switching

02 Jun 2016, 02:52

Weird, I have Win10 too. Try this:

Code: Select all

#NoEnv  			; Recommended for performance and compatibility with future AutoHotkey releases.
#WinActivateForce
; #Warn  			; Enable warnings to assist with detecting common errors.
SendMode Input  		; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  	; Ensures a consistent starting directory
 
; This function will send the supplied text to the specified window.
SendToHWND( HWND, Text ) {
	DllCall("SetForegroundWindow", UInt, HWND) 
	Sleep, 100
	SendInput, %Text%			; Send the Supplied text
}
 
 
; This key will create a pair of notepads and type in them seperately.
^d::						; Control D
 
	Run Notepad.exe, , , Pad1PID		; Create first Notepad 		
	Run Notepad.exe, , , Pad2PID		; Create second Notepad
	WinWait ahk_pid %Pad1PID%
	Winget Pad1HWND, ID, ahk_pid %Pad1PID%
	WinWait ahk_pid %Pad2PID%
	WinGet Pad2HWND, ID, ahk_pid %Pad2PID%

 
	SendToHWND(Pad1HWND,1)			; Send 1 to Pad 1
	SendToHWND(Pad2HWND,2)			; Send 2 to Pad 2
	SendToHWND(Pad1HWND,3)			; Send 3 to Pad 1
	SendToHWND(Pad2HWND,4)			; Send 4 to Pad 2
	SendToHWND(Pad1HWND,5)			; Send 5 to Pad 1
	SendToHWND(Pad2HWND,6)			; Send 6 to Pad 2
	return
Image
richardyager
Posts: 7
Joined: 31 May 2016, 18:14
Contact:

Re: Window Switching

02 Jun 2016, 08:33

Yeah, Same thing. Still Not working. Opens 2 Notepads no text... Very strange. I've tried this on 2 pc one is a desktop running Windows 10 on an i7 and the other is a microsoft surface also running windows 10 on an i7.

:wtf:
richardyager
Posts: 7
Joined: 31 May 2016, 18:14
Contact:

Re: Window Switching

03 Jun 2016, 10:45

Could this be a 64bit issue? Should I be using the 32 bit version?
User avatar
waetherman
Posts: 112
Joined: 05 Feb 2016, 17:00

Re: Window Switching

03 Jun 2016, 11:17

I have Windows 10 64 bit.

Autohotkey 1.1.22.09
I don't remember if it's 32 or 64 bit version.
Image

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Descolada, Mateusz53, peter_ahk, Rohwedder and 183 guests