登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

忆风的博客

http://www.cnblogs.com/dhjdhja

 
 
 

日志

 
 
 
 

锁定鼠标和键盘  

2009-05-06 09:28:17|  分类: .net |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace HookMouseAndKeyboard
{
    class HookMouseAndKeyboard
    {
        #region 锁定鼠标和键盘
        //有关鼠标和键盘钩子的相关定义
        static int hhookMouse = 0;
        static int hhookKeyBoard = 0;
        public const int WH_MOUSE = 14;
        public const int WH_KEYBOARD = 2;
        public const int WH_MOUSE_LL = 14;
        private const int WH_KEYBOARD_LL = 13;
        public const int HC_ACTION = 0;
        //public const int WM_KEYDOWN = 0x100;
        //public const int VK_RETURN = 0xD;
        public delegate int HookProc(int nCode, IntPtr wParam, IntPtr iParam);
        HookProc MouseHookProcedure, KeyBoardHookProcedure;
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hinstance, int threadid);
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, int lParam);
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern bool UnhookWindowsHookEx(int idHook);
        /// <summary>
        /// 钩子处理方法(鼠标锁定)
        /// </summary>
        /// <param name="nCode"> </param>
        /// <param name="wParam"> </param>
        /// <param name="lParam"> </param>
        /// <returns> </returns>
        private int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            //如果nCode <0,直接调用CallNextHookEx返回
            if (nCode < 0)
                //return CallNextHookEx(hhookMouse,nCode,wParam,lParam);
                return 1;
            //函数将消息向下传递,下一个钩子处理将截获这一消息
            //if(nCode!= HC_ACTION)
            //    return CallNextHookEx(hhookMouse, nCode, wParam, lParam);
            return 1;
        }
        /// <summary>
        /// 启动鼠标锁定
        /// </summary>
        /// <returns> </returns>
        public bool EnableMouseCapture()
        {
            MouseHookProcedure = new HookProc(this.MouseHookProc);
            Process currentProcess = Process.GetCurrentProcess();

            //安装全局鼠标钩子
            hhookMouse = SetWindowsHookEx(WH_MOUSE_LL, MouseHookProcedure, currentProcess.MainModule.BaseAddress, 0);
            if (hhookMouse == 0)
                return false;
            return true;
        }
        /// <summary>
        /// 解除鼠标锁定
        /// </summary>
        /// <returns> </returns>
        public bool DisableMouseCapture()
        {
            return UnhookWindowsHookEx(hhookMouse);
        }

        /// <summary>
        /// 钩子处理方法(键盘锁定)
        /// </summary>
        /// <param name="nCode"> </param>
        /// <param name="wParam"> </param>
        /// <param name="lParam"> </param>
        /// <returns> </returns>
        private int KeyBoardHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            //如果nCode <0,直接调用CallNextHookEx返回
            if (nCode < 0)
                //return CallNextHookEx(hhookKeyBoard, nCode, wParam, lParam);
                return 1;
            //函数将消息向下传递,下一个钩子处理将截获这一消息
            //if (nCode != HC_ACTION)
            //return CallNextHookEx(hhookKeyBoard, nCode, wParam, lParam);
            return 1;
        }
        /// <summary>
        /// 启动键盘锁定
        /// </summary>
        /// <returns> </returns>
        public bool EnableKeyBoardCapture()
        {
            KeyBoardHookProcedure = new HookProc(this.KeyBoardHookProc);
            Process currentProcess = Process.GetCurrentProcess();

            //安装全局键盘钩子
            hhookKeyBoard = SetWindowsHookEx(WH_KEYBOARD_LL, KeyBoardHookProcedure, currentProcess.MainModule.BaseAddress, 0);
            if (hhookKeyBoard == 0)
                return false;
            return true;
        }
        /// <summary>
        /// 解除键盘锁定
        /// </summary>
        /// <returns> </returns>
        public bool DisableKeyBoardCapture()
        {
            return UnhookWindowsHookEx(hhookKeyBoard);
        }

        #endregion
    }
}

  评论这张
 
阅读(279)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018