Jump to content

How can I send keystrokes to an inactive window ?


Recommended Posts

Hello everyone, I'm trying to send the keystroke to an inactive notepad windows while the another one is activating. 

The problem that I found is word "Hello World" can't type in the inactive window, after I pressed the "Home" button while the cursor is staying in the active window.

 

 

Here is my code. I'm trying with 2 windows of Notepad, using ControlSend() and WinGetHandle().

HotKeySet("{PGUP}", "NotepadHWND")
HotKeySet("{HOME}", "Sim")
HotKeySet("{DEL}", "PrntResult")

Global $VAR_NotepadHWND

While 1
    Sleep(50)
WEnd

Func NotepadHWND()
    $VAR_NotepadHWND = WinGetHandle("[ACTIVE]")
EndFunc

Func Sim()
    Return ControlSend($VAR_NotepadHWND, "", "", "Hello World !!")
EndFunc

Func PrntResult()
    MsgBox(0, "","HWND is :" & $VAR_NotepadHWND)
EndFunc

Thank you everyone for sharing your solution and knowledge. 🤗

Capture.PNG

Link to post
Share on other sites
8 hours ago, Nine said:

LoL: inactive notepad !  Ok, how about you tell us what application you are trying to automate, and what is the goal of your issue...

I'm newbie for autoit and I just want to try this function with multiple copies of an application open which have the same title/text.

My purpose is typing some word to an inactive window while I'm in the active window. Refer to my code, press "PGUP" button to specify the window I want to send word to by using WinGetHandle() to identify. After that press "Home" button to call the function to send a word to inactive window by using ControlSend(). But my code not provides me the thing what I want.

 

I would like to say sorry if my description isn't clear for you. 

Link to post
Share on other sites
16 hours ago, Mochiza said:

Func NotepadHWND()    

$VAR_NotepadHWND = WinGetHandle("[ACTIVE]")

EndFunc

wingethandle("[ACTIVE]") will return current active window handle (can return handle for another window than notebad ) ,so use this code instead

$VAR_NotepadHWND =WinGetHandle("[CLASS:Notepad]")

second , dont forget the Control ID in ControlSend so it should be :-

Func Sim()
    Return ControlSend($VAR_NotepadHWND, "", "Edit1"   , "Hello World !!")
                                           ;ControlID
EndFunc

 

Link to post
Share on other sites
2 hours ago, Network_Guy said:

wingethandle("[ACTIVE]") will return current active window handle (can return handle for another window than notebad ) ,so use this code instead

$VAR_NotepadHWND =WinGetHandle("[CLASS:Notepad]")

second , dont forget the Control ID in ControlSend so it should be :-

Func Sim()
    Return ControlSend($VAR_NotepadHWND, "", "Edit1"   , "Hello World !!")
                                           ;ControlID
EndFunc

 

Now I can send the word to inactive window.

I have the question that if I don't know the "ControlID" or any properties of control (Id, ClassNN, ... ), so the word can't be sent to inactive window. Do we have the alternative ways to send the word to inactive window ?

Link to post
Share on other sites

According to the help file: Send - Sends simulated keystrokes to the active window.
To send data to inactive windows you can either try to automate the GUI by using Control* functions or use COM to directly interact with the programs API and avoid the GUI.

It would help if you could tell us which program you REALLY try to automate at the end.

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2021-06-05 - Version 1.5.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (NEW 2021-06-14 - Version 1.6.5.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (NEW 2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to post
Share on other sites
  • Developers
8 hours ago, Mochiza said:

(I try with RO exe)

So this is for game automation?

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...