Page 1 of 1

controlsend to notepad2 not sending text

Posted: 12 Feb 2015, 01:43
by enthused
I was trying a script to send text to notepad2 but not sending anything.
It does launch notepad (notepad2.exe on my system) but no text is sent.
My question is what needs to be changed in the script?

Code: Select all

#singleinstance,force
pr=notepad
sc=ahk_exe Notepad2.exe
ClassNN=SysListView321
Gui,2: Add, Button, x10 y60 h25 w160 gA1,Write to Notepad
Gui,2: Show,x10 y10 h90 w200,Run_Notepad
Gui,2: Add, Edit, x10 y10 h40 w180 vE1,%a_space%TEST1`n%a_space%TEST2`n
return
2Guiclose:
exitapp

A1:
Gui,2: Submit, nohide
IfWinNotExist,%sc%
  {
  Run, %pr%,,,pid1
   WinWait,%SC%
   IfWinNotActive ,%SC%,,WinActivate,%SC%
      WinWaitActive,%SC%
  }
ControlSend,%classnn%,%e1%,%sc%
return

Re: controlsend to notepad2 not sending text

Posted: 12 Feb 2015, 02:07
by boiler
Seems to work better if you take the leave the control parameter blank in the ControlSend statement, so you're sending it to the Window instead of the control. It seems to mess up some characters, though. Like a 1 will become a ! and a T will become t, and stuff like that.

Re: controlsend to notepad2 not sending text

Posted: 12 Feb 2015, 08:31
by enthused
nice :)
worked beautifully except as you mentioned the case of the letters changed. But it's a start..
Thanks boiler.

Re: controlsend to notepad2 not sending text

Posted: 12 Feb 2015, 08:36
by boiler
Sure. If you happen to resolve the letters changing, please post how you did that. I noticed that before when I sent text to notepad (the regular notepad).

Re: controlsend to notepad2 not sending text

Posted: 12 Feb 2015, 09:01
by enthused
So far it looks like an issue with ControlSend and ControlSendRaw where it adds extra shift to what is being sent. It is consistent after the first send which is good.
TEST1 ; first send good
TEST2 ; first send good
tEST! ; second send note lower "T" and "!" instead of "1"
tEST@; second send note lower "T" and "@" instead of "2"
tEST! ; third send consistent
tEST@ ; third send consistent
tEST! ; fourth
tEST@ ; fourth
tEST! ; fifth
tEST@ ; fifth

Re: controlsend to notepad2 not sending text

Posted: 13 Feb 2015, 01:13
by enthused
boiler wrote:Sure. If you happen to resolve the letters changing, please post how you did that. I noticed that before when I sent text to notepad (the regular notepad).
Use SetKeyDelay to send Keystrokes:

Code: Select all

#singleinstance,force
pr=notepad
sc=ahk_exe Notepad2.exe
ClassNN=SysListView321
Gui,2: Add, Button, x10 y60 h25 w160 gA1,Write to Notepad
Gui,2: Show,x10 y10 h90 w200,Run_Notepad
Gui,2: Add, Edit, x10 y10 h40 w180 vE1, TEST1 `nTEST2 `n
return
2Guiclose:
exitapp

A1:
Gui,2: Submit, nohide
IfWinNotExist,%sc%
  {
  Run, %pr%,,,pid1
   WinWait,%SC%
   IfWinNotActive ,%SC%,,WinActivate,%SC%
      WinWaitActive,%SC%
  }
;SetKeyDelay, 10, 10 ; this will work
SetKeyDelay, 0, 10  ; so will this

ControlSend,,%e1%,%sc%
return
EDIT:
Credit to Lexikos explaining this:
http://ahkscript.org/boards/viewtopic.php?f=14&t=6389

Re: controlsend to notepad2 not sending text

Posted: 13 Feb 2015, 01:53
by RHCP
This has come up a few times before. Just to add to what Lexikos wrote, the issue is due to the fact that controlSend uses both post message and sendInput/sendEvent to send keystrokes. This is required as some programs will not correctly interpret the keystrokes unless the sent modifier keys are logically down (sent via sendInput/Event).

Modifier keys (unless explicitly stated e.g. {shitft down}) are sent via sendInput while non-modifers are sent via postMessage. Keys sent via postmessage are sent directly to the window and so have less delay than the other keys/messages. Consequently it's possible for keystroke messages to arrive out of synch resulting in unexpected characters. In your case the capitalised letters require the shift key to be sent via sendInput/Event.

In addition to using keyDelays, you can try controlSetText and posting WM_Char messages.
If you're working with a text control i would recommend using controlSetText.

Code: Select all

#singleinstance,force
pr=notepad
sc=ahk_exe Notepad.exe
control := "edit1"
loop, 15 
    s .= "TEST" A_index "`n"
Gui,2: Add, Edit, x10 y10 h180 w180 vE1, %s%
Gui,2: Add, Button, x10 y+10 h25 w160 gA1,Write to Notepad
Gui,2: Add, Button, xp y+5 h25 w160 gSetText, ControlSetText
Gui,2: Add, Button, xp y+5 h25 w160 gsendChars, sendChars
Gui,2: Show,x10 y10, Run_Notepad
return
2Guiclose:
exitapp

winCheck: 
Gui,2: Submit, nohide
IfWinNotExist,%sc%
{
    Run, %pr%,,,pid1
    WinWait,%SC%
}
IfWinNotActive ,%SC%,,WinActivate,%SC%
    WinWaitActive,%SC%
return

A1:
; Play around with key delays
gosub, winCheck
ControlSend,,%e1%,%sc%
return

SetText:
gosub, winCheck
; could use ControlGetText to get the current text, and append it to the new text (so the old text is appended to, rather than replaced)
StringReplace, e1, e1, `n, `r`n, All
controlSetText, %control%, %e1%, %sc%
return 

sendChars:
pSendChars(e1, control, sc)    
return 

pSendChars(string, control := "", WinTitle := "", WinText := "", ExcludeTitle := "", ExcludeText := "")
{
    for k, char in StrSplit(string)
        postmessage, WM_CHAR := 0x102, Asc(char),, %Control%, %WinTitle%, %WinText%, %ExcludeTitle%, %ExcludeText%
    return  
}

Re: controlsend to notepad2 not sending text

Posted: 13 Feb 2015, 07:21
by garry
thank you RHCP , tried with Uedit32
works fine with Controlsettext and SendChars

Code: Select all

pr=C:\Programme\IDM Computer Solutions\UltraEdit-32\uedit32.exe
sc=UltraEdit
control := "EditControl1"

Re: controlsend to notepad2 not sending text

Posted: 14 Feb 2015, 00:24
by enthused
Thanks for the very detailed information RHCP. This made a fine reading.
Post message is very fast indeed. Interestingly, I have no errors with it yet and I haven't used a Keydelay with it.

Re: controlsend to notepad2 not sending text

Posted: 14 Feb 2015, 01:25
by boiler
Thanks for the insight, guys.