此方法锁定不了组合键。锁定组合键的方法自己没搞定。
全部代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace detectUSB
{
public partial class Form1 : Form
{
[DllImport("User32.dll")] //需要using System.Runtime.InteropServices
static extern void BlockInput(bool block);//阻塞函数
public Form1()
{
InitializeComponent();
}
protected override void WndProc(ref Message m)
{
const int WM_DEVICECHANGE = 0x219;// 发生设备变动
const int WM_DEVICEARRVIAL = 0x8000;//如果m.Msg的值为0x8000那么表示有U盘插入
const int WM_DEVICEMOVECOMPLETE = 0x8004;//系统完成移除一个设备
//const int DBT_DEVTYP_VOLUME = 0x00000002;// 逻辑卷标
switch (m.Msg)
{
case WM_DEVICECHANGE:
switch (m.WParam.ToInt32())
{
case WM_DEVICEARRVIAL:
BlockInput(true); //锁定键盘和鼠标,但无法锁定ctrl+alt+del
break;
case WM_DEVICEMOVECOMPLETE:
BlockInput(false);//键盘和鼠标解锁
break;
}
break;
}
base.WndProc(ref m);
}
}
}