Mouse.bat 模拟鼠标操作脚本

mouse.bat 模拟鼠标操作,
调用方式
//clicks at the current position
call mouse click

//double clicks at the current position
call mouse doubleClick

//right clicks at the current position
call mouse rightClick

//returns the position of the cursor
call mouse position

//scrolls up the mouse wheel with 1500 units
call mouse scrollUp 150

//scrolls down with 100 postitions
call mouse scrollDown 100

//relatively(from the current position) moves the mouse with 100 horizontal and 100 vertial postitions
call mouse moveBy 100x100

//absolute positioning
call mouse moveTo 100x100

//relative drag (lefclick and move)
call mouse dragBy 300x200

//absolute drag
call mouse dragTo 500x500

// 2>nul||@goto :batch
/*
:batch
@echo off
setlocal

:: find csc.exe
set "csc="
for /r "%SystemRoot%\Microsoft.NET\Framework\" %%# in ("*csc.exe") do  set "csc=%%#"

if not exist "%csc%" (
   echo no .net framework installed
   exit /b 10
)

if not exist "%~n0.exe" (
   call %csc% /nologo /warn:0 /out:"%~n0.exe" "%~dpsfnx0" || (
      exit /b %errorlevel% 
   )
)
%~n0.exe %*
endlocal & exit /b %errorlevel%

*/

// To create this I've stole code from :
// http://inputsimulator.codeplex.com/
// https://stackoverflow.com/a/8022534/388389

using System;
using System.Runtime.InteropServices;

namespace MouseMover
{
    public class MouseSimulator
    {
        [DllImport("user32.dll", SetLastError = true)]
        static extern uint SendInput(uint nInputs, ref INPUT pInputs, int cbSize);
        [DllImport("user32.dll")]
        public static extern int SetCursorPos(int x, int y);
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetCursorPos(out POINT lpPoint);
        //----//
        [DllImport("user32.dll")]
        public static extern bool ClientToScreen(IntPtr hWnd, ref POINT lpPoint);
        [DllImport("user32.dll")]
        static extern void ClipCursor(ref Rect rect);
        [DllImport("user32.dll")]
        static extern void ClipCursor(IntPtr rect);
        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr CopyImage(IntPtr hImage, uint uType, int cxDesired, int cyDesired, uint fuFlags);
        [DllImport("user32.dll")]
        static extern bool CopyRect(out Rect lprcDst, [In] ref Rect lprcSrc);
		[DllImport("user32.dll")]
		static extern int GetSystemMetrics(SystemMetric smIndex);


        [StructLayout(LayoutKind.Sequential)]
        struct INPUT
        {
            public SendInputEventType type;
            public MouseKeybdhardwareInputUnion mkhi;
        }
        [StructLayout(LayoutKind.Explicit)]
        struct MouseKeybdhardwareInputUnion
        {
            [FieldOffset(0)]
            public MouseInputData mi;

            [FieldOffset(0)]
            public KEYBDINPUT ki;

            [FieldOffset(0)]
            public HARDWAREINPUT hi;
        }
        [StructLayout(LayoutKind.Sequential)]
        struct KEYBDINPUT
        {
            public ushort wVk;
            public ushort wScan;
            public uint dwFlags;
            public uint time;
            public IntPtr dwExtraInfo;
        }
        [StructLayout(LayoutKind.Sequential)]
        struct HARDWAREINPUT
        {
            public int uMsg;
            public short wParamL;
            public short wParamH;
        }
        [StructLayout(LayoutKind.Sequential)]
        public struct POINT
        {
            public int X;
            public int Y;

            public POINT(int x, int y)
            {
                this.X = x;
                this.Y = y;
            }
        }
        struct MouseInputData
        {
            public int dx;
            public int dy;
            public uint mouseData;
            public MouseEventFlags dwFlags;
            public uint time;
            public IntPtr dwExtraInfo;
        }
        struct Rect
        {
            public long left;
            public long top;
            public long right;
            public long bottom;

