下面是源代码 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;namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
static int hHook = 1;
const int WH_KEYBOARD = 7;
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (hHook == 0)
{
NativeMethods.HookProc hp = new NativeMethods.HookProc(KbHookProc);
hHook = NativeMethods.SetWindowsHookEx(WH_KEYBOARD, hp, IntPtr.Zero, NativeMethods.FindWindow(null, "notepad").ToInt32());
//NativeMethods.FindWindow(null,"MM").ToInt32()); if (hHook == 0)
{
MessageBox.Show("SetWindowsHookEx Failed");
return;
}
button1.Text = "UnHook Windows Hook";
}
else
{
UnHook();
}
}
void UnHook()
{
if (hHook > 0)
{
bool ret = NativeMethods.UnhookWindowsHookEx(hHook); if (ret == false)
{
MessageBox.Show("UnhookWindowsHookEx Failed");
return;
}
hHook = 0;
button1.Text = "Set Windows Hook";
this.Text = "Keyboard Hook";
}
} public static int KbHookProc(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode < 0)
{
return NativeMethods.CallNextHookEx(hHook, nCode, wParam, lParam);
}
else
{
// MessageBox.Show(wParam.ToString());
int r = NativeMethods.CallNextHookEx(hHook, nCode, wParam, lParam); if (wParam == (IntPtr)256)
{
}
else
{
KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct)); MessageBox.Show("您按下了"+MyKeyboardHookStruct.vkCode.ToString());
}
return r;
}
}
}
}using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApplication1
{
public class KeyboardHookStruct
{
public int vkCode; //表示一个在1到254间的虚似键盘码
public int scanCode; //表示硬件扫描码
public int flags;
public int time;
public int dwExtraInfo; }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;namespace WindowsFormsApplication1
{
internal static class NativeMethods
{
public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[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, IntPtr wParam, IntPtr lParam);
}
}
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;namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
static int hHook = 1;
const int WH_KEYBOARD = 7;
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (hHook == 0)
{
NativeMethods.HookProc hp = new NativeMethods.HookProc(KbHookProc);
hHook = NativeMethods.SetWindowsHookEx(WH_KEYBOARD, hp, IntPtr.Zero, NativeMethods.FindWindow(null, "notepad").ToInt32());
//NativeMethods.FindWindow(null,"MM").ToInt32()); if (hHook == 0)
{
MessageBox.Show("SetWindowsHookEx Failed");
return;
}
button1.Text = "UnHook Windows Hook";
}
else
{
UnHook();
}
}
void UnHook()
{
if (hHook > 0)
{
bool ret = NativeMethods.UnhookWindowsHookEx(hHook); if (ret == false)
{
MessageBox.Show("UnhookWindowsHookEx Failed");
return;
}
hHook = 0;
button1.Text = "Set Windows Hook";
this.Text = "Keyboard Hook";
}
} public static int KbHookProc(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode < 0)
{
return NativeMethods.CallNextHookEx(hHook, nCode, wParam, lParam);
}
else
{
// MessageBox.Show(wParam.ToString());
int r = NativeMethods.CallNextHookEx(hHook, nCode, wParam, lParam); if (wParam == (IntPtr)256)
{
}
else
{
KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct)); MessageBox.Show("您按下了"+MyKeyboardHookStruct.vkCode.ToString());
}
return r;
}
}
}
}using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApplication1
{
public class KeyboardHookStruct
{
public int vkCode; //表示一个在1到254间的虚似键盘码
public int scanCode; //表示硬件扫描码
public int flags;
public int time;
public int dwExtraInfo; }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;namespace WindowsFormsApplication1
{
internal static class NativeMethods
{
public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[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, IntPtr wParam, IntPtr lParam);
}
}
解决方案 »
- 关于C#写的服务器有哪些,大家聊聊
- 关于invoke
- 两个List<string> 合并?
- 急!!在线等,同一条SQL在程序里运行查出一条数据,在数据库里查询可以查出多条,为什么呢?
- C# winform 设置功能的窗口怎么设计(左边是设置项,右边是groupbox)
- 高分请求高手帮忙,自己想了10天也没解决我愿意拿出我所有的分来解决此问题
- comboBox1绑定dataset以后,如何取出Item的值。
- 多网卡通讯问题
- 如何调用NetMeeting时自动拨号?
- 怎样读取CONFIG里的信息
- 关于控制datagridvew中“某一列”的单元格的键盘输入(比如name列必须只能键入字母)
- byte数组问题
用IntPtr.Zero句柄可能不对,试试
Process curProcess = Process.GetCurrentProcess();
ProcessModule curModule = curProcess.MainModule;
[DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern IntPtr GetModuleHandle(string lpModuleName);
IntPtr.Zero----》GetModuleHandle(curModule.ModuleName)
这个方法里面,进了判断里面 if (ret == false)
{
MessageBox.Show("UnhookWindowsHookEx Failed");
return;
}
就跳出来了,还是要谢谢各位大哥的热心,先把帖子接了,,