C#使用SnsSharp实现鼠标键盘钩子,实现全局按键响应

gitee下载地址:https://gitee.com/linsns/snssharp

一、键盘事件,使用SnsKeyboardHook

按键事件共有3个:

        KeyDown(按键按下)        

        KeyUp(按键松开)         

        KeyPress(按键按下并松开)

以KeyDown事件为例,使用代码如下:

SnsKeyboardHook snsKeyboardhook = new SnsKeyboardHook();
snsKeyboardhook.KeyDown += SnsKeyboardhook_KeyDown; 
snsKeyboardhook.Start();

private void SnsKeyboardhook_KeyDown(object sender, KeyEventArgs e)
{
    labState.Text = string.Format("状态:当前按下键盘{0}", e.KeyCode);
}

其中SnsKeyboardhook_KeyDown就是按键响应函数。

二、鼠标事件,使用SnsMouseHook

鼠标事件共有6个:

        MouseDown(鼠标按下)        

        MouseUp(鼠标松开)        

        MouseMove(鼠标移动)        

        MouseWheel(鼠标滚动鼠轮)         

        Click(鼠标单击)        

        DoubleClick(鼠标双击)

以MouseDown事件为例,使用代码如下:

SnsMouseHook snsMouseHook = new SnsMouseHook();
snsMouseHook.MouseDown += SnsMouseHook_MouseDown;
snsMouseHook.Start();

private void SnsMouseHook_MouseDown(object sender, MouseEventArgs e)
{
    labState.Text = string.Format("状态:当前按下鼠标{0}", e.Button);
}

其中SnsMouseHook_MouseDown就是鼠标按下响应函数。

三、模拟键盘事件,使用SnsKeyboardSimulator

模拟按键事件共有3个:

        KeyDown(按键按下)        

        KeyUp(按键松开)         

        KeyPress(按键按下并松开)

模拟按下空格键:SnsKeyboardSimulator.KeyDown(Keys.Space);

模拟按下确定键:SnsKeyboardSimulator.KeyDown(Keys.Enter);

四、模拟鼠标事件,使用SnsMouseSimulator

模拟鼠标事件共有5个:

        MouseDown(鼠标按下)        

        MouseUp(鼠标松开)         

        MouseMove(鼠标移动)        

        Click(鼠标单击)        

        DoubleClick(鼠标双击)

模拟鼠标左键按下:SnsMouseSimulator.MouseDown(MouseButtons.Left);

模拟鼠标移动:SnsMouseSimulator.MouseMouve(x, y);

效果:

技术交流

QQ群: 577276243

你可能感兴趣的:(SnsSharp,c#,键盘,鼠标)