            public Rect(long left,long top,long right , long bottom)
            {
                this.left = left;
                this.top = top;
                this.right = right;
                this.bottom = bottom;
            }
        }

        [Flags]
        enum MouseEventFlags : uint
        {
            MOUSEEVENTF_MOVE = 0x0001,
            MOUSEEVENTF_LEFTDOWN = 0x0002,
            MOUSEEVENTF_LEFTUP = 0x0004,
            MOUSEEVENTF_RIGHTDOWN = 0x0008,
            MOUSEEVENTF_RIGHTUP = 0x0010,
            MOUSEEVENTF_MIDDLEDOWN = 0x0020,
            MOUSEEVENTF_MIDDLEUP = 0x0040,
            MOUSEEVENTF_XDOWN = 0x0080,
            MOUSEEVENTF_XUP = 0x0100,
            MOUSEEVENTF_WHEEL = 0x0800,
            MOUSEEVENTF_VIRTUALDESK = 0x4000,
            MOUSEEVENTF_ABSOLUTE = 0x8000
        }
        enum SendInputEventType : int
        {
            InputMouse,
            InputKeyboard,
            InputHardware
        }
		
		enum SystemMetric
		{
		  SM_CXSCREEN = 0,
		  SM_CYSCREEN = 1,
		}
		
		static int CalculateAbsoluteCoordinateX(int x)
		{
		  return (x * 65536) / GetSystemMetrics(SystemMetric.SM_CXSCREEN);
		}

		static int CalculateAbsoluteCoordinateY(int y)
		{
		  return (y * 65536) / GetSystemMetrics(SystemMetric.SM_CYSCREEN);
		}

        static void DoubleClick()
        {
            ClickLeftMouseButton();
            //System.Threading.Thread.Sleep(100);
            ClickLeftMouseButton();
        }

        static void MoveMouseBy(int x, int y) {
            INPUT mouseInput = new INPUT();
            mouseInput.type = SendInputEventType.InputMouse;
            mouseInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_MOVE;
            mouseInput.mkhi.mi.dx = x;
            mouseInput.mkhi.mi.dy = y;
            SendInput(1, ref mouseInput, Marshal.SizeOf(mouseInput));
        }

        static void MoveMouseTo(int x, int y) {
            INPUT mouseInput = new INPUT();
            mouseInput.type = SendInputEventType.InputMouse;
            mouseInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_MOVE|MouseEventFlags.MOUSEEVENTF_ABSOLUTE;
            mouseInput.mkhi.mi.dx = CalculateAbsoluteCoordinateX(x);
            mouseInput.mkhi.mi.dy = CalculateAbsoluteCoordinateY(y);
            SendInput(1, ref mouseInput, Marshal.SizeOf(mouseInput));

        }
        static void DragMouseBy(int x, int y) {

            INPUT mouseInput = new INPUT();
            mouseInput.type = SendInputEventType.InputMouse;
            mouseInput.mkhi.mi.dwFlags =  MouseEventFlags.MOUSEEVENTF_LEFTDOWN;
            SendInput(1, ref mouseInput, Marshal.SizeOf(mouseInput));

            //does not work with MouseEventFlags.MOUSEEVENTF_MOVE | MouseEventFlags.MOUSEEVENTF_LEFTDOWN
            // so two consec. send inputs
            mouseInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_MOVE;
            mouseInput.mkhi.mi.dx = CalculateAbsoluteCoordinateX(x);
            mouseInput.mkhi.mi.dy = CalculateAbsoluteCoordinateY(y);
            SendInput(1, ref mouseInput, Marshal.SizeOf(mouseInput));

            mouseInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_LEFTUP;
            SendInput(1, ref mouseInput, Marshal.SizeOf(mouseInput));

        }
        static void DragMouseTo(int x, int y) {
            INPUT mouseInput = new INPUT();
            mouseInput.type = SendInputEventType.InputMouse;
            mouseInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_LEFTDOWN;
            SendInput(1, ref mouseInput, Marshal.SizeOf(mouseInput));

            mouseInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_MOVE|MouseEventFlags.MOUSEEVENTF_ABSOLUTE;
            mouseInput.mkhi.mi.dx = x;
            mouseInput.mkhi.mi.dy = y;
            SendInput(1, ref mouseInput, Marshal.SizeOf(mouseInput));

            mouseInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_LEFTUP;
            SendInput(1, ref mouseInput, Marshal.SizeOf(mouseInput));
        }
        

