• 大小: 17KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-01
  • 语言: C#
  • 标签:

资源简介

钩子监视扫描枪,钩子监视扫描枪,钩子监视扫描枪,钩子监视扫描枪,钩子监视扫描枪,钩子监视扫描枪,钩子监视扫描枪,钩子监视扫描枪,钩子监视扫描枪,

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Text;

using System.Runtime.InteropServices;
using System.Reflection;
namespace SystemHook
{

    public class HookBarCode
    {
        public delegate void HookProc(int nCode IntPtr wParam IntPtr lParam);
        public delegate void GetBarCodeDelegate(string _BarCode);
        private event GetBarCodeDelegate GetBarCodeEvent;
        private HookProc KeyboardHookProcedure;
        private IntPtr  m_lHook=IntPtr.Zero ;
        private  StringBuilder ScanCode;   //扫描码 
        private  int TickCount;            //扫描时间

        #region 开启和关闭钩子
        /// 
        /// 打开系统钩子,开始监视扫描枪
        /// 

        public void StartSystemHook()
        {
            if (IntPtr.Zero == m_lHook)
            {
                KeyboardHookProcedure += CallHookProc; 
                m_lHook = SetWindowsHookEx(HookType.WH_KEYBOARD_LL KeyboardHookProcedure
                    Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]) 0);
            }
        }

        /// 
        /// 打开线程钩子,开始监视扫描枪
        /// 

        public void StartThreadHook(int threadID)
        {
            if (IntPtr.Zero == m_lHook)
            {
                KeyboardHookProcedure += CallHookProc;
                m_lHook = SetWindowsHookEx(HookType.WH_KEYBOARD_LL KeyboardHookProcedure
                    IntPtr.Zero threadID);
                
            }
        }

        /// 
        /// 关闭监视
        /// 

        public void StopHook()
        {
            if (IntPtr.Zero != m_lHook)
                UnhookWindowsHookEx(m_lHook);
        }
        #endregion

        /// 
        /// 添加扫描枪录入处理事件
        /// 

        /// 
        public void Add(GetBarCodeDelegate GetBarCodeMethod)
        {
            GetBarCodeEvent += new GetBarCodeDelegate(GetBarCodeMethod);
        }

        //回调函数
        private void CallHookProc(int nCode IntPtr wParam IntPtr lParam)
        {
            /* 
                KeyDown = 0x0100
            KeyUp = 0x0101
            SystemKeyDown = 0x0104
            SystemKeyUp = 0x0105
            */
            if (GetBarCodeEvent != null && nCode >= 0 && (int)wParam == 0x100)
            {
                HookEventMSG m = (HookEventMSG)Marshal.PtrToStructure(lParam typeof(HookEventMSG));
                
                if (m.vkCode >= 48 && m.vkCode <= 57)
                {
                    //如果扫描的是数字
                    AddText ( (m.vkCode - 48).ToString());
                }
                else if(m.vkCode>=65 && m.vkCode <=90)
                { 
                    //扫描为字母
                    //AddText(ConvertKeyCode((VirtualKeys)m.vkCode).ToString());
                    AddText(((char)m.vkCode).ToString ());
                }
                else i

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       1409  2007-12-12 18:27  钩子测试\钩子测试.sln

    ..A..H.     17920  2007-12-12 18:41  钩子测试\钩子测试.suo

     文件       1958  2007-12-12 18:27  钩子测试\SystemHook\SystemHook.csproj

     文件        168  2007-12-12 18:27  钩子测试\SystemHook\SystemHook.csproj.user

     文件       6073  2007-12-12 18:34  钩子测试\SystemHook\BarCode.cs

     文件       1363  2007-12-12 18:22  钩子测试\SystemHook\Properties\AssemblyInfo.cs

     文件        981  2007-12-12 18:40  钩子测试\钩子测试\Form1.cs

     文件       3257  2007-12-12 18:39  钩子测试\钩子测试\Form1.Designer.cs

     文件       3449  2007-12-12 18:27  钩子测试\钩子测试\钩子测试.csproj

     文件       5814  2007-12-12 18:39  钩子测试\钩子测试\Form1.resx

     文件        471  2007-12-12 18:22  钩子测试\钩子测试\Program.cs

     文件       5612  2007-12-12 18:22  钩子测试\钩子测试\Properties\Resources.resx

     文件        249  2007-12-12 18:22  钩子测试\钩子测试\Properties\Settings.settings

     文件       1220  2007-12-12 18:22  钩子测试\钩子测试\Properties\AssemblyInfo.cs

     文件       2879  2007-12-12 18:22  钩子测试\钩子测试\Properties\Resources.Designer.cs

     文件       1096  2007-12-12 18:22  钩子测试\钩子测试\Properties\Settings.Designer.cs

     目录          0  2007-12-12 18:42  钩子测试\SystemHook\Properties

     目录          0  2007-12-12 18:42  钩子测试\钩子测试\Properties

     目录          0  2007-12-12 18:42  钩子测试\SystemHook

     目录          0  2007-12-12 18:42  钩子测试\钩子测试

     目录          0  2007-12-12 18:42  钩子测试

----------- ---------  ---------- -----  ----

                53919                    21


评论

共有 条评论

相关资源