Sign in to follow this  
Followers 0
igotandrew

Send Keys to Minimized Window

6 posts in this topic

So I heard that ControlSend() messes up some characters with shift: !@#$%^&*(

And I found this source:

Global Const $VK_OEM_PLUS = 0xBB
Global Const $VK_OEM_MINUS = 0xBD
Global Const $VK_OEM_3 = 0xC0
Global Const $VK_TAB = 0x9
Global Const $VK_ESC = 0x1B
Global Const $VK_F5 = 0x74
Global Const $VK_F12 = 0x7B


Dim $hWnd = WingetHandle("Untitled - Notepad")


_SendKeys($hWnd, "123123123"); using send keys





Func _MakeLong($LoWord, $HiWord)
    Return BitOR($HiWord * 0x10000, BitAND($LoWord, 0xFFFF))
EndFunc
Func _MouseClick($hWnd, $button, $x, $y, $times = 1, $delay = 0)
    Local $ret, $ix
    $ret = DllCall("user32.dll", "int", "PostMessage", "hwnd", $hWnd, "int", 0x200, "int", 0, "long", _MakeLong($x, $y))
    If $ret[0] = 0 Then
        SetError(-1)
        Return
    EndIf
    $button = StringLower($button)
    If $button = "left" Then
        For $ix = 1 To $times
            $ret = DllCall("user32.dll", "int", "PostMessage", "hwnd", $hWnd, "int", 0x201, "int", 1, "long", _MakeLong($x, $y))
            If $ret[0] = 0 Then
                SetError(-2)
                Return
            Else
                $ret = DllCall("user32.dll", "int", "PostMessage", "hwnd", $hWnd, "int", 0x202, "int", 0, "long", _MakeLong($x, $y))
                If $ret[0] = 0 Then
                    SetError(-3)
                    Return
                EndIf
            EndIf
            Sleep($delay)
        Next
    ElseIf $button = "right" Then
        For $ix = 1 To $times
            $ret = DllCall("user32.dll", "int", "PostMessage", "hwnd", $hWnd, "int", 0x204, "int", 2, "long", _MakeLong($x, $y))
            If $ret[0] = 0 Then
                SetError(-4)
                Return
            Else
                $ret = DllCall("user32.dll", "int", "PostMessage", "hwnd", $hWnd, "int", 0x205, "int", 0, "long", _MakeLong($x, $y))
                If $ret[0] = 0 Then
                    SetError(-5)
                    Return
                EndIf
            EndIf
            Sleep($delay)
        Next
    Else
        SetError(-6)
        Return
    EndIf
EndFunc

Func _SendKeys($hWnd, $keys)
    If $hWnd <= 0 Or StringLen($keys) = 0 Then
        SetError(-1)
        Return False
    EndIf
    $keys = StringUpper($keys)
    $keys = StringReplace($keys, "`", Chr($VK_OEM_3))
    $keys = StringReplace($keys, "~", Chr($VK_OEM_3))
    $keys = StringReplace($keys, "-", Chr($VK_OEM_MINUS))
    $keys = StringReplace($keys, "=", Chr($VK_OEM_PLUS))
    $keys = StringReplace($keys, "{ENTER}", Chr(0xD))
    $keys = StringReplace($keys, "{TAB}", Chr(0x9))
    $keys = StringReplace($keys, "{ESC}", Chr($VK_ESC))
    $keys = StringReplace($keys, "{F5}", Chr($VK_F5))
    $keys = StringReplace($keys, "{F12}", Chr($VK_F12))
    $keys = StringReplace($keys, "{SHIFT}", "+")
    Local $i, $ret
    Local $shiftdown = False
    For $i = 1 To StringLen($keys)
        If StringMid($keys, $i, 1) = "+" Then
            DllCall("user32.dll", "int", "PostMessage", "hwnd", $hWnd, "int", 0x100, "int", 0x10, "long", 0x002A0001)
            DllCall("user32.dll", "int", "PostMessage", "hwnd", $hWnd, "int", 0x100, "int", 0x10, "long", 0x402A0001)
            $shiftdown = True
            Sleep(1)
            ContinueLoop
        Else
            $ret = DllCall("user32.dll", "int", "MapVirtualKey", "int", Asc(StringMid($keys, $i, 1)), "int", 0)
            If IsArray($ret) Then
                DllCall("user32.dll", "int", "PostMessage", "hwnd", $hWnd, "int", 0x100, "int", Asc(StringMid($keys, $i, 1)), "long", _MakeLong(1, $ret[0]))
                Sleep(1)
                DllCall("user32.dll", "int", "PostMessage", "hwnd", $hWnd, "int", 0x101, "int", Asc(StringMid($keys, $i, 1)), "long", _MakeLong(1, $ret[0]) + 0xC0000000)
            EndIf
        EndIf
        If $shiftdown Then
            Sleep(1)
            DllCall("user32.dll", "int", "PostMessage", "hwnd", $hWnd, "int", 0x101, "int", 0x10, "long", 0xC02A0001)
            $shiftdown = False
        EndIf
    Next
    Return True
EndFunc