        //There's conflict between negative DWOR values and UInt32 so there are two methods
        // for scrolling
        static void ScrollUp(int amount) {
            INPUT mouseInput = new INPUT();
            mouseInput.type = SendInputEventType.InputMouse;
            mouseInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_WHEEL;
            mouseInput.mkhi.mi.mouseData = (UInt32)amount;
            SendInput(1, ref mouseInput, Marshal.SizeOf(mouseInput));
        }

        static void ScrollDown(int amount)
        {
            INPUT mouseInput = new INPUT();
            mouseInput.type = SendInputEventType.InputMouse;
            mouseInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_WHEEL;
            mouseInput.mkhi.mi.mouseData = 0-(UInt32)amount;
            SendInput(1, ref mouseInput, Marshal.SizeOf(mouseInput));
        }


        static void ClickLeftMouseButton()
        {

            INPUT mouseInput = new INPUT();

            mouseInput.type = SendInputEventType.InputMouse;
            mouseInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_LEFTDOWN;
            SendInput(1, ref mouseInput, Marshal.SizeOf(mouseInput));

            //mouseInput.type = SendInputEventType.InputMouse;
            mouseInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_LEFTUP;
            SendInput(1, ref mouseInput, Marshal.SizeOf(mouseInput));

        }
        static void ClickRightMouseButton()
        {
            INPUT mouseInput = new INPUT();

            mouseInput.type = SendInputEventType.InputMouse;
            mouseInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_RIGHTDOWN;
            SendInput(1, ref mouseInput, Marshal.SizeOf(mouseInput));

            //mouseInput.type = SendInputEventType.InputMouse;
            mouseInput.mkhi.mi.dwFlags = MouseEventFlags.MOUSEEVENTF_RIGHTUP;
            SendInput(1, ref mouseInput, Marshal.SizeOf(mouseInput));

        }


        static void getCursorPos()
        {
            POINT p;
            if (GetCursorPos(out p))
            {
                Console.WriteLine(Convert.ToString(p.X) + "x" + Convert.ToString(p.Y));
            }
            else
            {
                Console.WriteLine("unknown");
            }
        }

        static void ScrollCaller(string ammountStr,Boolean up)
        {
            try
            {
                int ammount = int.Parse(ammountStr);
                if (ammount < 0)
                {
                    Console.WriteLine("Scroll ammount must be positive number");
                    System.Environment.Exit(3);
                }
                if (up)
                {
                    ScrollUp(ammount);
                }
                else
                {
                    ScrollDown(ammount);
                }
            }
            catch (Exception)
            {
                Console.WriteLine("Number parsing error");
                System.Environment.Exit(2);
            }

        }

        static int[] parser(String arg) {
            string[] sDim= arg.Split('x');
            if (sDim.Length == 1) {
                Console.WriteLine("Invalid arguments - dimmensions cannot be aquired");
                System.Environment.Exit(6);
            }
            try
            {
                int x = int.Parse(sDim[0]);
                int y = int.Parse(sDim[1]);
                return new int[]{x,y};
            }
            catch (Exception e) {
                Console.WriteLine("Error while parsing dimensions");
                System.Environment.Exit(7);
            }
            return null;

        }

