Make Macros work on a specific window

Example scripts and tips (replaces Old Scripts & Tips archive)

Moderators: Dorian (MJT support), JRL, Phil Pendlebury

Post Reply
NewUser93
Newbie
Posts: 2
Joined: Fri Sep 25, 2015 11:04 pm

Make Macros work on a specific window

Post by NewUser93 » Fri Sep 25, 2015 11:11 pm

Is it possible to make a macro work only on a specific window and alow me to use the computer freely?
I want the code to press a few keys every 10 sec while i do something else.

hagchr
Automation Wizard
Posts: 327
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: Make Macros work on a specific window

Post by hagchr » Sat Sep 26, 2015 11:53 am

Hi, you could possibly use ObjectSendKeys> which allows you to send keystrokes without the object being in focus. Eg, The following will send text to Notepad even though I am doing something else (Notepad needs to be open). Check the Code Builder Wizard.

Code: Select all

GetWindowHandle>Untitled - Notepad,hWndParent
FindObject>hWndParent,Edit,,1,hWnd,X1,Y1,X2,Y2,result
ObjectSendKeys>hWnd,{"This is a test"}

NewUser93
Newbie
Posts: 2
Joined: Fri Sep 25, 2015 11:04 pm

Re: Make Macros work on a specific window

Post by NewUser93 » Sat Sep 26, 2015 9:32 pm

Will it work with keys like F1, F2, TAB and Shift?
That seems to work with text only

hagchr
Automation Wizard
Posts: 327
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: Make Macros work on a specific window

Post by hagchr » Sun Sep 27, 2015 8:28 am

You can send both, check this link for codes: http://www.mjtnet.com/vkcodes.htm

I modified the previous example. Depending on what/where you send you may need to add some Wait>x (eg after pressing F1 below). As you see you can send one or more items (comma separated) per ObjectSendKeys> - you can play with it and see what works best in your case.

Code: Select all

GetWindowHandle>Untitled - Notepad,hWndParent
FindObject>hWndParent,Edit,,1,hWnd,X1,Y1,X2,Y2,result

//Text Keys
Let>Mess1={"This is text 1"}
Let>Mess2={"Text 2"}
Let>Mess3={"The End!"}

//Other Keys
Let>strTab=VK9
Let>strReturn=VK13
Let>strF1=VK112

ObjectSendKeys>hWnd,Mess1
ObjectSendKeys>hWnd,strTab,strTab
ObjectSendKeys>hWnd,Mess2
ObjectSendKeys>hWnd,strReturn,strReturn
ObjectSendKeys>hWnd,Mess3,strReturn
ObjectSendKeys>hWnd,strF1

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts