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
}