        static void CheckArgs(String[] args) {
            if (args.Length == 1)
            {
                Console.WriteLine("Not enough arguments");
                System.Environment.Exit(1);
            }
        }

       static void PrintHelp() {
            String filename = Environment.GetCommandLineArgs()[0];
            filename = filename.Substring(0, filename.Length);

            Console.WriteLine(filename+" controls the mouse cursor through command line ");
            Console.WriteLine("Usage:");
            Console.WriteLine("");
            Console.WriteLine(filename+" action [arguments]");
            Console.WriteLine("Actions:");
            Console.WriteLine("doubleClick  - double clicks at the current position");
            Console.WriteLine("click - clicks at the current position");
            Console.WriteLine("rightClick - clicks with the right mouse button at the current position");
            Console.WriteLine("position - prints the mouse cursor position");
            Console.WriteLine("scrollUp N - scrolls up the mouse wheel.Requires a number for the scroll ammount");
            Console.WriteLine("scrollDown N - scrolls down the mouse wheel.Requires a number for the scroll ammount");
            Console.WriteLine("moveBy NxM - moves the mouse curosor to relative coordinates.Requires two numbers separated by low case 'x' .");
            Console.WriteLine("moveTo NxM - moves the mouse curosor to absolute coordinates.Requires two numbers separated by low case 'x' .");
            Console.WriteLine("dragBy NxM - drags the mouse curosor to relative coordinates.Requires two numbers separated by low case 'x' .");
            Console.WriteLine("dragTo NxM - drags the mouse curosor to absolute coordinates.Requires two numbers separated by low case 'x' .");
            Console.WriteLine("");
            Console.WriteLine("Consider using only " +filename+" (without extensions) to prevent print of the errormessages after the first start");
            Console.WriteLine("  in case you are using batch-wrapped script.");

        }

