Skip to content

Instantly share code, notes, and snippets.

@PrateekKumarSingh
Created May 7, 2019 19:50
Show Gist options
  • Save PrateekKumarSingh/ed7530675000450d0af08317eeca42ab to your computer and use it in GitHub Desktop.
Save PrateekKumarSingh/ed7530675000450d0af08317eeca42ab to your computer and use it in GitHub Desktop.
# https://stackoverflow.com/questions/42145440/powershell-capture-mouse-click-event-inside-a-powershell-console
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);
}
}
"@
Add-Type -Assembly System.Windows.Forms
$ourID = (get-process -id (Get-WmiObject -class win32_process -Filter ("ProcessID = 14752")).parentprocessid).mainwindowhandle;
$win = New-Object Window($ourID)
cls
Write-host " Button " -ForegroundColor Black -BackgroundColor Yellow
While($true){
$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));
[Windows.Forms.UserControl]::MouseButtons
$mp
$win.update()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment