C# WINCE5 钩子

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

namespace Key
{
    public class Hook
    {
        public delegate int HookKeyProc(int code, IntPtr wParam, IntPtr lParam);
        private HookKeyProc hookKeyDeleg;

        private static int hHookKey = 0;
        public Hook()
        { }
        #region public methods
        //安装钩子
        public void Start()
        {
            if (hHookKey != 0)
            {
                //Unhook the previouse one
                this.Stop();
            }
            hookKeyDeleg = new HookKeyProc(HookKeyProcedure);
            hHookKey = SetWindowsHookEx(WH_KEYBOARD_LL, hookKeyDeleg, GetModuleHandle(null), 0);
            if (hHookKey == 0)
            {
                throw new SystemException("Failed acquiring of the hook.");
            }
        }
        //拆除钩子
        public void Stop()
        {
            UnhookWindowsHookEx(hHookKey);
        }

        #endregion

        #region protected and private methods
        private int HookKeyProcedure(int code, IntPtr wParam, IntPtr lParam)
        {

            KBDLLHOOKSTRUCT hookStruct = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT));
            if (code < 0)
            {
                return CallNextHookEx(hookKeyDeleg, code, wParam, lParam);
            }
            if (hookStruct.vkCode == 0x72)//拨打电话
            {
                   //自己的处理
            //处理完之后一定要返回-1
                return -1
            }

            // 没处理的键的消息往下传递
            return CallNextHookEx(hookKeyDeleg, code, wParam, lParam);

        }

        #endregion

        #region P/Invoke declarations

        [DllImport("coredll.dll")]
        private static extern int SetWindowsHookEx(int type, HookKeyProc HookKeyProc, IntPtr hInstance, int m);

        [DllImport("coredll.dll")]
        private static extern IntPtr GetModuleHandle(string mod);

        [DllImport("coredll.dll")]
        private static extern int CallNextHookEx(HookKeyProc hhk, int nCode, IntPtr wParam, IntPtr lParam);

        [DllImport("coredll.dll")]
        private static extern int GetCurrentThreadId();

        [DllImport("coredll.dll", SetLastError = true)]
        private static extern int UnhookWindowsHookEx(int idHook);

        private struct KBDLLHOOKSTRUCT
        {
            public int vkCode;
            public int scanCode;
            public int flags;
            public int time;
            public IntPtr dwExtraInfo;
        }

        const int WH_KEYBOARD_LL = 20;

        public class KeyBoardInfo
        {
            public int vkCode;
            public int scanCode;
            public int flags;
            public int time;

        }

        #endregion

    }
}


应用时,可以将这个类添加到自己的项目中,在要用的地方直接调用,方法如下:

Hook hk=new Hook();
hk.Start();
hk.Stop();
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值