        public static void Main(String[] args) {
            if (args.Length == 0) {
                PrintHelp();
                System.Environment.Exit(0);
            }
            if (args[0].ToLower() == "-help" || args[0].ToLower() == "-h") {
                PrintHelp();
                System.Environment.Exit(0);
            }

            if (args[0].ToLower() == "doubleclick")
            {
                DoubleClick();
            }
            else if (args[0].ToLower() == "click" || args[0].ToLower() == "leftclick")
            {
                ClickLeftMouseButton();
            }
            else if (args[0].ToLower() == "position")
            {
                getCursorPos();
            }
            else if (args[0].ToLower() == "rightclick")
            {
                ClickRightMouseButton();
            }
            else if (args[0].ToLower() == "scrollup")
            {
                CheckArgs(args);
                ScrollCaller(args[1], true);

            }
            else if (args[0].ToLower() == "scrolldown")
            {
                CheckArgs(args);
                ScrollCaller(args[1], false);

            }
            else if (args[0].ToLower() == "moveto")
            {
                CheckArgs(args);
                int[] xy = parser(args[1]);
                MoveMouseTo(xy[0], xy[1]);
            }
            else if (args[0].ToLower() == "moveby")
            {
                CheckArgs(args);
                int[] xy = parser(args[1]);
                MoveMouseBy(xy[0], xy[1]);
            }
            else if (args[0].ToLower() == "dragto")
            {
                CheckArgs(args);
                int[] xy = parser(args[1]);
                DragMouseTo(xy[0], xy[1]);
            }
            else if (args[0].ToLower() == "dragby")
            {
                CheckArgs(args);
                int[] xy = parser(args[1]);
                DragMouseBy(xy[0], xy[1]);
            }
            else
            {
                Console.WriteLine("Invalid action : " + args[0]);
                System.Environment.Exit(10);
            }
        }


    }
}
  • 5
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
f1 显示当前程序或者windows的帮助内容。 f2 当你选中一个文件的话,这意味着“重命名” f3 当你在桌面上的时候是打开“查找:所有文件” 对话框 f10或alt 激活当前程序的菜单栏 windows键或ctrl+esc 打开开始菜单 ctrl+alt+delete 在win9x中打开关闭程序对话框 delete 删除被选择的选择项目,如果是文件,将被放入回收站 shift+delete 删除被选择的选择项目,如果是文件,将被直接删除而不是放入回收站 ctrl+n 新建一个新的文件 ctrl+o 打开“打开文件”对话框 ctrl+p 打开“打印”对话框 ctrl+s 保存当前操作文件 ctrl+x 剪切被选择的项目到剪贴板 ctrl+insert 或 ctrl+c 复制被选择的项目到剪贴板 shift+insert 或 ctrl+v 粘贴剪贴板中哪谌莸降鼻拔恢? alt+backspace 或 ctrl+z 撤销上一步的操作 alt+shift+backspace 重做上一步被撤销的操作 windows键+m 最小化所有被打开的窗口。 windows键+ctrl+m 重新将恢复上一项操作前窗口的大小和位置 windows键+e 打开资源管理器 windows键+f 打开“查找:所有文件”对话框 windows键+r 打开“运行”对话框 windows键+break 打开“系统属性”对话框 windows键+ctrl+f 打开“查找:计算机”对话框 shift+f10或鼠标右击 打开当前活动项目的快捷菜单 shift 在放入cd的时候按下不放,可以跳过自动播放cd。在打开word的时候按下不放,可以跳过自启动的宏 alt+f4 关闭当前应用程序 alt+spacebar 打开程序最左上角的菜单 alt+tab 切换当前程序 alt+esc 切换当前程序 alt+enter 将windows下运行的msdos窗口在窗口和全屏幕状态间切换 print screen 将当前屏幕以图象方式拷贝到剪贴板 alt+print screen 将当前活动程序窗口以图象方式拷贝到剪贴板 ctrl+f4 关闭当前应用程序中的当前文本(如word中) ctrl+f6 切换到当前应用程序中的下一个文本(加shift 可以跳到前一个窗口) 在ie中: alt+right arrow 显示前一页(前进键) alt+left arrow 显示后一页(后退键) ctrl+tab 在页面上的各框架中切换(加shift反向) f5 刷新 ctrl+f5 强行刷新 目的快捷键 激活程序中的菜单栏 f10 执行菜单上相应的命令 alt+菜单上带下划线的字母 关闭多文档界面程序中的当 前窗口 ctrl+ f4 关闭当前窗口或退出程序 alt+ f4 复制 ctrl+ c 剪切 ctrl+ x 删除 delete 显示所选对话框项目的帮助 f1 显示当前窗口的系统菜单 alt+空格键 显示所选项目的快捷菜单 shift+ f10 显示“开始”菜单 ctrl+ esc 显示多文档界面程序的系统 菜单 alt+连字号(-) 粘贴 ctr l+ v 切换到上次使用的窗口或者 按住 alt然后重复按tab, 切换到另一个窗口 alt+ tab 撤消 ctrl+ z 二、使用“windows资源管理器”的快捷键 目的快捷键 如果当前选择展开了,要折 叠或者选择父文件夹左箭头 折叠所选的文件夹 num lock+负号(-) 如果当前选择折叠了,要展开 或者选择第一个子文件夹右箭头 展开当前选择下的所有文件夹 num lock+* 展开所选的文件夹 num lock+加号(+) 在左右窗格间切换 f6 三、使用 windows键 可以使用 microsoft自然键盘或含有 windows徽标键的其他任何兼容键盘的以下快捷键。 目的快捷键 在任务栏上的按钮间循环 windows+ tab 显示“查找:所有文件” windows+ f 显示“查找:计算机” ctrl+ windows+ f 显示“帮助” windows+ f1 显示“运行”命令 windows+ r 显示“开始”菜单 windows 显示“系统属性”对话框 windows+ break 显示“windows资源管理器” windows+ e 最小化或还原所有窗口 windows+ d 撤

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值