2017-03-25 5 views
0

に発射ません:MOUSE_EVENTは、私はプログラム的に左ボタンのマウスクリックをシミュレートしようとしています特定のアプリケーション(C#の)

 [DllImport("user32.dll")] 
     static extern bool SetCursorPos(int x, int y); 

     [DllImport("user32.dll")] 
     public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo); 

     public const int MOUSEEVENTF_LEFTDOWN = 0x02; 
     public const int MOUSEEVENTF_LEFTUP = 0x04;  

     public static void LeftMouseClick(int xpos, int ypos) 
     { 
      SetCursorPos(xpos, ypos); 
      mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0); 
      System.Threading.Thread.Sleep(1000); 
      mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0); 
     } 

カーソルが画面上の適切な位置に移動しますが、クリックが発射されていません。何かご意見は?

編集:私はいくつかのテストを行いました。私が使用している方法は問題ではないようです(スチーム、Skypeなどのアプリケーションをクリックします)。カーソルが特にクリックしたいアプリケーション(アンドロイドエミュレータ)の上にあるときにクリックメソッドを呼び出すと、何も起こりません。マウスカーソルがその場所に移動しますが、クリックしません...もう一度別のエミュレータをテストしようとしています。

+0

あなたは 'OR'dイベントを送るつもりはありませんか?私はアップとダウンが異なる時に起こるだろうと思うだろう – BradleyDotNET

+0

@ブラッドリードットコム私はそれをまだ編集していない別のイベントに編集した。 – binbin

+0

イベントは、フォーカスのあるスレッドのキューに挿入されます。それはどのプログラムですか?誤った入力は実際の問題の解決策ではない可能性があります。たとえそれがあなただったとしても、SendInputでそれを行うことを意図しています。あなたはその文書を読んでいないのですか? –

答えて

0

私はマウスイベントを包まれたに取り組んでいた別のプロジェクトのためのヘルパークラスを書いた:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.InteropServices; 
using System.Text; 
using System.Threading.Tasks; 

class MouseHelper 
{ 
    internal struct INPUT 
    { 
     public UInt32 Type; 
     public MOUSEKEYBDHARDWAREINPUT Data; 
    } 
    [StructLayout(LayoutKind.Explicit)] 
    internal struct MOUSEKEYBDHARDWAREINPUT 
    { 
     [FieldOffset(0)] 
     public MOUSEINPUT Mouse; 
    } 

    internal struct MOUSEINPUT 
    { 
     public Int32 X; 
     public Int32 Y; 
     public UInt32 MouseData; 
     public UInt32 Flags; 
     public UInt32 Time; 
     public IntPtr ExtraInfo; 
    } 

    /// <summary> 
    /// Synthesizes keystrokes, mouse motions, and button clicks. 
    /// </summary> 
    [DllImport("user32.dll")] 
    internal static extern uint SendInput(uint nInputs, [MarshalAs(UnmanagedType.LPArray), In] INPUT[] pInputs, int cbSize); 
    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] 
    public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo); 

    private const int MOUSEEVENTF_LEFTDOWN = 0x02; 
    private const int MOUSEEVENTF_LEFTUP = 0x04; 
    private const int MOUSEEVENTF_RIGHTDOWN = 0x08; 
    private const int MOUSEEVENTF_RIGHTUP = 0x10; 

    public static void DoMouseClick() 
    { 
     var inputMouseDown = new INPUT(); 
     inputMouseDown.Type = 0; /// input type mouse 
     inputMouseDown.Data.Mouse.Flags = 0x0002; /// left button down 

     var inputMouseUp = new INPUT(); 
     inputMouseUp.Type = 0; /// input type mouse 
     inputMouseUp.Data.Mouse.Flags = 0x0004; /// left button up 

     var inputs = new INPUT[] { inputMouseDown, inputMouseUp }; 
     SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT))); 
    } 

    public static void DoMouseClick2() 
    { 
     mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); 
    } 
} 

私が行うために必要なすべては、私はマウスクリックを望んでいた時はいつでもMouseHelper.DoMouseClick()を呼び出しました。私は、マウスの移動機能を拡張機能として追加することができると考えています。

+0

私はあなたのクラスを試しました、ありがとう。実際に正常に動作しているように見えますが、クリックすると特定のアプリケーションでクリックが発生しなくなります(アンドロイドエミュレータ)。 – binbin

関連する問題