RSS订阅追求最高效、最简单的解决方案
你的位置:首页 » autoit3研究 » 正文

[UDF]简易后台按键、鼠标、发送字符

选择字号: 超大 标准 技术小哥 发布于2016年08月02日 属于 autoit3研究 栏目  0个评论 31538人浏览

总共3个函数:

_MouseClick($Handle, $X, $Y, $mKey, $Times);
参数:窗口句柄+X坐标+Y坐标+鼠标按键+点击次数 
_SendKeys($Handle, $Keys, $isBack)
参数:窗口句柄+按键+是否后台
_SendString($Handle, $String)
参数:窗口句柄+字符串

解决了官方论坛UDF里_sendstring遇到部分窗口发送中文乱码的问题,postmessage改用WM_IME_CHAR消息
其他好像没什么改进,关于后台组合键问题真是搞不懂,一直没什么进展
组合键功能采用半后台模式。Send+Postmessage实现。
关于alt组合键可以大家可以根据实际需要实现非真正意义上的alt组合键:
比如ALT+F的模拟可以直接:_WinAPI_PostMessage($Handle, 0x0112, 0xF100, 0x46)
参数:窗口句柄+WM_SYSCOMMAND+SC_KEYMENU+虚拟键码(无谓大小写)



下面是使用示例:以ACN群窗口为例,坐标根据我的窗口定的,不准但只为体现用法

$Handle = WinGetHandle('ACN Kernel')


_SendString($Handle, 'PostMessage中文消息测试');发送中文字符测试


_SendKeys($Handle, '{ctrl}_{enter}');ctrl+enter组合键


_SendKeys($Handle, 'a');小写a按键


_SendKeys($Handle, '{shift}_a');shift+a=A


_MouseClick($Handle, 333, 500);鼠标单击发送按钮


_MouseClick($Handle, 480, 315, 'left', 2);双击群成员,打开对话


WinWaitActive('无所谓的蛋蛋')


$Handle = WinGetHandle('无所谓的蛋蛋')


_SendString($Handle, 'Hi Boy !!')


_SendKeys($Handle, '{alt}_s');alt+s发送消息


_MouseClick($Handle, 200, 200, 'right');鼠标右击出现菜单


_SendKeys($Handle, '{ctrl}_{alt}_a', False);截图3组合键


Sleep(100)


_SendKeys($Handle, '{alt}_c');alt+c关闭对话


复制代码 高亮切换



鉴于有人喜欢用记事本测试,这里多说句,记事本接收输入消息的是edit控件,而非记事本窗体,代码如下 

Run("notepad.exe")


  $hWnd = WinWaitActive("无标题 - 记事本")


  $cWnd = ControlGetHandle($hWnd, "", "Edit1")


  _SendString($cWnd, "PostMessage中文消息测试");发送中文字符测试


复制代码 高亮切换



本帖隐藏的内容需要回复才可以浏览

Func _SendString($hWnd, $Str)


        For $s = 1 To StringLen($Str)


                $St = StringMid($Str, $s, 1)


                If Asc($St) < 128 Then ;虚拟键码小于128为英文


                        _PostMessage($hWnd, 0x286, Asc($St), 0);WM_IME_CHAR


                Else;大于127为中文


                        $IME = StringToASCIIArray($St, 0, 1, 1)


                        Local $ASCs = '0x'


                        For $a = 0 To UBound($IME) - 1


                                $ASCs &= Hex($IME[$a], 2);合并内码


                        Next


                        _PostMessage($hWnd, 0x286, $ASCs, 0);WM_IME_CHAR


                EndIf


        Next


        ;使用_SendMessageA也行


EndFunc   ;==>_SendString


 


Func _MouseClick($hWnd, $x, $y, $button = 'left', $times = 1, $delay = 250)


        Local $ix


        Local $lParam = BitOR(BitAND($x, 0xFFFF), $y * 0x10000)


        $button = StringLower($button)


        If $button = "left" Then


                For $ix = 1 To $times


                        _PostMessage($hWnd, 0x200, 0, $lParam);WM_MOUSEMOVE


                        _PostMessage($hWnd, 0x201, 1, $lParam);WM_LBUTTONDOWN


                        _PostMessage($hWnd, 0x202, 0, $lParam);WM_LBUTTONUP


                        If $ix < $times Then Sleep($delay)


                Next


        ElseIf $button = "right" Then


                For $ix = 1 To $times


                        _PostMessage($hWnd, 0x200, 0, $lParam);WM_MOUSEMOVE


                        _PostMessage($hWnd, 0x204, 2, $lParam);WM_RBUTTONDOWN


                        _PostMessage($hWnd, 0x205, 0, $lParam);WM_RBUTTONUP


                        If $ix < $times Then Sleep($delay)


                Next


        EndIf


EndFunc   ;==>_MouseClick


 


