C# mouse keyboard monitor

/***********************************************************************************
 *                          C# mouse keyboard monitor
 * 说明:
 *     最近想用C#做一个鼠标、键盘模拟器,所以找了点资料模拟一下。
 *
 *                                                 2016-7-10 深圳 南山平山村 曾剑锋
 **********************************************************************************/

一、参考文档:
    1. C# 如何用按钮实现鼠标滚轮操作
        http://blog.csdn.net/jglie/article/details/6872333
    2. c#  mouse_event 模拟鼠标点击事件 绝对位置
        http://blog.sina.com.cn/s/blog_71d894bd01013goa.html
    3. C# Win32API 模拟鼠标移动及点击事件
        http://www.cnblogs.com/08shiyan/archive/2011/07/18/2109086.html
    4. How to: Simulate Mouse and Keyboard Events in Code
        https://msdn.microsoft.com/en-us/library/ms171548.aspx
    5. SendKeys Class
        https://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.aspx
    6. Virtual-Key Codes
        https://msdn.microsoft.com/zh-cn/library/dd375731(v=vs.85).aspx
    7. C#中将字母/字符转换为键盘的key/键值/keycode
        http://www.crifan.com/convert_char_letter_to_key_keycode_in_csharp/
    8. VkKeyScan function
        https://msdn.microsoft.com/en-us/library/ms646329(VS.85).aspx

二、KeyBoard
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;

    namespace MouseMonitorW
    {
        class KeyBoard
        {
            [DllImport("user32.dll")]
            static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
            [DllImport("user32.dll")]
            public static extern Keys VkKeyScan(char ch); 

            public static void sendKey(char key)
            {
                keybd_event((byte)VkKeyScan(key), 0, 0, 0);
                keybd_event((byte)VkKeyScan(key), 0, 2, 0);
            }
        }
    }

三、Mouse:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Runtime.InteropServices;

    namespace MouseMonitorW
    {
        class Mouse
        {
            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_WHEEL         = 0x0800;   // 模拟鼠标滚轮
            private const int MOUSEEVENTF_MIDDLEDOWN    = 0x0020;   // 模拟鼠标中键按下
            private const int MOUSEEVENTF_MIDDLEUP      = 0x0040;   // 模拟鼠标中键抬起
            private const int MOUSEEVENTF_ABSOLUTE      = 0x8000;   // 标示是否采用绝对坐标 

            [DllImport("user32")]
            private static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

            public static void move(int dx, int dy)
            {
                mouse_event(MOUSEEVENTF_MOVE, dx, dy, 0, 0);
            }

            public static void absMove(int x, int y)
            {
                mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, 500, 500, 0, 0);
            }

            public static void wheel(int roll)
            {
                mouse_event(MOUSEEVENTF_WHEEL, 0, 0, roll, 0);
            }

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

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

            public static void right()
            {
                mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
            }

            public static void middle()
            {
                mouse_event(MOUSEEVENTF_MIDDLEUP | MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0);
            }
        }
    }
时间: 2024-10-10 09:57:08

C# mouse keyboard monitor的相关文章

Essential Windows Keyboard Shortcuts

Using keyboard shortcuts help you use your laptop without a mouse Keyboard shortcuts greatly enhance your productivity and save you a whole lot of time. Instead of pointing and clicking with the touchpad or external mouse, you can keep your hands on

Kickstart Options

The following options can be placed in a kickstart file. If you prefer to use a graphical interface for creating your kickstart file, use the Kickstart Configurator application. Refer to Chapter 29, Kickstart Configurator for details. Note If the opt

访问器模式

访问器(visitor)模式 意图:主要将数据结构与数据操作分离.主要解决:稳定的数据结构和易变的操作耦合问题. 代码: #include <iostream> #include <list> using namespace std; class ComputerPartVisitor; class ComputerPart { public: virtual ~ComputerPart() {} public: virtual void accept(ComputerPartVi

Python著名的lib和开发框架(均为转载)

第一,https://github.com/vinta/awesome-python Awesome Python A curated list of awesome Python frameworks, libraries, software and resources. Inspired by awesome-php. Awesome Python Admin Panels Algorithms and Design Patterns Anti-spam Asset Management A

【设计模式】访问者模式

在访问者模式(Visitor Pattern)中,我们使用了一个访问者类,它改变了元素类的执行算法.通过这种方式,元素的执行算法可以随着访问者改变而改变.这种类型的设计模式属于行为型模式.根据模式,元素对象已接受访问者对象,这样访问者对象就可以处理元素对象上的操作. 介绍 意图:主要将数据结构与数据操作分离. 主要解决:稳定的数据结构和易变的操作耦合问题. 何时使用:需要对一个对象结构中的对象进行很多不同的并且不相关的操作,而需要避免让这些操作"污染"这些对象的类,使用访问者模式将这些

简单的贝叶斯分类器的python实现

1 # -*- coding: utf-8 -*- 2 ''' 3 >>> c = Classy() 4 >>> c.train(['cpu', 'RAM', 'ALU', 'io', 'bridge', 'disk'], 'architecture') 5 True 6 >>> c.train(['monitor', 'mouse', 'keyboard', 'microphone', 'headphones'], 'input_devices')

C++--学习笔记011

20150507 所谓继承,就是从父辈得到的属性和行为.是通过继承对父辈的属性和行为进行进一步的细化或者扩充来形成新的类. 在c++中,我们把旧有的类称为基类或者父类,而把从基类继承产生的新类则称为派生类,也称为子类. 派生类(子类)的声明方式 class 派生类名:继承方式 基类名1,继承方式 基类名2... { //派生类新增加的属性; //派生类新增加的行为; } ; 单继承,这个派生类只有一个基类 多继承,这个派生类可以有多个基类 20150508 派生方式有3种 publi,prote

Awesome Python

Awesome Python  A curated list of awesome Python frameworks, libraries, software and resources. Inspired by awesome-php. Awesome Python Environment Management Package Management Package Repositories Distribution Build Tools Interactive Interpreter Fi

Productivity tips, tricks and hacks for academics (2015 edition)

Productivity tips, tricks and hacks for academics (2015 edition) Contents Jump to: My philosophy: Optimize transaction costs. Don't work from home. Eliminate temptation to waste time. Salvage dead time with technology. Get rid of your TV. Taming emai