Using "Sendkeys" if application is already open

In this category you can exchange your programming questions and solutions.
Post Reply
Darren
Posts: 63
Joined: Wed May 11, 2011 11:55 am

Using "Sendkeys" if application is already open

Post: # 729Post Darren
Sun Jul 17, 2011 11:24 am

Does anybody know how to use "sendkeys" if a application is already open.

For example,

Say "Notepad.exe" is already open. Can I use "sendkeys" to send a line of text to notepad?

In reality, I am using AutoCAD, at certain points I need to run an application, written in FnxBasic to send a line of text to AutoCAD, that would be already open and running.

Many thanks in advance!!!

IsoCAM

Marco
Site Admin
Posts: 246
Joined: Sat Sep 15, 2018 8:41 pm

Using "Sendkeys" if application is already open

Post: # 730Post Marco
Sun Jul 17, 2011 8:15 pm

hi here som code to fix that. It are api calls of windows to get the current forground window if notepad is just started. Just before the sendkeys the focus is set to the notepad.
best regards

declare getfoc as "GetForegroundWindow" of user32
  result as integer
end declare

declare setfoc as "SetForegroundWindow" of user32
  hwnd as integer
end declare

object myform as form
  object butt as button
    caption="Start notepad"
    onclick=butt_click
  end object
  object butt2 as button
    top=butt.height
    caption="sendkeys"
    onclick=butt2_click
  end object
end object

myform.showmodal

sub butt_click
  `start notepad
  shell "c:windows
otepad.exe"
  `wait until it runs
  sleep 500
  `get handle
  getfoc.execute
  `put handle in setforeground window
  setfoc.hwnd=getfoc.result
end sub

sub butt2_click
  `set forgroundwindow
  setfoc.execute
  sendkeys "Hello world"
end sub

Darren
Posts: 63
Joined: Wed May 11, 2011 11:55 am

Using "Sendkeys" if application is already open

Post: # 731Post Darren
Wed Jul 20, 2011 11:19 am

Many thanks for your reply!!!

Please can I ask another question?

If I have several applications open at once, how can I send the sendkeys string to the application that currently has the focus?

For eample, Say I have "Notepad", "Wordpad" & "AutoCAD" open. I have a menu item inside AutoCAD that can run an external application (exe file). I want to run a FnxBasic compiled application (exe file) from inside AutoCAD.

Everything at this stage works OK. All I need, is to use the "Sendkeys" function to send a line of text to AutoCAD. So I need to set the focus to AutoCAD every time the FnxBasic appication is run.

Can you please tell me if this is possible?

Kindest Regards

Darren

Marco
Site Admin
Posts: 246
Joined: Sat Sep 15, 2018 8:41 pm

Using "Sendkeys" if application is already open

Post: # 732Post Marco
Thu Jul 21, 2011 3:25 pm

hi, i will explain it,

just dim a few integers to store the handles in.
ie
dim notepadhandle as integer
dim word handle as integer

then start applications
ie
shell notepad.exe
sleep 500
getfoc.execute
notepadhandle=getfoc.result
shell word.exe
sleep 500
getfoc.execute``get foregroundwindow
wordhandle=getfoc.result``set result into var

now you`ve got the windows handle to set the focus if you want to sendkeys to it,
ie
sendkeys to notepad
setfoc.hwnd=notepadhandle
setfoc.execute
sendkeys
and
sendkeys to word
setfoc.hwnd=wordhandle
setfoc.execute
sendkeys


i hope this will help
best regards marco

Post Reply