簡體   English   中英

如何移動光標或模擬其他應用程序的點擊?

[英]How to move the cursor or simulate clicks for other applications?

我正在使用Leap Motion Controller創建一個C#Windows應用程序。 我正在Windows 8和Visual Studio 2010上進行開發。我使用SetCursorPosuser32.dll mouse_event來移動光標並模擬點擊。

我希望在任何應用程序中都可以移動光標。 當我從Visual Studio運行/調試時,它僅在應用程序本身或Visual Studio中有效。 在其他應用程序中,鼠標不移動且單擊不起作用,但是,如果我嘗試用真正的鼠標移動光標,它將返回到原來的位置。 當獨立運行時,它不會在Visual Studio中移動,並且可以使用真實鼠標在其他應用程序中移動鼠標。

我有這段代碼可以使用SetCursorPosmouse_event

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

[DllImport("User32.Dll")]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);

public static void MouseClick(uint x, uint y) {
    mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, x, y, 0, 0);
}

在表單類中,鼠標位置存儲在兩個字段中。 Timer的刻度中,我設置光標位置並進行必要的單擊,如下所示:

if (!frame.Hands.IsEmpty) {
    Hand hand = frame.Hands.Leftmost;
    if (!hand.Fingers.IsEmpty) {
        // Get coordinates...
        SetCursorPos(mousex, mousey);
    }
}
foreach (Gesture gesture : gestures) {
    if (gesture.Type == Gesture.GestureType.TYPESCREENTAP) {
        MouseClick(mousex, mousey);
    }
}

if語句用於Leap設備; 我想移動鼠標並僅在有手才能獲取坐標時做其他事情。

甚至可以為其他應用程序移動光標或模擬鼠標單擊嗎? 如果是的話,怎么辦?

您啟發了我重構一些自動化代碼:

NativeMethods.cs-從在線獲取了大部分信息:

using System;
using System.Runtime.InteropServices;

public class NativeMethods
{
   [DllImport( "user32.dll", SetLastError = true )]
   internal static extern Int32 SendInput( Int32 cInputs, ref INPUT pInputs, Int32 cbSize );

   [StructLayout( LayoutKind.Explicit, Pack = 1, Size = 28 )]
   internal struct INPUT
   {
      [FieldOffset( 0 )] public InputType dwType;
      [FieldOffset( 4 )] public MOUSEINPUT mi;
      [FieldOffset( 4 )] public KEYBDINPUT ki;
      [FieldOffset( 4 )] public HARDWAREINPUT hi;
   }

   [StructLayout( LayoutKind.Sequential, Pack = 1 )]
   internal struct MOUSEINPUT
   {
      public Int32 dx;
      public Int32 dy;
      public Int32 mouseData;
      public MOUSEEVENTF dwFlags;
      public Int32 time;
      public IntPtr dwExtraInfo;
   }

   [StructLayout( LayoutKind.Sequential, Pack = 1 )]
   internal struct KEYBDINPUT
   {
      public Int16 wVk;
      public Int16 wScan;
      public KEYEVENTF dwFlags;
      public Int32 time;
      public IntPtr dwExtraInfo;
   }

   [StructLayout( LayoutKind.Sequential, Pack = 1 )]
   internal struct HARDWAREINPUT
   {
      public Int32 uMsg;
      public Int16 wParamL;
      public Int16 wParamH;
   }

   internal enum InputType : int
   {
      Mouse = 0,
      Keyboard = 1,
      Hardware = 2
   }

   [Flags()]
   internal enum MOUSEEVENTF : int
   {
      MOVE = 0x1,
      LEFTDOWN = 0x2,
      LEFTUP = 0x4,
      RIGHTDOWN = 0x8,
      RIGHTUP = 0x10,
      MIDDLEDOWN = 0x20,
      MIDDLEUP = 0x40,
      XDOWN = 0x80,
      XUP = 0x100,
      VIRTUALDESK = 0x400,
      WHEEL = 0x800,
      ABSOLUTE = 0x8000
   }

   [Flags()]
   public enum KEYEVENTF : int
   {
      EXTENDEDKEY = 1,
      KEYUP = 2,
      UNICODE = 4,
      SCANCODE = 8
   }

