• 大小: 87KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-27
  • 语言: C#
  • 标签: c#  keyboard  hoo  doking  IC  

资源简介

工厂用来做键盘测试的,我做好了,工程部用了别一个方案,所以就分享给大家学习吧,用到了C#键盘钩子,完整的项目案例哦

资源截图

代码片段和文件信息

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;
using Microsoft.Win32;
namespace Vjsdn.Tech.KeyboardHook
{
   public partial class frmKeyboardHook : Form
   {
      //勾子管理类 
      private System.Drawing.Color red_color = Color.Red;
      private System.Drawing.Color blue_color = Color.Blue;
      private System.Drawing.Color default_color = Color.Teal;
      private System.Drawing.Color color = Color.Blue;


      //20181022松下公司要求对特殊键测试五次以后才通过这里对空格键强要求
      private int space_canpass_count=10;//空格键要测试5次才能通过填入双倍数字
      private string key_name;//按下还是弹起记录
      private KeyboardHookLib _keyboardHook = null;
      private List keyarr;//装载所有按键用于清空用
      private List keyarr_tmp;//装载所有按键用于判断还有多少按键要检测
      private int task_count;
      private int current_count;
      private bool test_over = false;
      public frmKeyboardHook()
      {
         InitializeComponent();
      }

      private void button1_Click(object sender EventArgs e)
      {
          if (button1.Text == “开始测试“)
          {
              //安装勾子 
              _keyboardHook = new KeyboardHookLib();
              _keyboardHook.InstallHook(this.onkeydown);
              label1.Text = “系统也就绪,请插入键盘开始测试“;
              groupBox1.Focus();
              button1.Text = “结束测试“;
          }
          else {
              if (_keyboardHook != null) _keyboardHook.UninstallHook();
              button1.Text = “开始测试“;
              label1.Text = “系统也退出,请重新点击开始测试“;
          }

      }

      private void button2_Click(object sender EventArgs e)
      {
          reset_keyboard();
          
      }


       //重置键盘,清空颜色
      private void reset_keyboard()
      {
          label6.Text = ““;
          space_canpass_count = 10;//重置空格键
          test_over = false;
          current_count = task_count;
          keyarr_tmp.Clear();
          keyarr.ForEach(i => keyarr_tmp.Add(i));//重新复制待检测数组
          groupBox1.Focus();
          for (int i = keyarr_tmp.Count-1; i >= 0; i--) {
              string str = keyarr_tmp[i];
              Button btn = (Button)this.Controls.Find(str true)[0];
              btn.BackColor = default_color;
              
          }
         
        
        
      }


      private void render_key(string name) { //按键接收一个vkcode,此函数将拼接一个控件名,然后定位去修改
          try
          {
              string str = name;
              Button btn = (Button)this.Controls.Find(str true)[0];
              btn.BackColor = color;
          }
          catch {
          
          }
    
      }






      ///  
      /// 客户端键盘捕捉事件. 
      /// 
 
      /// 由Hook程序发送的按键信息 
      /// 是否拦截 
   

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

     文件        959  2018-10-20 21:26  Vjsdn.Tech.KeyboardHook.sln

     文件        187  2018-10-20 21:26  Vjsdn.Tech.KeyboardHook\App.config

     文件      37888  2018-10-21 21:00  Vjsdn.Tech.KeyboardHook\bin\Debug\Vjsdn.Tech.KeyboardHook.exe

     文件        187  2018-10-20 21:26  Vjsdn.Tech.KeyboardHook\bin\Debug\Vjsdn.Tech.KeyboardHook.exe.config

     文件      48640  2018-10-21 21:00  Vjsdn.Tech.KeyboardHook\bin\Debug\Vjsdn.Tech.KeyboardHook.pdb

     文件      22984  2018-10-21 18:33  Vjsdn.Tech.KeyboardHook\bin\Debug\Vjsdn.Tech.KeyboardHook.vshost.exe

     文件        187  2018-10-20 21:26  Vjsdn.Tech.KeyboardHook\bin\Debug\Vjsdn.Tech.KeyboardHook.vshost.exe.config

     文件        490  2018-04-12 07:35  Vjsdn.Tech.KeyboardHook\bin\Debug\Vjsdn.Tech.KeyboardHook.vshost.exe.manifest

     文件       9532  2018-10-21 21:00  Vjsdn.Tech.KeyboardHook\frmKeyboardHook.cs

     文件      68407  2018-10-21 17:31  Vjsdn.Tech.KeyboardHook\frmKeyboardHook.Designer.cs

     文件      11059  2018-10-21 17:31  Vjsdn.Tech.KeyboardHook\frmKeyboardHook.resx

     文件       4224  2018-10-20 21:28  Vjsdn.Tech.KeyboardHook\KeyboardHookLib.cs

     文件       1308  2018-10-20 22:58  Vjsdn.Tech.KeyboardHook\obj\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       7241  2018-10-20 21:27  Vjsdn.Tech.KeyboardHook\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件          0  2018-10-20 21:26  Vjsdn.Tech.KeyboardHook\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs

     文件          0  2018-10-20 21:26  Vjsdn.Tech.KeyboardHook\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs

     文件          0  2018-10-20 21:26  Vjsdn.Tech.KeyboardHook\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs

     文件       1447  2018-10-21 18:33  Vjsdn.Tech.KeyboardHook\obj\Debug\Vjsdn.Tech.KeyboardHook.csproj.FileListAbsolute.txt

     文件        985  2018-10-21 17:31  Vjsdn.Tech.KeyboardHook\obj\Debug\Vjsdn.Tech.KeyboardHook.csproj.GenerateResource.Cache

     文件       2209  2018-10-20 22:37  Vjsdn.Tech.KeyboardHook\obj\Debug\Vjsdn.Tech.KeyboardHook.csprojResolveAssemblyReference.cache

     文件      37888  2018-10-21 21:00  Vjsdn.Tech.KeyboardHook\obj\Debug\Vjsdn.Tech.KeyboardHook.exe

     文件       3778  2018-10-21 17:31  Vjsdn.Tech.KeyboardHook\obj\Debug\Vjsdn.Tech.KeyboardHook.frmKeyboardHook.resources

     文件      48640  2018-10-21 21:00  Vjsdn.Tech.KeyboardHook\obj\Debug\Vjsdn.Tech.KeyboardHook.pdb

     文件        180  2018-10-20 21:28  Vjsdn.Tech.KeyboardHook\obj\Debug\Vjsdn.Tech.KeyboardHook.Properties.Resources.resources

     文件        545  2018-10-20 21:26  Vjsdn.Tech.KeyboardHook\Program.cs

     文件       1366  2018-10-20 21:26  Vjsdn.Tech.KeyboardHook\Properties\AssemblyInfo.cs

     文件       2898  2018-10-20 21:26  Vjsdn.Tech.KeyboardHook\Properties\Resources.Designer.cs

     文件       5612  2018-10-20 21:26  Vjsdn.Tech.KeyboardHook\Properties\Resources.resx

     文件       1110  2018-10-20 21:26  Vjsdn.Tech.KeyboardHook\Properties\Settings.Designer.cs

     文件        249  2018-10-20 21:26  Vjsdn.Tech.KeyboardHook\Properties\Settings.settings

............此处省略12个文件信息

评论

共有 条评论