教程集 www.jiaochengji.com
教程集 >  脚本编程  >  Asp.net  >  正文 c# 键盘钩子的实现代码

c# 键盘钩子的实现代码

发布时间:2015-12-07   编辑:jiaochengji.com
c# 键盘钩子的实现代码,纯代码,有注释,试用了一下,不是很稳定,改回自带的 KeyUp函数了,分享下,有需要的朋友参考下。

c# 键盘钩子的实现代码,纯代码,有注释,试用了一下,不是很稳定,改回自带的 KeyUp函数了,分享下,有需要的朋友参考下。

为大家推荐几篇有关c# 钩子的文章:
c#钩子本线程内消息拦截的方法
c# 系统钩子的实现代码
c# 全局鼠标钩子的实现代码
学习 c# 钩子的小例子 

复制代码 代码示例:

//自定义类型
public enum HookType
    {
      WH_KEYBOARD = 2
    }

    public delegate int HOOKPROC(int nCode, int wParam, int lParam);

//接口调用
[DllImport( "User32.DLL ")]
public static extern int SendMessage(IntPtr hWnd, uint Msg,int wParam,int lParam);
[DllImport("kernel32")]
public static extern int GetCurrentThreadId();
[DllImport("User32.Dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport( "user32",CharSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall)]
public static extern int  SetWindowsHookEx(HookType idHook,HOOKPROC lpfn,int hmod,int dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto,CallingConvention = CallingConvention.StdCall)]
public static extern int CallNextHookEx(int idHook, int nCode,int wParam,int lParam);

//函数实现
public void SetHook()
    {   
      SetWindowsHookEx(HookType.WH_KEYBOARD,
        new HOOKPROC(this.MyKeyboardProc),
        0,
        GetCurrentThreadId());
    }

public int MyKeyboardProc(int nCode, int wParam, int lParam)
    {
      IntPtr ParenthWnd = new IntPtr(0);
      bool isPressed = (((lParam & 0x80000000) == 0) && (nCode==0));
      CallNextHookEx(m_HookHandle, nCode, wParam, lParam);  //先把消息发给系统默认处理队列
      global.lastKeyValue  = wParam;
      ParenthWnd = FindWindow( null,"零售业务");

      switch(wParam)
        {
          case  112: //修改数量 F1
            if(isPressed)
            {
              inputType = global.ST_NUM_STP1;
              doKeyWork();
            }
            break;
          case  113: //备注      F2
            if(isPressed)
            {
              inputType = global.ST_REMARK;
              textBoxBZ.Focus();
              textBoxBZ.SelectAll();
            }
            break;
          case  114: //摘要      F3
            if(isPressed)
            {
              inputType = global.ST_ABST;
              textBoxZY.Focus();
              textBoxZY.SelectAll();
            }
            break;
          case  115: //支付     F4
            if(isPressed)
              if((global.saleList == null) || (global.saleList.Length == 0))
                break;
              else 
              {
                showProductInfo();
                labelCPZS.Text  = global.sAllNum.ToString() + " 件";
                labelYSJE.Text  = global.sAllValue.ToString() + " 元";
                labelYHJE.Text  = global.sPriValue.ToString() + " 元";
                labelSSJE.Text  = global.sDueValue.ToString() + " 元";
                inputType = global.ST_GATHER_STP1;
                textBoxSK.Focus();
                textBoxSK.SelectAll();
              }
            break;
          case  116: //输入产品     F5
            if(isPressed)
            {
              inputType = global.ST_PRODUCT;
              textBoxSPMC.Focus();
              textBoxSPMC.SelectAll();
            }
            break;
          case  117:  //开始新订单  F6
            if(isPressed)
            {
              resetSale(); //销售界面复位
            }
            break;
          default:
            //CallNextHookEx(m_HookHandle, nCode, wParam, lParam);
            break;
        }

      return(0);
    }

    /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if(components != null)
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }

您可能感兴趣的文章:
c# 系统钩子的实现代码
C# Hook钩子实例-截取键盘输入
学习 c# 钩子的小例子
驳:《利用键盘钩子捕获Windows键盘动作》
c# 键盘钩子的实现代码
PHP中钩子的理解与实例教程
c#钩子本线程内消息拦截的方法
c# 全局鼠标钩子的实现代码
php中实现插件机制的方法介绍
Flask钩子函数是什么

[关闭]
~ ~