   /// <summary>The MapVirtualKey function translates (maps) a virtual-key code into a scan
   /// code or character value, or translates a scan code into a virtual-key code
   /// </summary>
   /// <param name="uCode">[in] Specifies the virtual-key code or scan code for a key.
   /// How this value is interpreted depends on the value of the uMapType parameter</param>
   /// <param name="uMapType">[in] Specifies the translation to perform. The value of this
   /// parameter depends on the value of the uCode parameter.</param>
   /// <returns>Either a scan code, a virtual-key code, or a character value, depending on
   /// the value of uCode and uMapType. If there is no translation, the return value is zero</returns>
   /// <remarks></remarks>
   [DllImport( "User32.dll", SetLastError = false, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto )]
   public static extern UInt32 MapVirtualKey( UInt32 uCode, MapVirtualKeyMapTypes uMapType );


   /// <summary>The set of valid MapTypes used in MapVirtualKey
   /// </summary>
   /// <remarks></remarks>
   public enum MapVirtualKeyMapTypes : uint
   {
      /// <summary>uCode is a virtual-key code and is translated into a scan code.
      /// If it is a virtual-key code that does not distinguish between left- and
      /// right-hand keys, the left-hand scan code is returned.
      /// If there is no translation, the function returns 0.
      /// </summary>
      /// <remarks></remarks>
      MAPVK_VK_TO_VSC = 0x0,

      /// <summary>uCode is a scan code and is translated into a virtual-key code that
      /// does not distinguish between left- and right-hand keys. If there is no
      /// translation, the function returns 0.
      /// </summary>
      /// <remarks></remarks>
      MAPVK_VSC_TO_VK = 0x1,

      /// <summary>uCode is a virtual-key code and is translated into an unshifted
      /// character value in the low-order word of the return value. Dead keys (diacritics)
      /// are indicated by setting the top bit of the return value. If there is no
      /// translation, the function returns 0.
      /// </summary>
      /// <remarks></remarks>
      MAPVK_VK_TO_CHAR = 0x2,

      /// <summary>Windows NT/2000/XP: uCode is a scan code and is translated into a
      /// virtual-key code that distinguishes between left- and right-hand keys. If
      /// there is no translation, the function returns 0.
      /// </summary>
      /// <remarks></remarks>
      MAPVK_VSC_TO_VK_EX = 0x3,

      /// <summary>Not currently documented
      /// </summary>
      /// <remarks></remarks>
      MAPVK_VK_TO_VSC_EX = 0x4
   }

}

