很多小盆友表示买不起机械键盘,甚至买不起机械手感键盘,一直在用笔记本自带的键盘。那么如何改造成机械键盘?废话不多说,上代码。
1.首先写一个全局监听键盘的类,主要是利用勾子实现。
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace KeyboardMusic
{
class KeyBoardHook
{
//需要绑定的事件
public event KeyEventHandler KeyUpEvent;
public event KeyEventHandler KeyDownEvent;
public event KeyPressEventHandler KeyPressEvent;
public delegate int HookProc(int nCode, int wParam, IntPtr lParam);
static int hKeyboardHook = 0; //声明键盘钩子处理的初始值
public const int WH_KEYBOARD_LL = 13;//全局键盘监听鼠标消息为13
HookProc KeyboardHookProcedure;
[StructLayout(LayoutKind.Sequential)]
public class KeyboardHookStruct
{
public int vkCode;
public int scanCode;
public int flags;
public int time;
public int dwExtraInfo;
}
//安装钩子
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
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", CharSet = CharSet.Auto, CallingConvention = Calli