gpt4 book ai didi

Powershell - 在 powershell 控制台中捕获鼠标单击事件

转载 作者:行者123 更新时间:2023-12-02 23:41:08 25 4
gpt4 key购买 nike

我正在尝试使用 Powershell 做一些技巧。我想编写一个脚本,该脚本可以监听 powershell 控制台内的鼠标事件(单击、移动等)。

例如,当我的脚本处于事件状态时,当我在 powershell 控制台内单击鼠标时,控制台可以输出我的光标位置。

是否可以?如果可以,怎么做?

提前致谢。

最佳答案

我发现可以使用以下方法来做到这一点:

[System.Windows.Forms.UserControl]::MouseButtons

它返回当前按下的鼠标按钮的字符串。我们对此进行投票和 [System.Windows.Forms.Cursor]::Position根据 WorWin 跟踪鼠标所在的位置以及按下的按钮。

但是,跟踪鼠标在控制台窗口内的位置有点棘手。我个人为此使用自定义类。
Add-Type -ReferencedAssemblies System.Drawing @"
using System;
using System.Drawing;
using System.Runtime.InteropServices;
public class Window
{
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);

[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();

public RECT bounds;
public bool isForeground;
private IntPtr hWnd;
public int Width;
public int Height;

public Window(IntPtr handle)
{
hWnd = handle;
Update();
}
public void Update()
{
if(GetWindowRect(hWnd, out bounds))
{
Width = bounds.Right - bounds.Left;
Height = bounds.Bottom - bounds.Top;

if(GetForegroundWindow() == hWnd){isForeground = true;}
else{isForeground = false;}
}
}
public bool Contains(Point pt)
{
return bounds.Contains(pt);
}
}
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;

public Boolean Contains(Point pt)
{
if( (pt.X >= Left) && (pt.X < Right) && (pt.Y >= Top) && (pt.Y < Bottom) ){ return true;}
return false;
}
}
public struct POINT
{
public int X;
public int Y;
public static implicit operator Point(POINT point)
{
return new Point(point.X, point.Y);
}
}
"@

要获取 Powershell 控制台窗口:
$ourID = (get-process -id (Get-WmiObject -class win32_process -Filter ("ProcessID = $pid")).parentprocessid).mainwindowhandle;
$win = New-Object Window($ourID);

现在, $win.bounds给了我们控制台窗口的外部边界,所以如果我们想找到光标所在的缓冲区单元,我们将不得不在我们的边界上做一些工作。
$innerOffsets = new-object RECT;
$innerOffsets.Top = [Windows.Forms.SystemInformation]::CaptionHeight + [Windows.Forms.SystemInformation]::HorizontalResizeBorderThickness + [Windows.Forms.SystemInformation]::Border3DSize.Height;
$innerOffsets.Bottom = -([Windows.Forms.SystemInformation]::HorizontalResizeBorderThickness + [Windows.Forms.SystemInformation]::Border3DSize.Height);
$inneroffsets.Left = [Windows.Forms.SystemInformation]::VerticalResizeBorderThickness + [Windows.Forms.SystemInformation]::Border3DSize.Width;
$inneroffsets.Right = -([Windows.Forms.SystemInformation]::VerticalResizeBorderThickness + [Windows.Forms.SystemInformation]::Border3DSize.Width);

if([console]::bufferheight -gt [console]::windowheight)
{
$inneroffsets.right -= [Windows.Forms.SystemInformation]::HorizontalScrollBarThumbWidth;
}
if([console]::bufferwidth -gt [console]::windowwidth)
{
$inneroffsets.bottom -= [Windows.Forms.SystemInformation]::VerticalScrollBarThumbHeight;
}

这为我们提供了获取窗口内部边界所需的偏移量。从这里我们可以得到我们在窗口上的相对位置。
$mp = [Windows.Forms.Cursor]::Position;
$mp.x -= ($win.bounds.left + $inneroffsets.left);
$mp.y -= ($win.bounds.top + $inneroffsets.top);

现在我们可以将鼠标位置转换为缓冲区单元格位置。
$innerWidth = ($win.bounds.right + $inneroffsets.right) - ($win.bounds.left + $inneroffsets.left);
$innerheight = ($win.bounds.bottom + $inneroffsets.bottom) - ($win.bounds.top + $inneroffsets.top);
$mp.x = [Math]::Floor($mp.x / ( $innerwidth / [console]::windowWidth));
$mp.y = [Math]::Floor($mp.y / ( $innerheight / [console]::windowheight));

您必须使用 $win.update()每次检查。您也可以使用 $win.isforeground[Windows.Forms.UserControl]::MouseButtons -match "Left"仅检查何时单击控制台窗口。

关于Powershell - 在 powershell 控制台中捕获鼠标单击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42145440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com