【问题标题】:How I can send mouse click in powershell?如何在PowerShell中发送鼠标点击?
【发布时间】:2020-12-23 08:57:54
【问题描述】:

如何使用此代码发送鼠标点击此位置。我想让我的鼠标到那里然后点击。

[System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point($xposi,$yposi)

【问题讨论】:

    标签: powershell events click mouse


    【解决方案1】:

    如果您不想编写自己的函数,Windows Automation Snapin for Powershell 有一个 Send-Click 函数。或者,您可以从windows API using mouse_event 导入一些功能,尽管我会推荐SendInput,因为它取代了mouse_event。下面是一个功能齐全的示例,它使用 P/Invoke 来获取您想要的功能并将左键单击发送到指定的屏幕坐标。

    $cSource = @'
    using System;
    using System.Drawing;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    public class Clicker
    {
    //https://msdn.microsoft.com/en-us/library/windows/desktop/ms646270(v=vs.85).aspx
    [StructLayout(LayoutKind.Sequential)]
    struct INPUT
    { 
        public int        type; // 0 = INPUT_MOUSE,
                                // 1 = INPUT_KEYBOARD
                                // 2 = INPUT_HARDWARE
        public MOUSEINPUT mi;
    }
    
    //https://msdn.microsoft.com/en-us/library/windows/desktop/ms646273(v=vs.85).aspx
    [StructLayout(LayoutKind.Sequential)]
    struct MOUSEINPUT
    {
        public int    dx ;
        public int    dy ;
        public int    mouseData ;
        public int    dwFlags;
        public int    time;
        public IntPtr dwExtraInfo;
    }
    
    //This covers most use cases although complex mice may have additional buttons
    //There are additional constants you can use for those cases, see the msdn page
    const int MOUSEEVENTF_MOVED      = 0x0001 ;
    const int MOUSEEVENTF_LEFTDOWN   = 0x0002 ;
    const int MOUSEEVENTF_LEFTUP     = 0x0004 ;
    const int MOUSEEVENTF_RIGHTDOWN  = 0x0008 ;
    const int MOUSEEVENTF_RIGHTUP    = 0x0010 ;
    const int MOUSEEVENTF_MIDDLEDOWN = 0x0020 ;
    const int MOUSEEVENTF_MIDDLEUP   = 0x0040 ;
    const int MOUSEEVENTF_WHEEL      = 0x0080 ;
    const int MOUSEEVENTF_XDOWN      = 0x0100 ;
    const int MOUSEEVENTF_XUP        = 0x0200 ;
    const int MOUSEEVENTF_ABSOLUTE   = 0x8000 ;
    
    const int screen_length = 0x10000 ;
    
    //https://msdn.microsoft.com/en-us/library/windows/desktop/ms646310(v=vs.85).aspx
    [System.Runtime.InteropServices.DllImport("user32.dll")]
    extern static uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);
    
    public static void LeftClickAtPoint(int x, int y)
    {
        //Move the mouse
        INPUT[] input = new INPUT[3];
        input[0].mi.dx = x*(65535/System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width);
        input[0].mi.dy = y*(65535/System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height);
        input[0].mi.dwFlags = MOUSEEVENTF_MOVED | MOUSEEVENTF_ABSOLUTE;
        //Left mouse button down
        input[1].mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
        //Left mouse button up
        input[2].mi.dwFlags = MOUSEEVENTF_LEFTUP;
        SendInput(3, input, Marshal.SizeOf(input[0]));
    }
    }
    '@
    Add-Type -TypeDefinition $cSource -ReferencedAssemblies System.Windows.Forms,System.Drawing
    #Send a click at a specified point
    [Clicker]::LeftClickAtPoint(600,600)
    

    【讨论】:

    • 你太棒了 tnx 的帮助
    • 这很棒,帮助我绕过 isTrusted: false
    • 正是我正在寻找的让我的电脑保持清醒的东西:D
    【解决方案2】:

    这是旧方法:

    #import mouse_event
    Add-Type -MemberDefinition '[DllImport("user32.dll")] public static extern void mouse_event(int flags, int dx, int dy, int cButtons, int info);' -Name U32 -Namespace W;
    #left mouse click
    [W.U32]::mouse_event(6,0,0,0,0);
    

    6 是 0x02 | 0x04, LMBD下 |文档中的 LMBUp

    https://msdn.microsoft.com/en-us/library/windows/desktop/ms646260(v=vs.85).aspx

    这里是移动和点击

    #move 10% along x and 10% along y and send left mouse click
    [W.U32]::mouse_event(0x02 -bor 0x04 -bor 0x8000 -bor 0x01, .1*65535, .1 *65535, 0, 0);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-05
      • 1970-01-01
      • 1970-01-01
      • 2012-09-04
      • 2012-06-04
      • 1970-01-01
      相关资源
      最近更新 更多