日期:2014-05-18  浏览次数:20735 次

最近研究一个键盘钩子 有些问题想咨询下!
最近研究一个键盘钩子,用于设计一个快捷按键后触发一些事件

要在按完快捷键之后执行一个显示倒计时的程序,显示在一个文本框或者label里

请问我应该写在哪里,大致代码如下:
C# code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Diagnostics;

namespace pigTime
{
    public partial class Form1 : Form
    {
        //委托   
        public delegate int HookProc(int nCode, int wParam, IntPtr lParam);
        static int hHook = 0;
        public const int WH_KEYBOARD_LL = 13;
        //LowLevel键盘截获,如果是WH_KEYBOARD=2,并不能对系统键盘截取,Acrobat Reader会在你截取之前获得键盘。   
        HookProc KeyBoardHookProcedure;
        //快捷按键1设定存储
        public static string qkey1 = "";
        //快捷按键2设定存储
        public static string qkey2 = "";
        //猪圈国家点
        public static int currCity = 0;
        //刷新点名称
        public static string currPoint = "";
        //设定时间间隔
        public static int tmjg = 0;
        //设定误差时间
        public static int wctm = 0;
        private static int hour, minute, second, millsecond;
        //键盘Hook结构函数   
        [StructLayout(LayoutKind.Sequential)]
        public class KeyBoardHookStruct
        {
            public int vkCode;
            public int scanCode;
            public int flags;
            public int time;
            public int dwExtraInfo;
        }
        #region DllImport
        //设置钩子   
        [DllImport("user32.dll")]
        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 bool UnhookWindowsHookEx(int idHook);
        [DllImport("user32.dll")]
        //调用下一个钩子   
        public static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam);

        [DllImport("kernel32.dll")]
        public static extern int GetCurrentThreadId();

        [DllImport("kernel32.dll")]
        public static extern IntPtr GetModuleHandle(string name);

        #endregion
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“dbDataSet.TimeData”中。您可以根据需要移动或移除它。
            this.timeDataTableAdapter.Fill(this.dbDataSet.TimeData);
            // TODO: 这行代码将数据加载到表“dbDataSet.itemData”中。您可以根据需要移动或移除它。
            this.itemDataTableAdapter.Fill(this.dbDataSet.itemData);
            this.smallItemDataTableAdapter.Fill(this.dbDataSet.SmallItemData);
            comboBox1.SelectedIndex = 6;

            int sitemid = int.Parse(comboBox1.SelectedValue.ToString());
            currCity = sitemid;
            DataRowCollection drc = this.smallItemDataTableAdapter.GetDataBy(sitemid).Rows;
            label4.Text = drc[0][1].ToString();
            label5.Text = drc[1][1].ToString();
            label6.Text = drc[0][1].ToString();
            label7.Text = drc[1][1].ToString();
            textBox3.Text = drc[0]["Qkey"].ToString();
            textBox4.Text = drc[1]["Qkey"].ToString();
            qkey1 = drc[0]["Qkey"].ToString();
            qkey2 = drc[1]["Qkey"].ToString();

            tmjg = int.Parse(textBox1.Text.Trim().ToString());
            wctm = int.Parse(textBox2.Text.Trim().ToString());

            Hook_Start();
            
        }

        

       
        #region 自定义事件
        public void Hook_Start()
        {
            // 安装键盘钩子   
            if (hHook == 0)
            {
                KeyBoardHookProcedure = new HookProc(KeyBoardHookProc);