键盘钩子lParam问题 if (lParam & 0x80000000)判断让键盘只处理一次事件,网上资料好像可行 ,为什么我提示错误 运算符“&”无法应用于“System.IntPtr”和“uint”类型的操作数。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 if ((lParam.ToInt64() & 0x80000000) == 0x80000000) //钩子事件内部调用,调用_clientMethod方法转发到客户端应用。 private static int OnHookProc(int nCode, int wParam, IntPtr lParam) { if ((lParam.ToInt64 ()& 0x80000000) == 0x80000000) { { if (nCode >= 0) { //转换结构 HookStruct hookStruct = (HookStruct)Marshal.PtrToStructure(lParam, typeof(HookStruct)); if (_clientMethod != null) { bool handle = false; //调用客户提供的事件处理程序。 _clientMethod(hookStruct, out handle); if (handle) return 1; //1:表示拦截键盘,return 退出 } } } return CallNextHookEx(_hHookValue, nCode, wParam, lParam); }提示错误 “Vjsdn.Tech.KeyboardHook.KeyboardHookLib.OnHookProc(int, int, System.IntPtr)”: 并非所有的代码路径都返回值 private static int OnHookProc(int nCode, int wParam, IntPtr lParam){ if ((lParam.ToInt64() & 0x80000000) == 0x80000000) { if (nCode >= 0) { //转换结构 HookStruct hookStruct = (HookStruct)Marshal.PtrToStructure(lParam, typeof(HookStruct)); if (_clientMethod != null) { bool handle = false; //调用客户提供的事件处理程序。 _clientMethod(hookStruct, out handle); if (handle) return 1; //1:表示拦截键盘,return 退出 } } } return CallNextHookEx(_hHookValue, nCode, wParam, lParam);} if ((lParam.ToInt64() & 0x80000000) == 0x80000000) 这句我这里0x80000000的颜色都是黑的,应该是这有问题,0x80000000应该是32位,是不是缺少引用,还是不太理解 键盘钩子类using System;using System.Collections.Generic;using System.Text;using System.Runtime.InteropServices;using System.Reflection;using System.Diagnostics;using Microsoft.Win32;using System.Windows.Forms;namespace Vjsdn.Tech.KeyboardHook{ /// <summary> /// 键盘Hook管理类, by www.vjsdn.com 易学原创 /// </summary> public class KeyboardHookLib { private const int WH_KEYBOARD_LL = 13; //键盘 //键盘处理事件委托 ,当捕获键盘输入时调用定义该委托的方法. private delegate int HookHandle(int nCode, int wParam, IntPtr lParam); //客户端键盘处理事件 public delegate void ProcessKeyHandle(HookStruct param, out bool handle); //接收SetWindowsHookEx返回值 private static int _hHookValue = 0; //勾子程序处理事件 private HookHandle _KeyBoardHookProcedure; //Hook结构 [StructLayout(LayoutKind.Sequential)] public class HookStruct { public int vkCode; public int scanCode; public int flags; public int time; public int dwExtraInfo; } //设置钩子 [DllImport("user32.dll")] private static extern int SetWindowsHookEx(int idHook, HookHandle lpfn, IntPtr hInstance, int threadId); //取消钩子 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] private static extern bool UnhookWindowsHookEx(int idHook); //调用下一个钩子 [DllImport("user32.dll")] private static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam); //获取当前线程ID [DllImport("kernel32.dll")] private static extern int GetCurrentThreadId(); //Gets the main module for the associated process. [DllImport("kernel32.dll")] private static extern IntPtr GetModuleHandle(string name); private IntPtr _hookWindowPtr = IntPtr.Zero; //构造器 public KeyboardHookLib() { } //外部调用的键盘处理事件 private static ProcessKeyHandle _clientMethod = null; /// <summary> /// 安装勾子 /// </summary> /// <param name="hookProcess">外部调用的键盘处理事件</param> public void InstallHook(ProcessKeyHandle clientMethod) { _clientMethod = clientMethod; // 安装键盘钩子 if (_hHookValue == 0) { _KeyBoardHookProcedure = new HookHandle(OnHookProc); _hookWindowPtr = GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName); _hHookValue = SetWindowsHookEx( WH_KEYBOARD_LL, _KeyBoardHookProcedure, _hookWindowPtr, 0); //如果设置钩子失败. if (_hHookValue == 0) UninstallHook(); } } //取消钩子事件 public void UninstallHook() { if (_hHookValue != 0) { bool ret = UnhookWindowsHookEx(_hHookValue); if (ret) _hHookValue = 0; } } //钩子事件内部调用,调用_clientMethod方法转发到客户端应用。 private static int OnHookProc(int nCode, int wParam, IntPtr lParam){ if ((lParam.ToInt32() & 0x80000000) == 0x80000000) { if (nCode >= 0) { //转换结构 HookStruct hookStruct = (HookStruct)Marshal.PtrToStructure(lParam, typeof(HookStruct)); if (_clientMethod != null) { bool handle = false; //调用客户提供的事件处理程序。 _clientMethod(hookStruct, out handle); if (handle) return 1; //1:表示拦截键盘,return 退出 } } } return CallNextHookEx(_hHookValue, nCode, wParam, lParam);} } }主界面using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using Vjsdn.Tech.KeyboardHook;using System.IO;namespace ProjectWarHelp{ public partial class Record : Form { private KeyboardHookLib _keyboardHook = null; public Record() { InitializeComponent(); } private void btnOpen_Click(object sender, EventArgs e) { lblState.Text = "数据记录中。。"; lblState.ForeColor = Color.Red; _keyboardHook = new KeyboardHookLib(); _keyboardHook.InstallHook(OnKeyPress); } private void btnClose_Click(object sender, EventArgs e) { lblState.Text = "数据记录中断"; lblState.ForeColor = Color.SaddleBrown; if (_keyboardHook != null) _keyboardHook.UninstallHook(); } public void OnKeyPress(KeyboardHookLib .HookStruct hookStruct,out bool handle) { handle = false; Keys key = (Keys)hookStruct.vkCode; FileStream fs = new FileStream(Application.StartupPath + @"\Record.txt", FileMode.Open); StreamWriter sw = new StreamWriter(fs); sw.BaseStream.Seek(0, SeekOrigin.End); sw.Write(key.ToString() + " "); sw.Close(); } private void Record_Load(object sender, EventArgs e) { lblState.Text = "数据记录中断"; lblState.ForeColor = Color.SaddleBrown; if (!File.Exists(Application.StartupPath + @"\Record.txt")) { FileStream fs = new FileStream(Application.StartupPath + @"\Record.txt", FileMode.CreateNew); fs.Close(); } FileStream fs2 = new FileStream(Application.StartupPath + @"\Record.txt", FileMode.Open); StreamWriter sw = new StreamWriter(fs2); sw.BaseStream.Seek(0, SeekOrigin.End); sw.WriteLine(""); sw.WriteLine("开始时间:"+System .DateTime .Now .ToString ()); sw.Close(); } private void Record_FormClosing(object sender, FormClosingEventArgs e) { FileStream fs = new FileStream(Application.StartupPath + @"\Record.txt", FileMode.Open); StreamWriter sw = new StreamWriter(fs); sw.BaseStream.Seek(0, SeekOrigin.End); sw.WriteLine(""); sw.WriteLine("结束时间:"+System.DateTime.Now.ToString()); sw.Close(); } }}问题还是解决不了private static int OnHookProc(int nCode, int wParam, IntPtr lParam)运行2次 加if ((lParam.ToInt32() & 0x80000000) == 0x80000000)这一直不能达到效果,lparam的32位一直是0,keyyon,keydown不是lparam的32位值会改变,问题到底出在哪,头都爆了。 新人毕设处女贴 关于DATASET的两个问题 急求!!! 关于三层结构的问题 c#动态得到控件名并更改其内容 C#保存 要从字符串"123,-1,苏州XX公司"依次提取整数123,整数-1,字符串"苏州XX公司",如何写代码 求助DEV控件的LookUpEdit的用法 点击项目文件VS说无法从web服务器获取项目文件 请问接口和纯虚的真实功能区别:) 如何取得treeView所选节点的值, textBox 的DataBindings CheckBoxList 提交时判断最少需要选择2个 C# C/S模式如何实现图片展示功能?
private static int OnHookProc(int nCode, int wParam, IntPtr lParam)
{
if ((lParam.ToInt64 ()& 0x80000000) == 0x80000000)
{ {
if (nCode >= 0)
{ //转换结构
HookStruct hookStruct = (HookStruct)Marshal.PtrToStructure(lParam, typeof(HookStruct)); if (_clientMethod != null)
{
bool handle = false;
//调用客户提供的事件处理程序。
_clientMethod(hookStruct, out handle);
if (handle)
return 1; //1:表示拦截键盘,return 退出
}
}
}
return CallNextHookEx(_hHookValue, nCode, wParam, lParam);
}提示错误 “Vjsdn.Tech.KeyboardHook.KeyboardHookLib.OnHookProc(int, int, System.IntPtr)”: 并非所有的代码路径都返回值
{
if ((lParam.ToInt64() & 0x80000000) == 0x80000000)
{
if (nCode >= 0)
{
//转换结构
HookStruct hookStruct = (HookStruct)Marshal.PtrToStructure(lParam, typeof(HookStruct));
if (_clientMethod != null)
{
bool handle = false;
//调用客户提供的事件处理程序。
_clientMethod(hookStruct, out handle);
if (handle)
return 1; //1:表示拦截键盘,return 退出
}
}
}
return CallNextHookEx(_hHookValue, nCode, wParam, lParam);
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Diagnostics;
using Microsoft.Win32;
using System.Windows.Forms;namespace Vjsdn.Tech.KeyboardHook
{
/// <summary>
/// 键盘Hook管理类, by www.vjsdn.com 易学原创
/// </summary>
public class KeyboardHookLib
{
private const int WH_KEYBOARD_LL = 13; //键盘 //键盘处理事件委托 ,当捕获键盘输入时调用定义该委托的方法.
private delegate int HookHandle(int nCode, int wParam, IntPtr lParam); //客户端键盘处理事件
public delegate void ProcessKeyHandle(HookStruct param, out bool handle); //接收SetWindowsHookEx返回值
private static int _hHookValue = 0; //勾子程序处理事件
private HookHandle _KeyBoardHookProcedure; //Hook结构
[StructLayout(LayoutKind.Sequential)]
public class HookStruct
{
public int vkCode;
public int scanCode;
public int flags;
public int time;
public int dwExtraInfo;
} //设置钩子
[DllImport("user32.dll")]
private static extern int SetWindowsHookEx(int idHook, HookHandle lpfn, IntPtr hInstance, int threadId); //取消钩子
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern bool UnhookWindowsHookEx(int idHook); //调用下一个钩子
[DllImport("user32.dll")]
private static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam); //获取当前线程ID
[DllImport("kernel32.dll")]
private static extern int GetCurrentThreadId(); //Gets the main module for the associated process.
[DllImport("kernel32.dll")]
private static extern IntPtr GetModuleHandle(string name); private IntPtr _hookWindowPtr = IntPtr.Zero; //构造器
public KeyboardHookLib() { } //外部调用的键盘处理事件
private static ProcessKeyHandle _clientMethod = null; /// <summary>
/// 安装勾子
/// </summary>
/// <param name="hookProcess">外部调用的键盘处理事件</param>
public void InstallHook(ProcessKeyHandle clientMethod)
{
_clientMethod = clientMethod; // 安装键盘钩子
if (_hHookValue == 0)
{
_KeyBoardHookProcedure = new HookHandle(OnHookProc); _hookWindowPtr = GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName); _hHookValue = SetWindowsHookEx(
WH_KEYBOARD_LL,
_KeyBoardHookProcedure,
_hookWindowPtr,
0); //如果设置钩子失败.
if (_hHookValue == 0) UninstallHook();
}
} //取消钩子事件
public void UninstallHook()
{
if (_hHookValue != 0)
{
bool ret = UnhookWindowsHookEx(_hHookValue);
if (ret) _hHookValue = 0;
}
} //钩子事件内部调用,调用_clientMethod方法转发到客户端应用。 private static int OnHookProc(int nCode, int wParam, IntPtr lParam)
{ if ((lParam.ToInt32() & 0x80000000) == 0x80000000)
{ if (nCode >= 0)
{
//转换结构
HookStruct hookStruct = (HookStruct)Marshal.PtrToStructure(lParam, typeof(HookStruct));
if (_clientMethod != null)
{
bool handle = false;
//调用客户提供的事件处理程序。
_clientMethod(hookStruct, out handle);
if (handle)
return 1; //1:表示拦截键盘,return 退出
}
}
}
return CallNextHookEx(_hHookValue, nCode, wParam, lParam);
}
}
}
主界面
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Vjsdn.Tech.KeyboardHook;
using System.IO;namespace ProjectWarHelp
{
public partial class Record : Form
{
private KeyboardHookLib _keyboardHook = null;
public Record()
{
InitializeComponent();
} private void btnOpen_Click(object sender, EventArgs e)
{
lblState.Text = "数据记录中。。";
lblState.ForeColor = Color.Red;
_keyboardHook = new KeyboardHookLib();
_keyboardHook.InstallHook(OnKeyPress);
} private void btnClose_Click(object sender, EventArgs e)
{
lblState.Text = "数据记录中断";
lblState.ForeColor = Color.SaddleBrown;
if (_keyboardHook != null)
_keyboardHook.UninstallHook();
}
public void OnKeyPress(KeyboardHookLib .HookStruct hookStruct,out bool handle)
{
handle = false;
Keys key = (Keys)hookStruct.vkCode;
FileStream fs = new FileStream(Application.StartupPath + @"\Record.txt", FileMode.Open);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.Write(key.ToString() + " ");
sw.Close();
} private void Record_Load(object sender, EventArgs e)
{
lblState.Text = "数据记录中断";
lblState.ForeColor = Color.SaddleBrown;
if (!File.Exists(Application.StartupPath + @"\Record.txt"))
{
FileStream fs = new FileStream(Application.StartupPath + @"\Record.txt", FileMode.CreateNew);
fs.Close();
}
FileStream fs2 = new FileStream(Application.StartupPath + @"\Record.txt", FileMode.Open);
StreamWriter sw = new StreamWriter(fs2);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine("");
sw.WriteLine("开始时间:"+System .DateTime .Now .ToString ());
sw.Close();
} private void Record_FormClosing(object sender, FormClosingEventArgs e)
{
FileStream fs = new FileStream(Application.StartupPath + @"\Record.txt", FileMode.Open);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine("");
sw.WriteLine("结束时间:"+System.DateTime.Now.ToString());
sw.Close();
}
}
}
问题还是解决不了private static int OnHookProc(int nCode, int wParam, IntPtr lParam)运行2次
加if ((lParam.ToInt32() & 0x80000000) == 0x80000000)这一直不能达到效果,lparam的32位一直是0,keyyon,keydown不是lparam的32位值会改变,问题到底出在哪,头都爆了。