Func _SendKeys($hWnd, $iKeys,  $isBack = True)


        $iKeys = StringUpper($iKeys)


        If $hWnd <= 0 Or $iKeys = '' Then Return


        Local $ShiftDown = False, $CtrlDown = False, $AltDown = False, $CompKey = False


       


        If StringInStr($iKeys, '_') Then $CompKey = True ;'_'为组合键分隔符


        If $CompKey Then


                If StringInStr($iKeys, '{SHIFT}') Then


                        Send('{SHIFTDOWN}')


                        $ShiftDown = True


                EndIf


                If StringInStr($iKeys, '{CTRL}') Then


                        Send('{CTRLDOWN}')


                        $CtrlDown = True


                EndIf


                If StringInStr($iKeys, '{ALT}') Then


                        Send('{ALTDOWN}')


                        $AltDown = True


                EndIf


                $K = StringSplit($iKeys, '_', 1)


                If @error Then Return


                $iKeys = $K[$K[0]]


        EndIf


        If $isBack Then


                $iKeys = _IntVirtualKey($iKeys)


                $MapVK = _MapVirtualKey($iKeys, 0)


                $lParam = BitOR($MapVK * 0x10000, BitAND(1, 0xFFFF))


                _PostMessage($hWnd, 0x100, $iKeys, $lParam);WM_KEYDOWN


                _PostMessage($hWnd, 0x101, $iKeys, $lParam + 0xC0000000);WM_KEYUP


        Else


                $iKeys = StringLower($iKeys)


                ;Send('{ASC 0' & $iKeys & '}')


                Send($iKeys)


        EndIf


        If $AltDown Then Send('{ALTUP}');弹起alt


        If $CtrlDown Then Send('{CTRLUP}');弹起ctrl


        If $ShiftDown Then Send('{SHIFTUP}');弹起shift


EndFunc   ;==>_SendKeys


 


Func _MapVirtualKey($iCode, $iType)


        Local $Ret = DllCall('user32.dll', 'uint', 'MapVirtualKeyW', 'uint', $iCode, 'uint', $iType)


        If (@error) Or (Not $Ret[0]) Then Return ''


        Return $Ret[0]


EndFunc   ;==>_MapVirtualKey


 


Func _IntVirtualKey($iKey)


        If $iKey == '{BS}' Then Return 0x08


        If $iKey == '{TAB}' Then Return 0x09


        If $iKey == '{SHIFT}' Then Return 0x10


        If $iKey == '{CTRL}' Then Return 0x11


        If $iKey == '{ENTER}' Then Return 0x0D


        If $iKey == '{CLEAR}' Then Return 0x0C


        If $iKey == '{ALT}' Then Return 0x12


        If $iKey == '{PAUSE}' Then Return 0x13


        If $iKey == '{CAPS LOCK}' Then Return 0x14


        If $iKey == '{ESC}' Then Return 0x1B


        If $iKey == '{SPACEBAR}' Then Return 0x20


        If $iKey == '{PAGEUP}' Then Return 0x21


        If $iKey == '{PAGEDOWN}' Then Return 0x22


        If $iKey == '{END}' Then Return 0x23


        If $iKey == '{HOME}' Then Return 0x24


        If $iKey == '{LEFT}' Then Return 0x25


        If $iKey == '{UP}' Then Return 0x26


        If $iKey == '{RIGHT}' Then Return 0x27


        If $iKey == '{DOWN}' Then Return 0x28


        If $iKey == '{SELECT}' Then Return 0x29


        If $iKey == '{PRINT}' Then Return 0x2A


        If $iKey == '{EXECUTE}' Then Return 0x2B


        If $iKey == '{PRINTSCREEN}' Then Return 0x2C


        If $iKey == '{INS}' Then Return 0x2D


        If $iKey == '{DEL}' Then Return 0x2E


        If $iKey == '{F1}' Then Return 0x70


        If $iKey == '{F2}' Then Return 0x71


        If $iKey == '{F3}' Then Return 0x72


        If $iKey == '{F4}' Then Return 0x73


        If $iKey == '{F5}' Then Return 0x74


        If $iKey == '{F6}' Then Return 0x75


        If $iKey == '{F7}' Then Return 0x76


        If $iKey == '{F8}' Then Return 0x77


        If $iKey == '{F9}' Then Return 0x78


        If $iKey == '{F10}' Then Return 0x79


        If $iKey == '{F11}' Then Return 0x7A


        If $iKey == '{F12}' Then Return 0x7B


        If $iKey == '{F13}' Then Return 0x7C


        If $iKey == '{F14}' Then Return 0x7D


        If $iKey == '{F15}' Then Return 0x7E


        If $iKey == '{F16}' Then Return 0x7F


        If $iKey == '{NUM LOCK}' Then Return 0x90


        If $iKey == '{SCROLL LOCK}' Then Return 0x91


        If $iKey == '*' Then Return 0x6A


        If $iKey == '+' Then Return 0x6B


        If $iKey == '-' Then Return 0x6D ;BD


        If $iKey == '.' Then Return 0x6E


        If $iKey == '/' Then Return 0x6F


        If $iKey == ';' Then Return 0xBA


        If $iKey == '=' Then Return 0xBB


        If $iKey == ',' Then Return 0xBC


        If $iKey == '`' Then Return 0xC0


        If $iKey == '[' Then Return 0xDB


        If $iKey == '\' Then Return 0xDC


        If $iKey == ']' Then Return 0xDD


        Return Asc($iKey)


EndFunc   ;==>_MakeVirtualKey


 


Func _PostMessage($hWnd, $iMsg, $iwParam, $ilParam)


        DllCall("user32.dll", "bool", "PostMessage", "hwnd", $hWnd, "uint", $iMsg, "wparam", $iwParam, "lparam", $ilParam)


EndFunc   ;==>_WinAPI_PostMessage


复制代码 高亮切换


打赏

标签:autoitxudf

1

猜你喜欢

  • mstsc远程的UDF2016-08-15
  • AU3获取桌面图标坐标数据2016-08-15
  • 右侧2016一起努力
    最新发布的文章
    最新评论

    公告

    十年相伴 值得信赖 需要定制开发、购买加速器可以联系 QQ1368762345,微信同号