后台发送按键和操作鼠标 By FeiYue

/*
;-------------------------------
;  后台发送按键和操作鼠标 By FeiYue
;
;  使用说明:
;  1、首先需要管理员权限
;  2、后台发送按键,要获取控件句柄
;  3、后台操作鼠标,要获取窗口句柄和窗口坐标
;-------------------------------
*/
;-- 测试:打开记事本,放到后台,发送一些字母,然后发送 Alt+F4 关闭窗口
F1::
Run, notepad.exe,,, pid
WinWait, ahk_pid %pid%,, 3
WinSet, Bottom,, ahk_pid %pid%
Sleep, 1000
;-- 可以用这个命令直接获取控件的句柄
MouseGetPos,,,, hwnd, 2
;-- 也可以通过 标题和类名 获取控件的句柄
hwnd:=后台.获取控件句柄("ahk_pid " pid, "Edit1")
后台.发送按键(hwnd, "a")
后台.发送按键(hwnd, "+b")
后台.发送按键(hwnd, "!F4")
return
F2::
;-- 可以用这个命令直接获取窗口的句柄
MouseGetPos,,, hwnd
;-- 也可以用WinExist获取窗口的句柄
hwnd:=WinExist("ahk_class 360se6_Frame")
后台.移动鼠标(hwnd, 997, 131)
; 后台.点击左键(hwnd, 999, 131)
; 后台.点击右键(hwnd, 999, 131)
return

;========== 下面是类和函数 ==========
Class 后台 {
  ;-- 类开始,使用类的命名空间可防止变量名、函数名污染
  获取控件句柄(WinTitle, Control="") {
    tmm:=A_TitleMatchMode, dhw:=A_DetectHiddenWindows
    SetTitleMatchMode, 2
    DetectHiddenWindows, On
    ControlGet, hwnd, Hwnd,, %Control%, %WinTitle%
    DetectHiddenWindows, %dhw%
    SetTitleMatchMode, %tmm%
    return, hwnd
  }
  点击左键(hwnd, x, y) {
    return, this.Click_PostMessage(hwnd, x, y, "L")
  }
  点击右键(hwnd, x, y) {
    return, this.Click_PostMessage(hwnd, x, y, "R")
  }
  移动鼠标(hwnd, x, y) {
    return, this.Click_PostMessage(hwnd, x, y, 0)
  }
  Click_PostMessage(hwnd, x, y, flag="L") {
    static WM_MOUSEMOVE:=0x200
      , WM_LBUTTONDOWN:=0x201, WM_LBUTTONUP:=0x202
      , WM_RBUTTONDOWN:=0x204, WM_RBUTTONUP:=0x205
    ;---------------------
    VarSetCapacity(pt,16,0), DllCall("GetWindowRect", "ptr",hwnd, "ptr",&pt)
    , ScreenX:=x+NumGet(pt,"int"), ScreenY:=y+NumGet(pt,4,"int")
    Loop {
      NumPut(ScreenX,pt,"int"), NumPut(ScreenY,pt,4,"int")
      , DllCall("ScreenToClient", "ptr",hwnd, "ptr",&pt)
      , x:=NumGet(pt,"int"), y:=NumGet(pt,4,"int")
      , id:=DllCall("ChildWindowFromPoint", "ptr",hwnd, "int64",y<<32|x, "ptr")
      if (id=hwnd or !id)
        Break
      else hwnd:=id
    }
    ;---------------------
    if (flag=0)
      PostMessage, WM_MOUSEMOVE, 0, (y<<16)|x,, ahk_id %hwnd%
    else if InStr(flag,"L")=1
    {
      PostMessage, WM_LBUTTONDOWN, 0, (y<<16)|x,, ahk_id %hwnd%
      PostMessage, WM_LBUTTONUP, 0, (y<<16)|x,, ahk_id %hwnd%
    }
    else if InStr(flag,"R")=1
    {
      PostMessage, WM_RBUTTONDOWN, 0, (y<<16)|x,, ahk_id %hwnd%
      PostMessage, WM_RBUTTONUP, 0, (y<<16)|x,, ahk_id %hwnd%
    }
  }
  发送按键(hwnd, key) {
    static WM_KEYDOWN:=0x100, WM_KEYUP:=0x101
      , WM_SYSKEYDOWN:=0x104, WM_SYSKEYUP:=0x105, KEYEVENTF_KEYUP:=0x2
    Alt:=Ctrl:=Shift:=0
    if InStr(key,"!")
      Alt:=1, key:=StrReplace(key,"!")
    if InStr(key,"^")
    {
      Ctrl:=1, key:=StrReplace(key,"^")
      this.Send_keybd_event("Ctrl")
      Sleep, 100
    }
    if InStr(key,"+")
    {
      Shift:=1, key:=StrReplace(key,"+")
      this.Send_keybd_event("Shift")
      Sleep, 100
    }
    this.Send_PostMessage(hwnd, Alt=1 ? WM_SYSKEYDOWN : WM_KEYDOWN, key)
    Sleep, 100
    this.Send_PostMessage(hwnd, Alt=1 ? WM_SYSKEYUP : WM_KEYUP, key)
    if (Shift=1)
      this.Send_keybd_event("Shift", KEYEVENTF_KEYUP)
    if (Ctrl=1)
      this.Send_keybd_event("Ctrl", KEYEVENTF_KEYUP)
  }
  Send_PostMessage(hwnd, msg, key) {
    static WM_KEYDOWN:=0x100, WM_KEYUP:=0x101
      , WM_SYSKEYDOWN:=0x104, WM_SYSKEYUP:=0x105
    VK:=GetKeyVK(Key), SC:=GetKeySC(Key)
    flag:=msg=WM_KEYDOWN ? 0
      : msg=WM_KEYUP ? 0xC0
      : msg=WM_SYSKEYDOWN ? 0x20
      : msg=WM_SYSKEYUP ? 0xE0 : 0
    PostMessage, msg, VK, (count:=1)|(SC<<16)|(flag<<24),, ahk_id %hwnd%
  }
  Send_keybd_event(key, msg=0) {
    static KEYEVENTF_KEYUP:=0x2
    VK:=GetKeyVK(Key), SC:=GetKeySC(Key)
    DllCall("keybd_event", "int",VK, "int",SC, "int",msg, "int",0)
  }
  ;-- 类结束
}
;

给TA捐赠
共{{data.count}}人
人已捐赠
其他案例

后台点击函数测试示例

2021-12-2 15:53:45

其他

划词翻译+OCR翻译-分享版

2021-12-2 15:53:58

6 条回复 A文章作者 M管理员
  1. AUTOAHK

    网上也没个教程,希望大大可以出个教程😚

  2. 梦也清寒

    好像92行和110行这两个位置 少了些关于Alt的描述

  3. user15424

    好像鼠标操作没反应 后台.发送按键(hwnd, “F3”) sleep,5000 ;后台.发送按键(hwnd, “F4”) 后台.移动鼠标(hwnd, 225, 182) 后台.点击左键(hwnd, 225, 182)

  4. €totochow

    编译语法错误

  5. yaoxin1588

    非常感谢分享,如果是后台 拖动 MouseClickDrag 该怎么写呢?

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索