将文本从AutoIt表单复制到记事本

时间:2014-07-27 08:03:56

标签: windows autoit notepad

我正在使用此AutoIt代码,只需按一下按钮即可将文本发送到记事本:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
$Form = GUICreate("Replicate text to notepad", 615, 50, 190, 122)
$Input = GUICtrlCreateInput("Placeholder text", 0, 0, 609, 21)
$Button = GUICtrlCreateButton("Send to notepad", 0, 24, 609, 25)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button
            Example(GUICtrlRead($Input))

    EndSwitch
WEnd

Func Example($text)

    Run("notepad.exe")

    Local $hWnd = WinWait("[CLASS:Notepad]", "", 10)

    ControlSend($hWnd, "", "Edit1", $text)

EndFunc

工作得很好。但是现在我想按下它们时发送按键。在AutoIt中有类似OnKeyDown的东西吗?因此,每次输入字符时,我都不必按发送按钮将其发送到记事本。

1 个答案:

答案 0 :(得分:1)

我就是这样做的。 工作很整洁!

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>


Run("notepad.exe")
Global $hWnd = WinWait("[CLASS:Notepad]", "", 10)

$Form = GUICreate("Replicate text to notepad", 615, 50, 190, 122)
$Input = GUICtrlCreateInput("Placeholder text", 0, 0, 609, 21)
$Button = GUICtrlCreateButton("Send to notepad", 0, 24, 609, 25)
GUISetState(@SW_SHOW)

$OldText = ""

While 1
    $nMsg = GUIGetMsg()

    $NewText = GUICtrlRead($Input)
    If $OldText <> $NewText Then
        $OldText = $NewText
        Example($NewText)
    EndIf

    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button
            Example(GUICtrlRead($Input))

    EndSwitch
WEnd

Func Example($text)
    ControlSetText($hWnd, "", "Edit1", $text)
EndFunc