键盘钩子分几种,有进程钩子,就是只是捕获某一个特定进程的键盘输入。有程序钩子,只捕获某一个程序的键盘输入。还有全局钩子,可以捕获所有进程或者程序的键盘输入。捕获就是说钩子程序能获取你输入的信息,比方说原来有一些QQ木马就是安装键盘钩子,用于记录你输入的QQ号和密码,又或者一些游戏的盗号程序就是这样的。
建议进行查杀
如果服务器端是windows环境的话,那么钩子实际就是批处理文件(扩展名bat)
这些钩子都存放在服务器端的hooks文件夹内,比较常用的钩子包括pre-committmpl、post-committmpl等,pre-committmpl是当服务器端接收到commit请求的时候自动调用这个钩子,post-committmpl是当完成commit *** 作的时候服务器端自动调用这个钩子,在windows环境下钩子程序的扩展名要改成bat,而不能用tmpl
至于钩子的内容,就看自己的发挥了,你需要熟悉DOS批处理文件的编写技巧,常见的应用比如:当commit完成后,自动调用钩子程序,通过svn update命令,将最新版本发布到服务器上的测试环境中
软件HOOK即钩子函数,钩子函数是Windows消息处理机制的一部分,通过设置“钩子”,应用程序可以在系统级对所有消息、事件进行过滤,访问在正常情况下无法访问的消息。
钩子的本质是一段用以处理系统消息的程序,通过系统调用,把它挂入系统。每当特定的消息发出,在没有到达目的窗口前,钩子程序就先捕获该消息,亦即钩子函数先得到控制权。这时钩子函数即可以加工处理(改变)该消息,也可以不作处理而继续传递该消息,还可以强制结束消息的传递。
HOOK技术在windows系统下编程,应该会接触到api函数的使用,常用的api函数大概有2000个左右。今天随着控件,stl等高效编程技术的出现,api的使用概率在普通的用户程序上就变得越来越小了。当诸如控件这些现成的手段不能实现的功能时,我们还需要借助api。
using System;
using SystemRuntimeInteropServices;
using SystemReflection;
using SystemThreading;
using SystemWindowsForms;
using SystemDiagnostics;
using SystemCollectionsGeneric;
namespace ConsoleApplicationTest
{
/// <summary>
/// 这个类可以让你得到一个在运行中程序的所有鼠标和键盘事件 (系统钩子)
/// 并且引发一个带KeyEventArgs和MouseEventArgs参数的NET事件以便你很容易使用这些信息
/// 鼠标钩子处理函数:MouseHookProc
/// 键盘钩子处理函数:KeyboardHookProc
/// 使用方法:
/// 1新建该类。
/// 2声明OnMouseActivity、OnKeyDownEvent、OnKeyUpEvent、OnKeyPressEvent
/// 3使用时调用相应的事件即可。
/// 4要添加应用请在MouseHookProc()和KeyboardHookProc()中自己添加。
/// 使用示例:
/// public mkhook = new KeyBordHook();
/// mkhookOnKeyDownEvent += new KeyEventHandler(OnKeyDownEventHandler);
/// mkhookOnMouseActivity += new MouseEventHandler(OnMouseEventHandler);
/// private void OnKeyDownEventHandler(object sender, KeyEventArgs e)
/// {
/// ConsoleWriteLine("KeyDown");
/// }
/// private void OnMouseEventHandler(object sender, MouseEventArgs e)
/// {
/// ConsoleWriteLine("MouseEvent");
/// }
/// </summary>
/// <remarks>
/// 修改:xyan nay6@163com
/// 修改时间:100611
/// </remarks>
public class KeyBordHook : IDisposable
{
private const int WM_KEYDOWN = 0x100;
private const int WM_KEYUP = 0x101;
private const int WM_SYSKEYDOWN = 0x104;
private const int WM_SYSKEYUP = 0x105;
//全局的事件
public event MouseEventHandler OnMouseActivity;//鼠标事件
public event KeyEventHandler OnKeyDownEvent;//键按下
public event KeyEventHandler OnKeyUpEvent;//键放下
public event KeyPressEventHandler OnKeyPressEvent;//键按下
static int hMouseHook = 0; //鼠标钩子句柄
static int hKeyboardHook = 0; //键盘钩子句柄
//鼠标常量
public const int WH_MOUSE = 7;
public const int WH_MOUSE_LL = 14; //鼠标常量
public const int WH_KEYBOARD_LL = 13; //键盘常量
HookProc MouseHookProcedure; //声明鼠标钩子事件类型
HookProc KeyboardHookProcedure; //声明键盘钩子事件类型
//Declare wrapper managed POINT class
[StructLayout(LayoutKindSequential)]
public class POINT
{
public int x;
public int y;
}
//声明鼠标钩子的封送结构类型
[StructLayout(LayoutKindSequential)]
public class MouseHookStruct
{
public POINT pt;
public int hwnd;
public int wHitTestCode;
public int dwExtraInfo;
}
//声明键盘钩子的封送结构类型
[StructLayout(LayoutKindSequential)]
public class KeyboardHookStruct
{
public int vkCode; //表示一个在1到254间的虚似键盘码
public int scanCode; //表示硬件扫描码
public int flags;
public int time;
public int dwExtraInfo;
}
//装置钩子的函数
[DllImport("user32dll", CharSet = CharSetAuto, CallingConvention = CallingConventionStdCall)]
public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
//卸下钩子的函数
[DllImport("user32dll", CharSet = CharSetAuto, CallingConvention = CallingConventionStdCall)]
public static extern bool UnhookWindowsHookEx(int idHook);
//下一个钩挂的函数
[DllImport("user32dll", CharSet = CharSetAuto, CallingConvention = CallingConventionStdCall)]
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("kernel32dll", CharSet = CharSetAuto,
CallingConvention = CallingConventionStdCall)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);
//先前按下的键
public List<Keys> preKeys = new List<Keys>();
/// <summary>
/// 墨认的构造函数构造当前类的实例并自动的运行起来
/// </summary>
public KeyBordHook()
{
Start();
}
//析构函数
~KeyBordHook()
{
Stop();
}
public void Dispose()
{
Stop();
}
public void Start()
{
//安装键盘钩子
if (hKeyboardHook == 0)
{
KeyboardHookProcedure = new HookProc(KeyboardHookProc);
//hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProcedure, MarshalGetHINSTANCE(AssemblyGetExecutingAssembly()GetModules()[0]), 0);
Process curProcess = ProcessGetCurrentProcess();
ProcessModule curModule = curProcessMainModule;
hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL,
KeyboardHookProcedure,
GetModuleHandle(curModuleModuleName),
0);
if (hKeyboardHook == 0)
{
Stop();
ConsoleWriteLine("SetWindowsHookEx ist failed");
throw new Exception("SetWindowsHookEx ist failed");
}
}
// install Mouse hook
if (hMouseHook == 0)
{
// Create an instance of HookProc
MouseHookProcedure = new HookProc(thisMouseHookProc);
Process curProcess = ProcessGetCurrentProcess();
ProcessModule curModule = curProcessMainModule;
//curProcessThreads[0]Id;
hMouseHook = SetWindowsHookEx(WH_MOUSE_LL,
MouseHookProcedure,
GetModuleHandle(curModuleModuleName),
0);//curProcessId//ProcessGetCurrentProcess()Id
//If SetWindowsHookEx fails
if (hMouseHook == 0)
{
Stop();
ConsoleWriteLine("SetWindowsHookEx failed ");
throw new Exception("SetWindowsHookEx failed ");
}
}
}
public void Stop()
{
bool retMouse = true;
bool retKeyboard = true;
if (hMouseHook != 0)
{
retMouse = UnhookWindowsHookEx(hMouseHook);
hMouseHook = 0;
}
if (hKeyboardHook != 0)
{
retKeyboard = UnhookWindowsHookEx(hKeyboardHook);
hKeyboardHook = 0;
}
//如果卸下钩子失败
if (!(retKeyboard))
{
ConsoleWriteLine("UnhookWindowsHookEx failed");
throw new Exception("UnhookWindowsHookEx failed");
}
}
private const int WM_MOUSEMOVE = 0x200;
private const int WM_LBUTTONDOWN = 0x201;
private const int WM_RBUTTONDOWN = 0x204;
private const int WM_MBUTTONDOWN = 0x207;
private const int WM_LBUTTONUP = 0x202;
private const int WM_RBUTTONUP = 0x205;
private const int WM_MBUTTONUP = 0x208;
private const int WM_LBUTTONDBLCLK = 0x203;
private const int WM_RBUTTONDBLCLK = 0x206;
private const int WM_MBUTTONDBLCLK = 0x209;
private int MouseHookProc(int nCode, Int32 wParam, IntPtr lParam)
{
// if ok and someone listens to our events
if ((nCode >= 0) && (OnMouseActivity != null))
{
MouseButtons button = MouseButtonsNone;
switch (wParam)
{
case WM_LBUTTONDOWN://鼠标左键
case WM_LBUTTONUP:
case WM_LBUTTONDBLCLK:
button = MouseButtonsLeft;
break;
case WM_RBUTTONDOWN://鼠标右键
case WM_RBUTTONUP:
case WM_RBUTTONDBLCLK:
button = MouseButtonsRight;
break;
//case WM_MBUTTONDOWN:
// //case WM_MBUTTONUP:
// //case WM_MBUTTONDBLCLK:
// button = MouseButtonsMiddle;
// break;
default:
break;
}
int clickCount = 0;
if (button != MouseButtonsNone)
{
if (wParam == WM_LBUTTONDBLCLK || wParam == WM_RBUTTONDBLCLK || wParam == WM_MBUTTONDBLCLK)
{
clickCount = 2;
}
else
{
clickCount = 1;
}
}
//}
//ConsoleWriteLine(clickCountToString());
//Marshall the data from callback
MouseHookStruct MyMouseHookStruct = (MouseHookStruct)MarshalPtrToStructure(lParam, typeof(MouseHookStruct));
MouseEventArgs e = new MouseEventArgs(
button,
clickCount,
MyMouseHookStructptx,
MyMouseHookStructpty,
0);
OnMouseActivity(this, e);
}
return CallNextHookEx(hMouseHook, nCode, wParam, lParam);
}
private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
{
ConsoleWriteLine("In KeyboardHookProc");
if ((nCode >= 0) && (OnKeyDownEvent != null || OnKeyUpEvent != null || OnKeyPressEvent != null))
{
KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)MarshalPtrToStructure(lParam, typeof(KeyboardHookStruct));
//当有OnKeyDownEvent 或 OnKeyPressEvent不为null时,ctrl alt shift keyup时 preKeys
//中的对应的键增加
if ((OnKeyDownEvent != null || OnKeyPressEvent != null) && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN))
{
Keys keyData = (Keys)MyKeyboardHookStructvkCode;
if (IsCtrlAltShiftKeys(keyData) && preKeysIndexOf(keyData) == -1)
{
preKeysAdd(keyData);
ConsoleWriteLine(keyDataToString());
}
}
//引发OnKeyDownEvent
if (OnKeyDownEvent != null && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN))
{
Keys keyData = (Keys)MyKeyboardHookStructvkCode;
KeyEventArgs e = new KeyEventArgs(GetDownKeys(keyData));
OnKeyDownEvent(this, e);
}
//引发OnKeyPressEvent
if (OnKeyPressEvent != null && wParam == WM_KEYDOWN)
{
byte[] keyState = new byte[256];
GetKeyboardState(keyState);
byte[] inBuffer = new byte[2];
if (ToAscii(MyKeyboardHookStructvkCode,
MyKeyboardHookStructscanCode,
keyState,
inBuffer,
MyKeyboardHookStructflags) == 1)
{
KeyPressEventArgs e = new KeyPressEventArgs((char)inBuffer[0]);
OnKeyPressEvent(this, e);
}
}
//当有OnKeyDownEvent 或 OnKeyPressEvent不为null时,ctrl alt shift keyup时 preKeys
//中的对应的键删除
if ((OnKeyDownEvent != null || OnKeyPressEvent != null) && (wParam == WM_KEYUP || wParam == WM_SYSKEYUP))
{
Keys keyData = (Keys)MyKeyboardHookStructvkCode;
if (IsCtrlAltShiftKeys(keyData))
{
for (int i = preKeysCount - 1; i >= 0; i--)
{
if (preKeys[i] == keyData)
{
preKeysRemoveAt(i);
}
}
}
}
//引发OnKeyUpEvent
if (OnKeyUpEvent != null && (wParam == WM_KEYUP || wParam == WM_SYSKEYUP))
{
Keys keyData = (Keys)MyKeyboardHookStructvkCode;
KeyEventArgs e = new KeyEventArgs(GetDownKeys(keyData));
OnKeyUpEvent(this, e);
}
}
ConsoleWriteLine("Out KeyboardHookProc");
return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
}
private Keys GetDownKeys(Keys key)
{
Keys rtnKey = KeysNone;
foreach (Keys keyTemp in preKeys)
{
switch (keyTemp)
{
case KeysLControlKey:
case KeysRControlKey:
rtnKey = rtnKey | KeysControl;
break;
case KeysLMenu:
case KeysRMenu:
rtnKey = rtnKey | KeysAlt;
break;
case KeysLShiftKey:
case KeysRShiftKey:
rtnKey = rtnKey | KeysShift;
break;
default:
break;
}
}
rtnKey = rtnKey | key;
return rtnKey;
}
private Boolean IsCtrlAltShiftKeys(Keys key)
{
switch (key)
{
case KeysLControlKey:
case KeysRControlKey:
case KeysLMenu:
case KeysRMenu:
case KeysLShiftKey:
case KeysRShiftKey:
return true;
default:
return false;
}
}
}
}
以上回答你满意么?
你是用动态链接库写的吗??还是就是在本程序里?一般来说任务栏不属你的程序管,所以不受控制。
如果你想要控制整个计算机,那得用动态链接库。做成一个全局的钩子
我之前写过,不小心把电脑全屏蔽了,只好强制关机,呵呵···
很难说有什么问题
我之前也是做过钩子程序
其实按F5,和F6是一样的,只是捕捉了一个按键
然后进行了相应的 *** 作
照道理应该不会慢
要么是你F5按下后,打开网页后还有其他 *** 作没有完成,导致F6延时
要么是你F6按下后,有其他 *** 作,导致F6本身比较慢
以上就是关于进程钩子 键盘钩子 内核钩子都是什么意思呢。全部的内容,包括:进程钩子 键盘钩子 内核钩子都是什么意思呢。、SVN钩子程序如何写,新手入门,完全不懂,从零开始、软件hook是什么等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)