发送鼠标和放大器;键盘事件 [英] Send mouse & keyboard events

查看:120
本文介绍了发送鼠标和放大器;键盘事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发有一些键或鼠标事件发送到活动窗口的应用程序。

I'm developing an app that have to send some keys or mouse events to the active window.

我使用这个类:
鼠标

using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Mouse
{
    public static class VirtualMouse
    {
        // import the necessary API function so .NET can
        // marshall parameters appropriately
        [DllImport("user32.dll")]
        static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);

        // constants for the mouse_input() API function
        private const int MOUSEEVENTF_MOVE = 0x0001;
        private const int MOUSEEVENTF_LEFTDOWN = 0x0002;
        private const int MOUSEEVENTF_LEFTUP = 0x0004;
        private const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
        private const int MOUSEEVENTF_RIGHTUP = 0x0010;
        private const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
        private const int MOUSEEVENTF_MIDDLEUP = 0x0040;
        private const int MOUSEEVENTF_ABSOLUTE = 0x8000;    

        // simulates movement of the mouse.  parameters specify changes
        // in relative position.  positive values indicate movement
        // right or down
        public static void Move(int xDelta, int yDelta)
        {
            mouse_event(MOUSEEVENTF_MOVE, xDelta, yDelta, 0, 0);
        }

        // simulates movement of the mouse.  parameters specify an
        // absolute location, with the top left corner being the
        // origin
        public static void MoveTo(int x, int y)
        {
            mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, x, y, 0, 0);
        }

        // simulates a click-and-release action of the left mouse
        // button at its current position
        public static void LeftClick()
        {
            mouse_event(MOUSEEVENTF_LEFTDOWN, Control.MousePosition.X, Control.MousePosition.Y, 0, 0);
            mouse_event(MOUSEEVENTF_LEFTUP, Control.MousePosition.X, Control.MousePosition.Y, 0, 0);
        }    
        public static void RightClick()
        {
            mouse_event(MOUSEEVENTF_RIGHTDOWN, Control.MousePosition.X, Control.MousePosition.Y, 0, 0);
            mouse_event(MOUSEEVENTF_RIGHTUP, Control.MousePosition.X, Control.MousePosition.Y, 0, 0);
        }
    }
}

键盘

using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Mouse
{
    public static class VirtualKeyboard
    {
        [DllImport("user32.dll")] static extern uint keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);    
        public static void KeyDown(System.Windows.Forms.Keys key)
        {
            keybd_event((byte)key, 0, 0, 0);
        }

        public static void KeyUp(System.Windows.Forms.Keys key)
        {
            keybd_event((byte)key, 0, 0x7F, 0);
        }
    }
}

这是我的测试code:

this is my testing code:

 private void button1_Click(object sender, EventArgs e)
 {
    Thread.Sleep(2000);            
    VirtualMouse.Move(100, 100);
    VirtualMouse.RightClick();
    VirtualKeyboard.KeyDown(System.Windows.Forms.Keys.A);
    VirtualKeyboard.KeyUp(System.Windows.Forms.Keys.A);
 }

鼠标移动,但不会发送点击。任何想法?
我怎样才能做好一键继续pssed一段时间$ P $?我尝试使用了Thread.Sleep的KeyDown和KeyUp之间它不工作。

Mouse moves, but doesn't send click. Any idea? How can I make a key continue pressed for some time? I tried using thread.sleep between KeyDown and KeyUp and it's not working.

推荐答案

有是codePLEX(微软的开源网站)

There is an open source project on CodePlex (Microsoft's open source website)

视窗模拟输入(C#SendInput包装 - 模拟键盘和鼠标)

Windows Input Simulator (C# SendInput Wrapper - Simulate Keyboard and Mouse)

HTTP://inputsimulator.$c$cplex.com/

它有例子和实际使用简单。

It has examples and real simple to use.

这篇关于发送鼠标和放大器;键盘事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