精华内容
下载资源
问答
  • Python 技术篇-pyHook键盘鼠标监听事件,监测鼠标键盘按键实例演示 键盘鼠标监听事件。 本来很简单的东西,网上很多文章一堆东西甩在哪,很难看懂,让人感觉有多难。学东西就是要一步一步的来,咱们先入门,然后再去...

    本来很简单的东西,网上很多文章一堆东西甩在哪,很难看懂,让人感觉有多难。学东西就是要一步一步的来,咱们先入门,然后再去看那些更高级的内容,下面我就来给大家演示。

    我们用到的库有 pyHook 和 pywin32 这两个库。
    pywin32pyHook 的安装教程

    pythoncom 就是属于 pywin32 的。

    import pyHook
    import pythoncom
    
    # 监听到鼠标事件调用
    def onMouseEvent(event):
    	if(event.MessageName!="mouse move"):# 因为鼠标一动就会有很多mouse move,所以把这个过滤下
            print(event.MessageName)
        return True # 为True才会正常调用,如果为False的话,此次事件被拦截
    
    # 监听到键盘事件调用
    def onKeyboardEvent(event):
        print(event.Key)# 返回按下的键
        return True
    
    def main():
    	# 创建管理器
        hm = pyHook.HookManager()
        # 监听键盘
        hm.KeyDown = onKeyboardEvent   
        hm.HookKeyboard()  
        # 监听鼠标 
        hm.MouseAll = onMouseEvent   
        hm.HookMouse()
        # 循环监听
        pythoncom.PumpMessages() 
     
    if __name__ == "__main__":
        main()
    

    效果如下:
    在这里插入图片描述
    喜欢的点个赞❤吧!

    展开全文
  • Python 键盘鼠标监听

    千次阅读 2018-11-08 22:07:14
    Python 键盘鼠标监听

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

    也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

                         

    异想天开的想记录一下自己每天的键盘键位走向,于是就在网上搜索了一下相关的实现,然后就发现了一个第三方的库pyHook.封装的很好,我们只需要傻瓜式的调用里面的API就可以了。

    下面是我在使用pyHook 的过程中遇到的问题和经验总结。

    准备工作

    • 操作系统: windows7 64位旗舰版

    • Python版本: 2.7.11

    • 第三方库:

    • IDE: PyCharm 2016.2 pro

    API 基础

    PyHook是一个基于Python的“钩子”库,主要用于监听当前电脑上鼠标和键盘的事件。这个库依赖于另一个Python库PyWin32,如同名字所显示的,PyWin32只能运行在Windows平台,所以PyHook也只能运行在Windows平台。

    pyHook里面最重要的大致有三个API比较常用。如下:


    • HookManager()

     

    创建一个“钩子”管家,便于接下的全局设置

  • HookManager.onMouseEvent  +  HookManager.HookMouse()

     

    监听鼠标事件,并且设置鼠标钩子。这两个应该作为原子操作同时出现。

  • HookManager.onKeyboardEvent + HookManager.HookKeyboard()
     

    监听键盘事件,并且设置键盘钩子,同样这两个应该同时出现。

  •  

    案例展示

    下面是我自己写的一个测试代码,具体内容如下:

    # coding:utf-8#    __author__ = 'Mark sinoberg'#    __date__ = '2016/6/24'#    __Desc__ = 简单的入门示例程序import pyHookimport pythoncomfrom time import *result = ''def onMouseEvent(event):    # 监听鼠标事件    print "Message Name :", event.MessageName    print "Message:",event.Message    print "Time: " , event.Time    print "Window: ",event.Window    print "Window Name : " ,event.WindowName    print "Position : ",event.Position    print "Wheel : ",event.Wheel    print "Injected: ",event.Injected    # 需要注意的是返回True,以便将事件传给其他的处理程序,如果返回False的话,鼠标事件在这里就会被拦截,即鼠标会僵在此处失去响应    return Truedef onKeyboardEvent(event):    # 监听键盘事件    print "MessageName:", event.MessageName    print "Message:", event.Message    print "Time:", event.Time    print "Window:", event.Window    print "WindowName:", event.WindowName    print "Ascii:", event.Ascii, chr(event.Ascii)    print "Key:", event.Key    print "KeyID:", event.KeyID    print "ScanCode:", event.ScanCode    print "Extended:", event.Extended    print "Injected:", event.Injected    print "Alt", event.Alt    print "Transition", event.Transition    print "---"    # 同鼠标事件监听函数的返回值    # 写一个保存到本地文件的方法,而且应该以写二进制的方式来写入,设置result为全局的,避免文件被覆盖    global result    file = open(r'F:/log.txt','wb')    result = result + "Time : " + str(asctime())+"|:"+"WindowName:"+str( event.WindowName)+"|"+"Key:"+str( event.Key)+"|"+"MessageName:"+str( event.MessageName)    file.writelines(result)    if event.Key == "q":        file.close()    return Truedef main():    # 创建一个:钩子“管理对象    hm = pyHook.HookManager()    # 监听所有的键盘事件    hm.KeyDown = onKeyboardEvent    #设置键盘”钩子“    hm.HookKeyboard()    # 监听鼠标事件    hm.mouseAll = onMouseEvent    # 设置鼠标钩子    hm.HookMouse()    # 进入循环侦听,需要手动进行关闭,否则程序将一直处于监听的状态。可以直接设置而空而使用默认值    pythoncom.PumpMessages()    # 我也不知道为什么直接放置到main函数中不管用if __name__ == "__main__":    main()
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68

    global result 的设置,这样就可以记录下来之前的result来保证之前的数据不被覆盖!

    测试结果

    验证键盘监听结果

    这是实时的检测结果,是我在程序中添加设置result为全局的,避免文件被覆盖
    时所截获的数据流。

    问题分析

    在上面的代码中,注释已经做的很详细了。但还是有一些奇怪的现象要讲一讲:

    • main函数:这里单独定义了一个main函数,然后让主函数来进行调用。也许,你会认为这不是多此一举吗?然而,事实是只有这样才能让代码顺利执行下去。真的是有悖常理。

    • 使用了全局变量result: 这里就是为了防止在向文件中写入数据的时候覆盖之前的文件信息 。

    • 在判断键盘按下Q键的时候,停止键盘事件的记录。如果想彻底的停止程序的监听的话,需要调用win32api.PostQuitMessage().

    拓展

    有了这些基础性的知识之后,就可以大胆的思考做一些有意思的东西了。除了记录自己在电脑上的行为,还可以将这些数据整合起来,利用数据挖掘的相关的技术,做更加高深的处理。

    或者,你想偷偷的知道别人在电脑上的操作行为,这些都可以很愉快的实现。

    而且通过发邮件的形式或者链接远程服务器的方式同样可以远程的进行更Hack的操作。

    这里只是抛砖引玉,有兴趣的不妨在下面留言哦。

               

    给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

    这里写图片描述
    展开全文
  • Ubuntu 键盘鼠标失效怎么办?详解!

    万次阅读 多人点赞 2019-03-19 19:01:10
    ubuntu 键盘鼠标失效怎么办?详解!Ubuntu : the keyboard and mouse not work!问题描述:启动Ubuntu键盘鼠标失效/失灵,只能通过电源键重启,重启仍解决不了问题。环境:Ubuntu 18.04 + Windows 10 双系统1. 重启...

    Ubuntu : the keyboard and mouse not work!

    问题描述:启动Ubuntu键盘鼠标失效/失灵,只能通过电源键重启,重启仍解决不了问题。

    今天打开Ubuntu,键盘鼠标失灵了!!!查找国内博客解决方案发现都无法解决(气…,秉着不成功便成仁的信念,搞了整整3个小时,终于搞定,长出一口气!废话不多说,直接开干!

    参考谷歌:这篇博文完美地解决了我的问题。

    环境:Ubuntu 18.04 + Windows 10 双系统

    1. 重启系统后按“ESC”进入grub引导界面:

    这里看个人电脑情况,楼主按一下“ESC”就可以进入了。

    2. 在引导界面选择 Advanced Options
    3. 选择 带有(Recovery mode )的选项
    4. 接着选择 Network 并点 yes
    5. 继续选择 Drop to root shell prompt,并点“Enter”
    6. 输入:mount -o rw,remount /
    7. 再输入:apt install xserver-xorg-input-all (这里确保电脑联网)

    等待安装配置完毕,搞定!!
    最后按 “Ctrl+D” 退出, 输入 “sudo reboot” 重启就完事了!

    弄完之后,鼠标键盘的灯终于亮了!

    版权声明:转载请注明原文链接!谢谢!

    展开全文
  • C#键盘鼠标钩子

    千次阅读 2015-08-25 16:41:12
    亲测可用 /* * 老外写的一个C#钩子类,写的非常好... * 捕获键盘鼠标所有事件 * 模拟鼠标和模拟键盘操作 * * */ /// /// 鼠标和键盘钩子的抽象类 /// public abstract class GlobalHook { #region Wind

    亲测可用

        /*
         * 老外写的一个C#钩子类,写的非常好。
         * 捕获键盘鼠标所有事件
         * 模拟鼠标和模拟键盘操作
         * 
         * */
        /// <summary>  
        /// 鼠标和键盘钩子的抽象类
        /// </summary>  
        public abstract class GlobalHook
        {
    
            #region Windows API Code
    
            [StructLayout(LayoutKind.Sequential)]
            protected class POINT
            {
                public int x;
                public int y;
            }
    
            [StructLayout(LayoutKind.Sequential)]
            protected class MouseHookStruct
            {
                public POINT pt;
                public int hwnd;
                public int wHitTestCode;
                public int dwExtraInfo;
            }
    
            [StructLayout(LayoutKind.Sequential)]
            protected class MouseLLHookStruct
            {
                public POINT pt;
                public int mouseData;
                public int flags;
                public int time;
                public int dwExtraInfo;
            }
    
            [StructLayout(LayoutKind.Sequential)]
            protected class KeyboardHookStruct
            {
                public int vkCode;
                public int scanCode;
                public int flags;
                public int time;
                public int dwExtraInfo;
            }
    
            [DllImport("user32.dll", CharSet = CharSet.Auto,
               CallingConvention = CallingConvention.StdCall, SetLastError = true)]
            protected static extern int SetWindowsHookEx(
                int idHook,
                HookProc lpfn,
                IntPtr hMod,
                int dwThreadId);
    
            [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            private static extern IntPtr GetModuleHandle(String modulename);
    
            [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            private static extern IntPtr SetWindowsHookExW(
                        int idHook,
                        HookProc lpfn,
                        IntPtr hmod,
                        uint dwThreadID);
    
    
            [DllImport("user32.dll", CharSet = CharSet.Auto,
                CallingConvention = CallingConvention.StdCall, SetLastError = true)]
            protected static extern int UnhookWindowsHookEx(int idHook);
    
    
            [DllImport("user32.dll", CharSet = CharSet.Auto,
                 CallingConvention = CallingConvention.StdCall)]
            protected static extern int CallNextHookEx(
                int idHook,
                int nCode,
                int wParam,
                IntPtr lParam);
    
            [DllImport("user32")]
            protected static extern int ToAscii(
                int uVirtKey,
                int uScanCode,
                byte[] lpbKeyState,
                byte[] lpwTransKey,
                int fuState);
    
            [DllImport("user32")]
            protected static extern int GetKeyboardState(byte[] pbKeyState);
    
            [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
            protected static extern short GetKeyState(int vKey);
    
            protected delegate int HookProc(int nCode, int wParam, IntPtr lParam);
    
            protected const int WH_MOUSE_LL = 14;
            protected const int WH_KEYBOARD_LL = 13;
    
            protected const int WH_MOUSE = 7;
            protected const int WH_KEYBOARD = 2;
            protected const int WM_MOUSEMOVE = 0x200;
            protected const int WM_LBUTTONDOWN = 0x201;
            protected const int WM_RBUTTONDOWN = 0x204;
            protected const int WM_MBUTTONDOWN = 0x207;
            protected const int WM_LBUTTONUP = 0x202;
            protected const int WM_RBUTTONUP = 0x205;
            protected const int WM_MBUTTONUP = 0x208;
            protected const int WM_LBUTTONDBLCLK = 0x203;
            protected const int WM_RBUTTONDBLCLK = 0x206;
            protected const int WM_MBUTTONDBLCLK = 0x209;
            protected const int WM_MOUSEWHEEL = 0x020A;
            protected const int WM_KEYDOWN = 0x100;
            protected const int WM_KEYUP = 0x101;
            protected const int WM_SYSKEYDOWN = 0x104;
            protected const int WM_SYSKEYUP = 0x105;
    
            protected const byte VK_SHIFT = 0x10;
            protected const byte VK_CAPITAL = 0x14;
            protected const byte VK_NUMLOCK = 0x90;
    
            protected const byte VK_LSHIFT = 0xA0;
            protected const byte VK_RSHIFT = 0xA1;
            protected const byte VK_LCONTROL = 0xA2;
            protected const byte VK_RCONTROL = 0x3;
            protected const byte VK_LALT = 0xA4;
            protected const byte VK_RALT = 0xA5;
    
            protected const byte LLKHF_ALTDOWN = 0x20;
    
            #endregion
    
            #region Private Variables
    
            protected int _hookType;
            protected int _handleToHook;
            protected bool _isStarted;
            protected HookProc _hookCallback;
    
            #endregion
    
            #region Properties
    
            public bool IsStarted
            {
                get
                {
                    return _isStarted;
                }
            }
    
            #endregion
    
            #region Constructor
    
            public GlobalHook()
            {
                //绑定ApplicationExit事件
                Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
    
            }
    
            #endregion
    
            #region Methods
    
            public void Start()
            {
    
                if (!_isStarted &&
                    _hookType != 0)
                {
    
                    //确保_hookCallback不是一个空的引用
                    //如果是,GC会随机回收它,并且一个空的引用会爆出异常
                    _hookCallback = new HookProc(HookCallbackProcedure);
                    using (Process curPro = Process.GetCurrentProcess())
                    {
                        using (ProcessModule curMod = curPro.MainModule)
                        {
                            _handleToHook = (int)SetWindowsHookExW(
                                _hookType,
                                _hookCallback,
                                 GetModuleHandle(curMod.ModuleName),
                                0);
                        }
                    }
                    // 钩成功
                    if (_handleToHook != 0)
                    {
                        _isStarted = true;
                    }
    
                }
    
            }
    
            public void Stop()
            {
    
                if (_isStarted)
                {
                    UnhookWindowsHookEx(_handleToHook);
                    _isStarted = false;
                }
    
            }
            /// <summary>
            /// 钩子回调函数
            /// </summary>
            /// <param name="nCode"></param>
            /// <param name="wParam"></param>
            /// <param name="lParam"></param>
            /// <returns></returns>
            protected virtual int HookCallbackProcedure(int nCode, Int32 wParam, IntPtr lParam)
            {
    
                // This method must be overriden by each extending hook  
                return 0;
    
            }
            /// <summary>
            /// 程序退出方法
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void Application_ApplicationExit(object sender, EventArgs e)
            {
    
                if (_isStarted)
                {
                    Stop();
                }
    
            }
    
            #endregion
    
        }
    
        /// <summary>  
        /// 键盘钩子类
        /// </summary>  
        public class KeyboardHook : GlobalHook
        {
    
            #region Events
    
            public event KeyEventHandler KeyDown;
            public event KeyEventHandler KeyUp;
            public event KeyPressEventHandler KeyPress;
    
            #endregion
    
            #region Constructor
    
            public KeyboardHook()
            {
    
                _hookType = WH_KEYBOARD_LL;
    
            }
    
            #endregion
    
            #region Methods
    
            protected override int HookCallbackProcedure(int nCode, int wParam, IntPtr lParam)
            {
    
                bool handled = false;
    
                if (nCode > -1 && (KeyDown != null || KeyUp != null || KeyPress != null))
                {
    
                    KeyboardHookStruct keyboardHookStruct =
                        (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
    
                    // Is Control being held down?  
                    bool control = ((GetKeyState(VK_LCONTROL) & 0x80) != 0) ||
                                   ((GetKeyState(VK_RCONTROL) & 0x80) != 0);
    
                    // Is Shift being held down?  
                    bool shift = ((GetKeyState(VK_LSHIFT) & 0x80) != 0) ||
                                 ((GetKeyState(VK_RSHIFT) & 0x80) != 0);
    
                    // Is Alt being held down?  
                    bool alt = ((GetKeyState(VK_LALT) & 0x80) != 0) ||
                               ((GetKeyState(VK_RALT) & 0x80) != 0);
    
                    // Is CapsLock on?  
                    bool capslock = (GetKeyState(VK_CAPITAL) != 0);
    
                    // Create event using keycode and control/shift/alt values found above  
                    KeyEventArgs e = new KeyEventArgs(
                        (Keys)(
                            keyboardHookStruct.vkCode |
                            (control ? (int)Keys.Control : 0) |
                            (shift ? (int)Keys.Shift : 0) |
                            (alt ? (int)Keys.Alt : 0)
                            ));
    
                    // Handle KeyDown and KeyUp events  
                    switch (wParam)
                    {
    
                        case WM_KEYDOWN:
                        case WM_SYSKEYDOWN:
                            if (KeyDown != null)
                            {
                                KeyDown(this, e);
                                handled = handled || e.Handled;
                            }
                            break;
                        case WM_KEYUP:
                        case WM_SYSKEYUP:
                            if (KeyUp != null)
                            {
                                KeyUp(this, e);
                                handled = handled || e.Handled;
                            }
                            break;
    
                    }
    
                    // Handle KeyPress event  
                    if (wParam == WM_KEYDOWN &&
                       !handled &&
                       !e.SuppressKeyPress &&
                        KeyPress != null)
                    {
    
                        byte[] keyState = new byte[256];
                        byte[] inBuffer = new byte[2];
                        GetKeyboardState(keyState);
    
                        if (ToAscii(keyboardHookStruct.vkCode,
                                  keyboardHookStruct.scanCode,
                                  keyState,
                                  inBuffer,
                                  keyboardHookStruct.flags) == 1)
                        {
    
                            char key = (char)inBuffer[0];
                            if ((capslock ^ shift) && Char.IsLetter(key))
                                key = Char.ToUpper(key);
                            KeyPressEventArgs e2 = new KeyPressEventArgs(key);
                            KeyPress(this, e2);
                            handled = handled || e.Handled;
    
                        }
    
                    }
    
                }
    
                if (handled)
                {
                    return 1;
                }
                else
                {
                    return CallNextHookEx(_handleToHook, nCode, wParam, lParam);
                }
    
            }
    
            #endregion
    
        }
    
        /// <summary>  
        /// 鼠标钩子类
        /// </summary>  
        public class MouseHook : GlobalHook
        {
    
            #region MouseEventType Enum
    
            private enum MouseEventType
            {
                None,
                MouseDown,
                MouseUp,
                DoubleClick,
                MouseWheel,
                MouseMove
            }
    
            #endregion
    
            #region Events
            /// <summary>
            /// 鼠标按下事件
            /// </summary>
            public event MouseEventHandler MouseDown;
            /// <summary>
            /// 送开鼠标事件
            /// </summary>
            public event MouseEventHandler MouseUp;
            /// <summary>
            /// 鼠标移动事件
            /// </summary>
            public event MouseEventHandler MouseMove;
            /// <summary>
            /// 鼠标滚轮事件
            /// </summary>
            public event MouseEventHandler MouseWheel;
            /// <summary>
            /// 鼠标单击事件
            /// </summary>
            public event EventHandler Click;
            /// <summary>
            /// 鼠标双击事件
            /// </summary>
            public event EventHandler DoubleClick;
    
            #endregion
    
            #region Constructor
    
            public MouseHook()
            {
    
                _hookType = WH_MOUSE_LL;
    
            }
    
            #endregion
    
            #region Methods
            /// <summary>
            /// 钩子回调函数
            /// </summary>
            /// <param name="nCode"></param>
            /// <param name="wParam"></param>
            /// <param name="lParam"></param>
            /// <returns></returns>
            protected override int HookCallbackProcedure(int nCode, int wParam, IntPtr lParam)
            {
    
                if (nCode > -1 && (MouseDown != null || MouseUp != null || MouseMove != null))
                {
    
                    MouseLLHookStruct mouseHookStruct =
                        (MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseLLHookStruct));
    
                    MouseButtons button = GetButton(wParam);
                    MouseEventType eventType = GetEventType(wParam);
    
                    MouseEventArgs e = new MouseEventArgs(
                        button,
                        (eventType == MouseEventType.DoubleClick ? 2 : 1),
                        mouseHookStruct.pt.x,
                        mouseHookStruct.pt.y,
                        (eventType == MouseEventType.MouseWheel ? (short)((mouseHookStruct.mouseData >> 16) & 0xffff) : 0));
    
                    // Prevent multiple Right Click events (this probably happens for popup menus)  
                    if (button == MouseButtons.Right && mouseHookStruct.flags != 0)
                    {
                        eventType = MouseEventType.None;
                    }
    
                    switch (eventType)
                    {
                        case MouseEventType.MouseDown:
                            if (MouseDown != null)
                            {
                                MouseDown(this, e);
                            }
                            break;
                        case MouseEventType.MouseUp:
                            if (Click != null)
                            {
                                Click(this, new EventArgs());
                            }
                            if (MouseUp != null)
                            {
                                MouseUp(this, e);
                            }
                            break;
                        case MouseEventType.DoubleClick:
                            if (DoubleClick != null)
                            {
                                DoubleClick(this, new EventArgs());
                            }
                            break;
                        case MouseEventType.MouseWheel:
                            if (MouseWheel != null)
                            {
                                MouseWheel(this, e);
                            }
                            break;
                        case MouseEventType.MouseMove:
                            if (MouseMove != null)
                            {
                                MouseMove(this, e);
                            }
                            break;
                        default:
                            break;
                    }
    
                }
    
                return CallNextHookEx(_handleToHook, nCode, wParam, lParam);
    
            }
    
            private MouseButtons GetButton(Int32 wParam)
            {
    
                switch (wParam)
                {
    
                    case WM_LBUTTONDOWN:
                    case WM_LBUTTONUP:
                    case WM_LBUTTONDBLCLK:
                        return MouseButtons.Left;
                    case WM_RBUTTONDOWN:
                    case WM_RBUTTONUP:
                    case WM_RBUTTONDBLCLK:
                        return MouseButtons.Right;
                    case WM_MBUTTONDOWN:
                    case WM_MBUTTONUP:
                    case WM_MBUTTONDBLCLK:
                        return MouseButtons.Middle;
                    default:
                        return MouseButtons.None;
    
                }
    
            }
    
            private MouseEventType GetEventType(Int32 wParam)
            {
    
                switch (wParam)
                {
    
                    case WM_LBUTTONDOWN:
                    case WM_RBUTTONDOWN:
                    case WM_MBUTTONDOWN:
                        return MouseEventType.MouseDown;
                    case WM_LBUTTONUP:
                    case WM_RBUTTONUP:
                    case WM_MBUTTONUP:
                        return MouseEventType.MouseUp;
                    case WM_LBUTTONDBLCLK:
                    case WM_RBUTTONDBLCLK:
                    case WM_MBUTTONDBLCLK:
                        return MouseEventType.DoubleClick;
                    case WM_MOUSEWHEEL:
                        return MouseEventType.MouseWheel;
                    case WM_MOUSEMOVE:
                        return MouseEventType.MouseMove;
                    default:
                        return MouseEventType.None;
    
                }
            }
    
            #endregion
    
        }
    
        /// <summary>  
        /// 模拟键盘事件类
        /// </summary>  
        public static class KeyboardSimulator
        {
    
            #region Standard Keyboard Shortcuts Enum
    
            /// <summary>  
            ///  标准的人体工程键盘快捷键 
            /// </summary>  
            public enum StandardShortcut
            {
                Copy,
                Cut,
                Paste,
                SelectAll,
                Save,
                Open,
                New,
                Close,
                Print
            }
    
            #endregion
    
            #region Windows API Code
    
            const int KEYEVENTF_EXTENDEDKEY = 0x1;
            const int KEYEVENTF_KEYUP = 0x2;
    
            [DllImport("user32.dll")]
            static extern void keybd_event(byte key, byte scan, int flags, int extraInfo);
    
            #endregion
    
            #region Methods
    
            public static void KeyDown(Keys key)
            {
                keybd_event(ParseKey(key), 0, 0, 0);
            }
    
            public static void KeyUp(Keys key)
            {
                keybd_event(ParseKey(key), 0, KEYEVENTF_KEYUP, 0);
            }
    
            public static void KeyPress(Keys key)
            {
                KeyDown(key);
                KeyUp(key);
            }
    
            public static void SimulateStandardShortcut(StandardShortcut shortcut)
            {
                switch (shortcut)
                {
                    case StandardShortcut.Copy:
                        KeyDown(Keys.Control);
                        KeyPress(Keys.C);
                        KeyUp(Keys.Control);
                        break;
                    case StandardShortcut.Cut:
                        KeyDown(Keys.Control);
                        KeyPress(Keys.X);
                        KeyUp(Keys.Control);
                        break;
                    case StandardShortcut.Paste:
                        KeyDown(Keys.Control);
                        KeyPress(Keys.V);
                        KeyUp(Keys.Control);
                        break;
                    case StandardShortcut.SelectAll:
                        KeyDown(Keys.Control);
                        KeyPress(Keys.A);
                        KeyUp(Keys.Control);
                        break;
                    case StandardShortcut.Save:
                        KeyDown(Keys.Control);
                        KeyPress(Keys.S);
                        KeyUp(Keys.Control);
                        break;
                    case StandardShortcut.Open:
                        KeyDown(Keys.Control);
                        KeyPress(Keys.O);
                        KeyUp(Keys.Control);
                        break;
                    case StandardShortcut.New:
                        KeyDown(Keys.Control);
                        KeyPress(Keys.N);
                        KeyUp(Keys.Control);
                        break;
                    case StandardShortcut.Close:
                        KeyDown(Keys.Alt);
                        KeyPress(Keys.F4);
                        KeyUp(Keys.Alt);
                        break;
                    case StandardShortcut.Print:
                        KeyDown(Keys.Control);
                        KeyPress(Keys.P);
                        KeyUp(Keys.Control);
                        break;
                }
            }
    
            static byte ParseKey(Keys key)
            {
    
                // Alt, Shift, and Control need to be changed for API function to work with them  
                switch (key)
                {
                    case Keys.Alt:
                        return (byte)18;
                    case Keys.Control:
                        return (byte)17;
                    case Keys.Shift:
                        return (byte)16;
                    default:
                        return (byte)key;
                }
    
            }
    
            #endregion
    
        }
    
    
        /// <summary>  
        /// 模拟鼠标事件类
        /// </summary>  
        public static class MouseSimulator
        {
            /// <summary>  
            /// And X, Y point on the screen  
            /// </summary>  
            public struct MousePoint
            {
    
                public MousePoint(Point p)
                {
                    X = p.X;
                    Y = p.Y;
                }
    
                public int X;
                public int Y;
    
                public static implicit operator Point(MousePoint p)
                {
                    return new Point(p.X, p.Y);
                }
    
            }
    
            /// <summary>  
            /// Mouse buttons that can be pressed  
            /// </summary>  
            public enum MouseButton
            {
                Left = 0x2,
                Right = 0x8,
                Middle = 0x20
            }
    
            #region Windows API Code
    
            [DllImport("user32.dll")]
            static extern int ShowCursor(bool show);
    
            [DllImport("user32.dll")]
            static extern void mouse_event(int flags, int dX, int dY, int buttons, int extraInfo);
    
            const int MOUSEEVENTF_MOVE = 0x1;
            const int MOUSEEVENTF_LEFTDOWN = 0x2;
            const int MOUSEEVENTF_LEFTUP = 0x4;
            const int MOUSEEVENTF_RIGHTDOWN = 0x8;
            const int MOUSEEVENTF_RIGHTUP = 0x10;
            const int MOUSEEVENTF_MIDDLEDOWN = 0x20;
            const int MOUSEEVENTF_MIDDLEUP = 0x40;
            const int MOUSEEVENTF_WHEEL = 0x800;
            const int MOUSEEVENTF_ABSOLUTE = 0x8000;
    
            #endregion
    
            #region Properties
    
            /// <summary>  
            /// Gets or sets a structure that represents both X and Y mouse coordinates  
            /// </summary>  
            public static MousePoint Position
            {
                get
                {
                    return new MousePoint(Cursor.Position);
                }
                set
                {
                    Cursor.Position = value;
                }
            }
    
            /// <summary>  
            /// Gets or sets only the mouse's x coordinate  
            /// </summary>  
            public static int X
            {
                get
                {
                    return Cursor.Position.X;
                }
                set
                {
                    Cursor.Position = new Point(value, Y);
                }
            }
    
            /// <summary>  
            /// Gets or sets only the mouse's y coordinate  
            /// </summary>  
            public static int Y
            {
                get
                {
                    return Cursor.Position.Y;
                }
                set
                {
                    Cursor.Position = new Point(X, value);
                }
            }
    
            #endregion
    
            #region Methods
    
            /// <summary>  
            /// Press a mouse button down  
            /// </summary>  
            /// <param name="button"></param>  
            public static void MouseDown(MouseButton button)
            {
                mouse_event(((int)button), 0, 0, 0, 0);
            }
    
            public static void MouseDown(MouseButtons button)
            {
                switch (button)
                {
                    case MouseButtons.Left:
                        MouseDown(MouseButton.Left);
                        break;
                    case MouseButtons.Middle:
                        MouseDown(MouseButton.Middle);
                        break;
                    case MouseButtons.Right:
                        MouseDown(MouseButton.Right);
                        break;
                }
            }
    
            /// <summary>  
            /// Let a mouse button up  
            /// </summary>  
            /// <param name="button"></param>  
            public static void MouseUp(MouseButton button)
            {
                mouse_event(((int)button) * 2, 0, 0, 0, 0);
            }
    
            public static void MouseUp(MouseButtons button)
            {
                switch (button)
                {
                    case MouseButtons.Left:
                        MouseUp(MouseButton.Left);
                        break;
                    case MouseButtons.Middle:
                        MouseUp(MouseButton.Middle);
                        break;
                    case MouseButtons.Right:
                        MouseUp(MouseButton.Right);
                        break;
                }
            }
    
            /// <summary>  
            /// Click a mouse button (down then up)  
            /// </summary>  
            /// <param name="button"></param>  
            public static void Click(MouseButton button)
            {
                MouseDown(button);
                MouseUp(button);
            }
    
            public static void Click(MouseButtons button)
            {
                switch (button)
                {
                    case MouseButtons.Left:
                        Click(MouseButton.Left);
                        break;
                    case MouseButtons.Middle:
                        Click(MouseButton.Middle);
                        break;
                    case MouseButtons.Right:
                        Click(MouseButton.Right);
                        break;
                }
            }
    
            /// <summary>  
            /// Double click a mouse button (down then up twice)  
            /// </summary>  
            /// <param name="button"></param>  
            public static void DoubleClick(MouseButton button)
            {
                Click(button);
                Click(button);
            }
    
            public static void DoubleClick(MouseButtons button)
            {
                switch (button)
                {
                    case MouseButtons.Left:
                        DoubleClick(MouseButton.Left);
                        break;
                    case MouseButtons.Middle:
                        DoubleClick(MouseButton.Middle);
                        break;
                    case MouseButtons.Right:
                        DoubleClick(MouseButton.Right);
                        break;
                }
            }
    
            /// <summary>  
            /// Show a hidden current on currently application  
            /// </summary>  
            public static void Show()
            {
                ShowCursor(true);
            }
    
            /// <summary>  
            /// Hide mouse cursor only on current application's forms  
            /// </summary>  
            public static void Hide()
            {
                ShowCursor(false);
            }
    
            #endregion
    
        }  


     

     

    展开全文
  • 安装系统键盘鼠标无法使用

    万次阅读 2016-08-30 22:10:42
    安装系统键盘鼠标无法使用 win7原版系统键盘鼠标失灵 安装win7系统无法识别鼠标键盘
  • ubuntu系统键盘鼠标失灵的修复方案

    千次阅读 2020-04-17 18:51:00
    ubuntu键盘鼠标驱动修复事情的起因导致键盘鼠标失灵的原因解决方案 事情的起因 前一天安装了某个软件,结果第二天进系统的时候卡在了登录界面,按啥键都没反应,鼠标也动不了,一开始以为仅仅是卡死,结果用了以前的...
  • Vue监听键盘鼠标事件

    千次阅读 2019-04-24 16:05:16
    vue页面监听键盘鼠标等事件。 官方给的例子是在input标签中的,我们想要的效果是不使用固定标签。 2 实现原理 2.1 增加监听 mounted () { window.addEventListener('keyup',this.handleKeyup) window....
  • 无需显示器键盘鼠标,只需网线连接,给树莓派4B安装64位操作系统。
  • 键盘鼠标模拟全知道

    千次阅读 2017-08-01 02:23:05
    一、基于windows 消息机制的鼠标键盘模拟  (一)、应用程序级模拟 ... 2、 SendInput函数模拟全局键盘鼠标事件  3、用全局钩子模拟键盘消息 二、驱动级模拟 ********************************
  • 飞飞键盘鼠标连点器是一个模拟键盘鼠标的绿色免费小软件,简体中文,免安装,方便实用!该自动点击器类似按键精灵、按键游侠等键盘鼠标模拟软件,但飞飞键盘鼠标连点器抛弃了很多不常用的功能,涵盖了键盘鼠标的常见...
  • 这次主要说说键盘鼠标的习惯。 Mac电脑键盘与PC Windows键盘布局不同,主要是左下角经常用到的几个功能键,但MacOS系统已经替用户考虑到了这点,可以灵活的设置,具体如何设置,取决于使用者是把键盘、鼠标当成Mac...
  • VR项目添加键盘鼠标控制

    千次阅读 2017-11-23 17:52:48
    VR项目添加键盘鼠标控制使用VR模版创建的项目,程序运行后,只能使用VR头盔进行交互场景,不便于调试工作,为此,在场景中添加键盘、鼠标控制功能。1 .键盘控制添加:键盘控制主要为ASDW 键控制前后和移动,具体步骤...
  • 嵌入式QT的键盘鼠标和触摸屏支持

    千次阅读 2020-09-18 13:52:19
    在不使用触摸屏的情况下,嵌入式QT对多个键盘鼠标,包括热拔插,都是支持的很好的 但是一旦使用了触摸屏,就会发现鼠标失效了,因为使用触摸屏,必然要使用tslib进行校准,否则屏幕的箭头是很不准的,使用了tslib,...
  • Windows编程之键盘鼠标钩子

    千次阅读 2019-05-18 21:07:22
    以一个简单的钩子程序为例,屏蔽所有键盘鼠标消息,按下F1停止钩子 #include<windows.h> HHOOK hook,hook2; LRESULT CALLBACK kbdProc(int code, WPARAM wParam, LPARAM lParam); int WINAPI WinMain...
  • matplotlib键盘鼠标事件(tcy)

    千次阅读 2018-12-03 19:15:17
    键盘鼠标事件:2018/10/24 https://matplotlib.org/users/event_handling.html?highlight=event%20inaxes --------------------------------------------------------------------------------- 1.1.鼠标事件 #...
  • 解决 ubuntu 开机后键盘鼠标失灵!

    千次阅读 2019-05-31 13:17:00
    近期不知安装了什么package,导致 ubuntu 开机后键盘鼠标一直没法用,刚开始以为是 ubuntu 桌面环境崩溃了,后来发现系统能显示连接到网络、时间也在运行,那应该就是键盘鼠标失灵了。 网上查了一下,应该是 x...
  • 搬家或搬办公室经常会遇到PS2接口类型的键盘鼠标不能用问题   1、看看是否插错接口,可根据ps2接口颜色区分是否插错 2、切记:PS2接口不支持热插拔,所以请关机后再插再启动电脑...
  • 虚幻4键盘鼠标控制输入节点
  • 利用PyUserInput记录键盘鼠标事件信息

    千次阅读 2018-04-14 22:36:26
    群皮最近很皮,想弄一些没啥卵用的东西,想记录键盘鼠标信息,例如鼠标点击坐标,鼠标移动轨迹,点击间隔,键盘按键,按键点击间隔等等,(不要小瞧了这些信息,可以利用数理统计和机器学习可以分析出你的使用习惯,...
  • Input Director多台电脑共用一套键盘鼠标局域网 https://jingyan.baidu.com/article/3c48dd349c0414e10be358af.html
  • 两台电脑共享一套键盘鼠标的神器

    万次阅读 2018-04-09 14:54:01
    实验室有台台式电脑,自己又带了笔记本,现在键盘鼠标是连接在台式机上的,当要使用笔记本的时候再去用笔记本的触摸板是很不习惯,就想找找有没有能什么东西能实现一套键盘鼠标给两台电脑用的,从某宝上搜到的是使用...
  • 桌面上有两台电脑, 一台mac一台windows, 由于桌面空间紧张, 放两套键盘鼠标有点浪费空间, 如果能让mac和windows共享键盘鼠标就好了, 经过一番搜寻, 找到了一款名为synergy的软件, 这款软件覆盖全平台, mac, windows,...
  • pywin32激活指定窗口,模拟键盘鼠标

    千次阅读 2019-12-23 23:41:11
    import win32gui hwnd=win32gui.FindWindow("ChatWnd", "标题") #获取窗口 win32gui.SetForegroundWindow(hwnd) #...#模拟键盘鼠标 #-*-coding:utf-8-*- import os import time import win32gui import win32api i...
  • 【OpenGL】-009 GLUT中处理键盘鼠标事件   键盘和鼠标是GUI程序获取用户输入的重要方式,在windows程序中,通过WM_KEY_DOWN/WM_KEY_UP/WM_LBUTTONDOWN/WM_RBUTTONDOWN等消息的响应,用户可以方便的处理键盘鼠标的...
  • python 模拟键盘鼠标输入

    千次阅读 2013-09-25 19:33:52
    最近需要模拟键盘鼠标的输入,一番查找之后,得到的一些结果 ************************************************************************************************************************ pyUserInput,...
  • 实现DX游戏的键盘鼠标操作

    千次阅读 2014-06-16 22:14:46
    *碰到DX游戏SendMessage 和PostMessage是没办法使用模拟键盘鼠标操作的,这时候我们可以用WinIO的SendInput把键盘鼠标消息放到消息队列里面去实现 代码如下: 模拟鼠标   int SuccessAction1, SuccessAction2, ...
  • hook键盘鼠标 在XP下能用 WIN7不能用
  • JavaScript键盘鼠标监听功能

    千次阅读 2019-06-11 10:31:20
    实际应用中,我们会遇到监听按键输入和鼠标点击事件,在这里我们进行对鼠标键盘事件的总结. #### Keyboard​Event KeyboardEvent 对象描述了键盘的交互方式。 每个事件都描述了一个按键(Each event describes a key...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 46,188
精华内容 18,475
关键字:

键盘鼠标