Func _ArrowKey($hWnd, $key)
    If $hWnd <= 0 Or ($key <> "left" And $key <> "right" And $key <> "up" And $key <> "down") Then
        SetError(-1)
        Return
    EndIf
    Local $wParam, $lParam, $ret
    If $key = "left" Then
        $wParam = 0x25
        $lParam = 0x14B0001
    ElseIf $key = "right" Then
        $wParam = 0x27
        $lParam = 0x14D0001
    ElseIf $key = "down" Then
        $wParam = 0x28
        $lParam = 0x1500001
    ElseIf $key = "up" Then
        $wParam = 0x26
        $lParam = 0x1480001
    EndIf
    $ret = DllCall("user32.dll", "int", "PostMessage", "hwnd", $hWnd, "int", 0x100, "int", $wParam, "int", $lParam)
    If $ret[0] = 0 Then
        MsgBox(16, "_ArrowKey Error", "There was an error posting the WM_KEYDOWN message")
        SetError(-2)
        Return
    EndIf
    Sleep(2)
    $ret = DllCall("user32.dll", "int", "PostMessage", "hwnd", $hWnd, "int", 0x101, "int", $wParam, "int", ($lParam + 0xC0000000))
    If $ret[0] = 0 Then
        MsgBox(16, "_ArrowKey Error", "There was an error posting the WM_KEYUP message")
        SetError(-3)
        Return
    EndIf
EndFunc

AU3 Coding isn't working when I click the button.

And it doesn't seem to send the keys.

Any ideas?

Share this post


Link to post
Share on other sites



No one knows?

Share this post


Link to post
Share on other sites

No one knows?

No one knows that ControlSend() messes up characters. Where is your demo of the problem? Send() and ControlSend() by default use some special characters to signal ALT, CTL, Shift, etc. If you didn't know that, read the help file entry for Send() and the same key syntax applies to ControlSend().

To send those special characters "raw" (without interpreting for meta-keys) use the raw flag. This works minimized as well as with the active window:

$sTitle = "Untitled - Notepad"
$PID = Run("notepad.exe")
WinWait($sTitle)
$hWin = WinGetHandle($sTitle)

; Normal window
ControlSend($hWin, "", "Edit1", "Sent with raw flag:  !@#$%^&*()" & @CRLF, 1)
ControlSend($hWin, "", "Edit1", "Send with shift:  +1+2+3+4+5+6+7+8+9+0" & @CRLF)

; Minimized window
WinSetState($hWin, "", @SW_MINIMIZE)
ControlSend($hWin, "", "Edit1", "Sent with raw flag:  !@#$%^&*()" & @CRLF, 1)
ControlSend($hWin, "", "Edit1", "Send with shift:  +1+2+3+4+5+6+7+8+9+0" & @CRLF)

; Show result
WinSetState($hWin, "", @SW_RESTORE)

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

No one knows that ControlSend() messes up characters. Where is your demo of the problem? Send() and ControlSend() by default use some special characters to signal ALT, CTL, Shift, etc. If you didn't know that, read the help file entry for Send() and the same key syntax applies to ControlSend().

To send those special characters "raw" (without interpreting for meta-keys) use the raw flag. This works minimized as well as with the active window:

$sTitle = "Untitled - Notepad"
$PID = Run("notepad.exe")
WinWait($sTitle)
$hWin = WinGetHandle($sTitle)

; Normal window
ControlSend($hWin, "", "Edit1", "Sent with raw flag:  !@#$%^&*()" & @CRLF, 1)
ControlSend($hWin, "", "Edit1", "Send with shift:  +1+2+3+4+5+6+7+8+9+0" & @CRLF)

; Minimized window
WinSetState($hWin, "", @SW_MINIMIZE)
ControlSend($hWin, "", "Edit1", "Sent with raw flag:  !@#$%^&*()" & @CRLF, 1)
ControlSend($hWin, "", "Edit1", "Send with shift:  +1+2+3+4+5+6+7+8+9+0" & @CRLF)

; Show result
WinSetState($hWin, "", @SW_RESTORE)

:)

Thanks, again you come to my rescue ;)

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

No one knows that ControlSend() messes up characters. Where is your demo of the problem? Send() and ControlSend() by default use some special characters to signal ALT, CTL, Shift, etc. If you didn't know that, read the help file entry for Send() and the same key syntax applies to ControlSend().

To send those special characters "raw" (without interpreting for meta-keys) use the raw flag. This works minimized as well as with the active window:

$sTitle = "Untitled - Notepad"
$PID = Run("notepad.exe")
WinWait($sTitle)
$hWin = WinGetHandle($sTitle)

; Normal window
ControlSend($hWin, "", "Edit1", "Sent with raw flag:  !@#$%^&*()" & @CRLF, 1)
ControlSend($hWin, "", "Edit1", "Send with shift:  +1+2+3+4+5+6+7+8+9+0" & @CRLF)

; Minimized window
WinSetState($hWin, "", @SW_MINIMIZE)
ControlSend($hWin, "", "Edit1", "Sent with raw flag:  !@#$%^&*()" & @CRLF, 1)
ControlSend($hWin, "", "Edit1", "Send with shift:  +1+2+3+4+5+6+7+8+9+0" & @CRLF)

; Show result
WinSetState($hWin, "", @SW_RESTORE)

:)

I know Edit1 is classnameNN. But how can I do like this when I have only handle value (without controlID and classnameNN) Edited by Fabroni

Share this post


Link to post
Share on other sites

I know Edit1 is classnameNN. But how can I do like this when I have only handle value (without controlID and classnameNN)

Just supply the handle instead of the ClassNameNN:
$PID = Run("notepad.exe")
WinWait("Untitled - Notepad")
$hWin = WinGetHandle("Untitled - Notepad")
$hEdit1 = ControlGetHandle($hWin, "", "Edit1")
ControlSend($hWin, "", "Edit1", "Sent by ClassNameNN." & @CRLF)
ControlSend($hWin, "", $hEdit1, "Sent by control handle." & @CRLF)

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


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
Sign in to follow this  
Followers 0