找回密码
 加入
搜索
查看: 4178|回复: 4

如何向一个后台窗口发送按键信息

[复制链接]
发表于 2008-7-22 17:52:45 | 显示全部楼层 |阅读模式
按键精灵已经提供了这样的plugin,autoit里面有这样的函数吗?
发表于 2009-5-11 23:49:55 | 显示全部楼层
我没发现有
发表于 2009-5-14 17:02:01 | 显示全部楼层
postmessage
发表于 2009-5-14 17:38:35 | 显示全部楼层
奉上自制超简陋版_WinSendKey,权作抛砖引玉。
这个其实是因为某一天想办法刷新桌面时想出的最笨办法:向桌面发送按键F5。后来学会了SHChangeNotify就没用上。
;===============================================================================
; 说明:   向指定窗口发送虚拟按键消息
; 语法:   _WinSendKey($sTitle, $sText, $cKey)
; 参数:   $sTitle     - 窗口标题或句柄
;         $sText      - 窗口包含文字
;         $cKey       - 按键
; 需要:   无
; 返回:   成功 - 1
;         失败 - 0, 并设置 @error 到
;            1 - 窗口不存在
;            2 - $cKey 无效
;            3 - API 操作失败, 设置 @extended 到返回值
; 备注:   按键暂时只支持 A-Z, 0-9, F1-F12, ENTER, ESCAPE, TAB 及光标键, 并且不支持组合键。格式同Send()
;===============================================================================
Func _WinSendKey($sTitle, $sText, $cKey)
        Local $hWnd, $iKey, $aResult, $aError
        Local Const $WM_KEYDOWN = 0x0100
        Local Const $WM_KEYUP = 0x0101

        If IsHWnd($sTitle) Then
                $hWnd = $sTitle
        Else
                $hWnd = WinGetHandle($sTitle, $sText)
        EndIf
        If $hWnd = "" OR NOT WinExists($hWnd) Then Return SetError(1, 0, 0)

        $cKey = StringUpper($cKey)
        Switch $cKey
                Case "{F1}"
                        $iKey = 0x0070
                Case "{F2}"
                        $iKey = 0x0071
                Case "{F3}"
                        $iKey = 0x0072
                Case "{F4}"
                        $iKey = 0x0073
                Case "{F5}"
                        $iKey = 0x0074
                Case "{F6}"
                        $iKey = 0x0075
                Case "{F7}"
                        $iKey = 0x0076
                Case "{F8}"
                        $iKey = 0x0077
                Case "{F9}"
                        $iKey = 0x0078
                Case "{F10}"
                        $iKey = 0x0079
                Case "{F11}"
                        $iKey = 0x007A
                Case "{F12}"
                        $iKey = 0x007B
                Case "{ENTER}"
                        $iKey = 0x000D
                Case "{ESC}", "{ESCAPE}"
                        $iKey = 0x001B
                Case "{TAB}"
                        $iKey = 0x0009
                Case "{UP}"
                        $iKey = 0x0026
                Case "{DOWN}"
                        $iKey = 0x0028
                Case "{LEFT}"
                        $iKey = 0x0025
                Case "{RIGHT}"
                        $iKey = 0x0027
                Case Else
                        $iKey = Asc($cKey)
                        Switch $iKey
                                Case 48 To 57, 65 To 90
                                Case Else
                                        Return SetError(2, 0, 0)
                        EndSwitch
        EndSwitch

        $aResult = DllCall("user32.dll", "int", "PostMessage", "hwnd", $hWnd, "uint", $WM_KEYDOWN, "wparam", $iKey, "lparam", 0)
        $aError = DllCall("kernel32.dll", "int", "GetLastError")
        If $aResult[0] <> 0 AND WinExists($hWnd) Then _
                        DllCall("user32.dll", "int", "PostMessage", "hwnd", $hWnd, "uint", $WM_KEYUP, "wparam", $iKey, "lparam", 0)

        If $aResult[0] <> 0 Then
                Return 1
        Else
                Return SetError(3, $aError[0], 0)
        EndIf
EndFunc   ;==>_WinSendKey
发表于 2009-8-30 23:47:55 | 显示全部楼层
不错的东东收下了
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-9-22 21:35 , Processed in 0.073736 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表