基本信息
源码名称:C#键盘钩子操作源码 实例下载
源码大小:0.03M
文件格式:.rar
开发语言:C#
更新时间:2013-01-08
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
C#键盘钩子操作源码
#region Constructors
/// <summary>
/// Sets up a keyboard hook to trap all keystrokes without
/// passing any to other applications.
/// </summary>
public KeyboardHook()
{
proc = new HookHandlerDelegate(HookCallback);
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
hookID = NativeMethods.SetWindowsHookEx(WH_KEYBOARD_LL, proc,
NativeMethods.GetModuleHandle(curModule.ModuleName), 0);
}
}
/// <summary>
/// Sets up a keyboard hook with custom parameters.
/// </summary>
/// <param name="param">A valid name from the Parameter enum; otherwise, the
/// default parameter Parameter.None will be used.</param>
public KeyboardHook(string param)
: this()
{
if (!String.IsNullOrEmpty(param) && Enum.IsDefined(typeof(Parameters), param))
{
SetParameters((Parameters)Enum.Parse(typeof(Parameters), param));
}
}
/// <summary>
/// Sets up a keyboard hook with custom parameters.
/// </summary>
/// <param name="param">A value from the Parameters enum.</param>
public KeyboardHook(Parameters param)
: this()
{
SetParameters(param);
}
private void SetParameters(Parameters param)
{
switch (param)
{
case Parameters.None:
break;
case Parameters.AllowAltTab:
AllowAltTab = true;
break;
case Parameters.AllowWindowsKey:
AllowWindowsKey = true;
break;
case Parameters.AllowAltTabAndWindows:
AllowAltTab = true;
AllowWindowsKey = true;
break;
case Parameters.PassAllKeysToNextApp:
PassAllKeysToNextApp = true;
break;
}
}
#endregion
#region Check Modifier keys
/// <summary>
/// Checks whether Alt, Shift, Control or CapsLock
/// is enabled at the same time as another key.
/// Modify the relevant sections and return type
/// depending on what you want to do with modifier keys.
/// </summary>
private void CheckModifiers()
{
StringBuilder sb = new StringBuilder();
if ((NativeMethods.GetKeyState(VK_CAPITAL) & 0x0001) != 0)
{
//CAPSLOCK is ON
sb.AppendLine("Capslock is enabled.");
}
if ((NativeMethods.GetKeyState(VK_SHIFT) & 0x8000) != 0)
{
//SHIFT is pressed
sb.AppendLine("Shift is pressed.");
}
if ((NativeMethods.GetKeyState(VK_CONTROL) & 0x8000) != 0)
{
//CONTROL is pressed
sb.AppendLine("Control is pressed.");
}
if ((NativeMethods.GetKeyState(VK_MENU) & 0x8000) != 0)
{
//ALT is pressed
sb.AppendLine("Alt is pressed.");
}
Console.WriteLine(sb.ToString());
}
#endregion Check Modifier keys
#region Hook Callback Method
/// <summary>
/// Processes the key event captured by the hook.
/// </summary>
private IntPtr HookCallback(
int nCode, IntPtr wParam, ref KBDLLHOOKSTRUCT lParam)
{
bool AllowKey = PassAllKeysToNextApp;
//Filter wParam for KeyUp events only
if (nCode >= 0 &&
(wParam == (IntPtr)WM_KEYUP || wParam == (IntPtr)WM_SYSKEYUP))
{
// Check for modifier keys, but only if the key being
// currently processed isn't a modifier key (in other
// words, CheckModifiers will only run if Ctrl, Shift,
// CapsLock or Alt are active at the same time as
// another key)
if (!(lParam.vkCode >= 160 && lParam.vkCode <= 164))
{
CheckModifiers();
}
// Check for key combinations that are allowed to
// get through to Windows
//
// Ctrl Esc or Windows key
if (AllowWindowsKey)
{
switch (lParam.flags)
{
//Ctrl Esc
case 0:
if (lParam.vkCode == 27)
AllowKey = true;
break;
//Windows keys
case 1:
if ((lParam.vkCode == 91) || (lParam.vkCode == 92))
AllowKey = true;
break;
}
}
// Alt Tab
if (AllowAltTab)
{
if ((lParam.flags == 32) && (lParam.vkCode == 9))
AllowKey = true;
}
OnKeyIntercepted(new KeyboardHookEventArgs(lParam.vkCode, AllowKey));
//If this key is being suppressed, return a dummy value
if (AllowKey == false)
return (System.IntPtr)1;
}
//Pass key to next application
return NativeMethods.CallNextHookEx(hookID, nCode, wParam, ref lParam);
}
#endregion
#region Event Handling
/// <summary>
/// Raises the KeyIntercepted event.
/// </summary>
/// <param name="e">An instance of KeyboardHookEventArgs</param>
public void OnKeyIntercepted(KeyboardHookEventArgs e)
{
if (KeyIntercepted != null)
KeyIntercepted(e);
}
/// <summary>
/// Delegate for KeyboardHook event handling.
/// </summary>
/// <param name="e">An instance of InterceptKeysEventArgs.</param>
public delegate void KeyboardHookEventHandler(KeyboardHookEventArgs e);
/// <summary>
/// Event arguments for the KeyboardHook class's KeyIntercepted event.
/// </summary>
public class KeyboardHookEventArgs : System.EventArgs
{
private string keyName;
private int keyCode;
private bool passThrough;
/// <summary>
/// The name of the key that was pressed.
/// </summary>
public string KeyName
{
get { return keyName; }
}
/// <summary>
/// The virtual key code of the key that was pressed.
/// </summary>
public int KeyCode
{
get { return keyCode; }
}
/// <summary>
/// True if this key combination was passed to other applications,
/// false if it was trapped.
/// </summary>
public bool PassThrough
{
get { return passThrough; }
}
public KeyboardHookEventArgs(int evtKeyCode, bool evtPassThrough)
{
keyName = ((Keys)evtKeyCode).ToString();
keyCode = evtKeyCode;
passThrough = evtPassThrough;
}
}
#endregion