用C#做键盘敲击次数记录求代码
要在游戏里面也可以记录!!!!!!!
protected override void OnKeyPress(KeyPressEventArgs e)
{
keybordint++;
keybordinttemp++;
base.OnKeyPress(e);
}
这个代码不能用.只能在这个程序内有记录.其他程序里不能记录!!!!
求代码!!!!!!!!!!!!!!!
答案:
刚好最近学习钩子,以下是整理源码。
static int hKeyboardHook = 0; //键盘钩子句柄
public const int WH_KEYBOARD_LL = 13; //处理键盘消息
private const int WM_KEYDOWN = 0x100;//键盘按下
//声明键盘钩子事件类型.
HookProc KeyboardHookProcedure;
//声明键盘钩子的封送结构类型
[StructLayout(LayoutKind.Sequential)]
public class KeyboardHookStruct
{
public int vkCode; //表示一个在1到254间的虚似键盘码
}
//装置钩子的函数
[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 = CallingConvention.StdCall)]
public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);
[DllImport("user32")]
public static extern int ToAscii(int uVirtKey, int uScanCode, byte[] lpbKeyState, byte[] lpwTransKey, int fuState);
[DllImport("user32")]
public static extern int GetKeyboardState(byte[] pbKeyState);
[DllImport("kernel32.dll", CharSet = CharSet.Auto,CallingConvention = CallingConvention.StdCall)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);
//先前按下的键
public List<Keys> preKeys = new List<Keys>();
int b;
//析构函数.
~Form1()
{
Stop();
}
public void Start()
{
//安装键盘钩子
if (hKeyboardHook == 0)
{
KeyboardHookProcedure = new HookProc(KeyboardHookProc);
Process curProcess = Process.GetCurrentProcess();//获取新的 Process 组件并将其与当前活动的进程关联
ProcessModule curModule = curProcess.MainModule;
hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProcedure, GetModuleHandle(curModule.ModuleName), 0);
if (hKeyboardHook == 0)
{
Stop();
throw new Exception("SetWindowsHookEx ist failed.");
}
b = 0;
}
}
public void Stop()
{
bool retKeyboard = true;
if (hKeyboardHook != 0)
{
retKeyboard = UnhookWindowsHookEx(hKeyboardHook);
hKeyboardHook = 0;
}
//如果卸下钩子失败
if (!(retKeyboard)) throw new Exception("UnhookWindowsHookEx failed.");
}
int num = 0;//记录次数
private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
{
if (wParam == WM_KEYDOWN)
{
KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
Keys keyData = (Keys)MyKeyboardHookStruct.vkCode;
preKeys.Add(keyData);
num++;
label1.Text = Convert.ToString(num);
b = 1;
}
return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
}
private void Form1_Load(object sender, EventArgs e)
{
Start();
}
对于OnKeyPress的重载是针对你的Form来的,也就是说发往你的Form的KeyPress事件会被截获,而发往其他程序的消息是得不到的
按照你说的,实际上是做的键盘钩子(截获密码用的),这样说就差不多能理解可行性了
另外,就算是键盘钩子,好像也仅能针对某一个进程,貌似不能针对整个系统
个人观点 坐等高人关于你提到这个问题,我以前也遇到过,后来北风网老师帮我解决了,
建议你去看一下北风网他们的.net课程,他们老师讲的课有解决这方面问题的参考链接: http://www.ibeifeng.com/goods.php?id=129我只能给你个提示-----全局钩子 要想在任何窗体下点击都有记录 需要全局钩子
必须启动这个程序才能统计,可以将其做成服务来做。运行到任务栏图标中。
首先··你要记录其它程序的按键记录·就得用
上一个:icomparer怎么让他 正排序 逆排序 C#
下一个:我写的C#的一个类的问题