2017-02-09 6 views
0

Powershellでいくつかのトリックをしようとしています。私はスクリプトを書きたいと思って、そのスクリプトは、PowerShellのコンソールの中でマウスのイベント(クリック、移動など)を聞くことができます。Powershell - Powershellコンソール内でマウスクリックイベントをキャプチャする

たとえば、スクリプトがアクティブな状態で、PowerShellコンソール内でマウスをクリックすると、コンソールはカーソルの位置を出力できます。

可能ですか?可能であれば、どうですか?

ありがとうございます。

+0

なぜこの記事を下落させた人がいますか?ここで質問するのは正しい質問ではありませんか? – Fentoyal

+0

こんにちは、* SOの良い質問*には、少なくとも始まりには少しのコードが含まれていますが、それはdownvoteの理由かもしれません。 – sodawillow

+0

申し訳ありませんが、私の質問状態のように、私はこれが可能かどうかわからないので、まだコードはありません。私は$ホストオブジェクトを調べましたが、イベントハンドラがないようです。オンラインで検索すると、ほとんどの結果は「イベントを聞く」の代わりに「イベントを送信する」ことになります。そこで私はここに来て助けを求めました。 – Fentoyal

答えて

0

どのようにすべてが一緒にピースしようとしているのか分かりません。 これが役立つかもしれません。

<# Gets the Mouse Position #> 
[System.Windows.Forms.Cursor]::Position 
+0

答えをありがとう。ただし、これはクリックイベントをキャプチャできません。 PSにマウスのReadKeyのようなものがあることを願っています。ReadClick – Fentoyal

0

私はそれは、以下の使用してこれを行うことが可能だということがわかりました:現在押さマウスボタンの文字列を返します

[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は私たちに、コンソールウィンドウの外側の境界を与え、私たちは、カーソルをbuffercellどの検索する場合、我々はつもりであります私たちの範囲でいくつかの仕事をする必要があります。

$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"を使用して、コンソールウィンドウがクリックされたときだけチェックすることもできます。

+0

詳細な回答ありがとう!あなたのソリューションをまだテストする機会がありませんでしたが、あなたの精巧な説明に本当に感謝しています! – Fentoyal

関連する問題