MouseInput.cs

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace SendInput
{
   public class MouseInput
   {
      public static void LeftClick()
      {
         DoMouse( NativeMethods.MOUSEEVENTF.LEFTDOWN, new System.Drawing.Point( 0, 0 ) );
         DoMouse( NativeMethods.MOUSEEVENTF.LEFTUP, new System.Drawing.Point( 0, 0 ) );
      }

      public static void LeftClick( int x, int y )
      {
         DoMouse( NativeMethods.MOUSEEVENTF.MOVE | NativeMethods.MOUSEEVENTF.ABSOLUTE, new System.Drawing.Point( x, y ) );
         DoMouse( NativeMethods.MOUSEEVENTF.LEFTDOWN, new System.Drawing.Point( x, y ) );
         DoMouse( NativeMethods.MOUSEEVENTF.LEFTUP, new System.Drawing.Point( x, y ) );
      }

      public static void ClickBoundingRectangleByPercentage( int xPercentage, int yPercentage, System.Drawing.Rectangle bounds )
      {
         double additional = 0.0;
         if ( xPercentage == 99 )
            additional = 0.5;
         int xPixel = Convert.ToInt32( bounds.Left + bounds.Width * ( xPercentage + additional ) / 100 );
         int yPixel = Convert.ToInt32( bounds.Top + bounds.Height * ( yPercentage ) / 100 );
         LeftClick( xPixel, yPixel );
      }

      public static void RightClick()
      {
         DoMouse( NativeMethods.MOUSEEVENTF.RIGHTDOWN, new System.Drawing.Point( 0, 0 ) );
         DoMouse( NativeMethods.MOUSEEVENTF.RIGHTUP, new System.Drawing.Point( 0, 0 ) );
      }

      public static void RightClick( int x, int y )
      {
         DoMouse( NativeMethods.MOUSEEVENTF.MOVE | NativeMethods.MOUSEEVENTF.ABSOLUTE, new System.Drawing.Point( x, y ) );
         DoMouse( NativeMethods.MOUSEEVENTF.RIGHTDOWN, new System.Drawing.Point( x, y ) );
         DoMouse( NativeMethods.MOUSEEVENTF.RIGHTUP, new System.Drawing.Point( x, y ) );
      }

      public static void MoveMouse( Point p )
      {
         MoveMouse( p.X, p.Y );
      }

      public static void MoveMouse( System.Windows.Point p )
      {
         MoveMouse( Convert.ToInt32( p.X ), Convert.ToInt32( p.Y ) );
      }

      public static void MoveMouse( int x, int y )
      {
         DoMouse( NativeMethods.MOUSEEVENTF.MOVE | NativeMethods.MOUSEEVENTF.ABSOLUTE, new System.Drawing.Point( x, y ) );
      }

      public static System.Drawing.Point GetMousePosition()
      {
         return Cursor.Position;
      }

      public static void ScrollWheel( int scrollSize )
      {
         DoMouse( NativeMethods.MOUSEEVENTF.WHEEL, new System.Drawing.Point( 0, 0 ), scrollSize );
      }

      private static void DoMouse( NativeMethods.MOUSEEVENTF flags, Point newPoint, int scrollSize = 0 )
      {
         NativeMethods.INPUT input = new NativeMethods.INPUT();
         NativeMethods.MOUSEINPUT mi = new NativeMethods.MOUSEINPUT();
         input.dwType = NativeMethods.InputType.Mouse;
         input.mi = mi;
         input.mi.dwExtraInfo = IntPtr.Zero;
         // mouse co-ords: top left is (0,0), bottom right is (65535, 65535)
         // convert screen co-ord to mouse co-ords...
         input.mi.dx = newPoint.X * 65535 / Screen.PrimaryScreen.Bounds.Width;
         input.mi.dy = newPoint.Y * 65535 / Screen.PrimaryScreen.Bounds.Height;
         input.mi.time = 0;
         input.mi.mouseData = scrollSize * 120;
         // can be used for WHEEL event see msdn
         input.mi.dwFlags = flags;
         int cbSize = Marshal.SizeOf( typeof ( NativeMethods.INPUT ) );
         int result = NativeMethods.SendInput( 1, ref input, cbSize );
         if ( result == 0 )
            Debug.WriteLine( Marshal.GetLastWin32Error() );
      }
   }
}

是的,您想使用“ SendInput”功能。

請參見: 除非我移動光標,否則SendInput不會執行單擊鼠標按鈕

也,

http://www.pinvoke.net/default.aspx/user32.sendinput

http://www.pinvoke.net/default.aspx/Structures/INPUT.html

我已經修復了第一個超級鏈接-請參閱有關以下內容的評論:

使用SendInput函數時,應注意一些事項。

如果未指定MOUSEEVENTF_ABSOLUTE標志,則dx和dy(MouseInputData結構)是相對於當前鼠標位置的相對坐標。 如果確實指定了MOUSEEVENTF_ABSOLUTE,則dx和dy是0到65535之間的絕對坐標。因此,如果x和y坐標是屏幕坐標,則應使用以下函數來計算dx和dy:

http://msdn.microsoft.com/zh-cn/library/ms646310%28VS.85%29.aspx

我設法自己解決了這個問題。

有兩個問題。 第一個(也是最主要的)是Leap設備在后台時沒有向我的應用程序發送幀。 通過將此代碼添加到偵聽器的onConnect()方法,可以解決此問題 ,如下所示:

controller.SetPolicyFlags(Controller.PolicyFlag.POLICYBACKGROUNDFRAMES);

第二個問題是,在獨立運行時,該應用程序無權將輸入發送給其他應用程序。 我按照此處 (向下滾動,有關uiAccess的信息)和此處的說明進行操作,並對我的應用程序簽名並將其添加到清單中:

<requestedExecutionLevel level="asInvoker" uiAccess="true" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM