controlsend to notepad2 not sending text

Get help with using AutoHotkey and its commands and hotkeys
enthused
Posts: 94
Joined: 27 Dec 2014, 03:28

controlsend to notepad2 not sending text

12 Feb 2015, 01:43

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
User avatar
boiler
Posts: 2911
Joined: 21 Dec 2014, 02:44

Re: controlsend to notepad2 not sending text

12 Feb 2015, 02:07

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.
enthused
Posts: 94
Joined: 27 Dec 2014, 03:28

Re: controlsend to notepad2 not sending text

12 Feb 2015, 08:31

nice :)
worked beautifully except as you mentioned the case of the letters changed. But it's a start..
Thanks boiler.
User avatar
boiler
Posts: 2911
Joined: 21 Dec 2014, 02:44

Re: controlsend to notepad2 not sending text

12 Feb 2015, 08:36

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).
enthused
Posts: 94
Joined: 27 Dec 2014, 03:28

Re: controlsend to notepad2 not sending text

12 Feb 2015, 09:01

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"
[email protected]; second send note lower "T" and "@" instead of "2"
tEST! ; third send consistent
[email protected] ; third send consistent
tEST! ; fourth
[email protected] ; fourth
tEST! ; fifth
[email protected] ; fifth
enthused
Posts: 94
Joined: 27 Dec 2014, 03:28

Re: controlsend to notepad2 not sending text

13 Feb 2015, 01:13

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
RHCP
Posts: 191
Joined: 30 Sep 2013, 10:59

Re: controlsend to notepad2 not sending text

13 Feb 2015, 01:53

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  
}
garry
Posts: 1871
Joined: 22 Dec 2013, 12:50

Re: controlsend to notepad2 not sending text

13 Feb 2015, 07:21

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"
enthused
Posts: 94
Joined: 27 Dec 2014, 03:28

Re: controlsend to notepad2 not sending text

14 Feb 2015, 00:24

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.
User avatar
boiler
Posts: 2911
Joined: 21 Dec 2014, 02:44

Re: controlsend to notepad2 not sending text

14 Feb 2015, 01:25

Thanks for the insight, guys.

Return to “Ask For Help”

Who is online

Users browsing this forum: AHKStudent, Crow Of Shadow, doctorafterman, Rohwedder and 240 guests