键盘实现鼠标右键 js获取桌面鼠标键盘事件怎么实现

学习前端的同学你们知道怎么你们知道获取桌面鼠标键盘事件怎么实现吗?不知道的话跟着小编一起来学习了解获取桌面鼠标键盘事件吧。

获取桌面鼠标键盘事件的代码

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Runtime.InteropServices;

using System.Reflection;

namespace UDPService

{

/// <summary>

///

/// </summary>

[StructLayout(LayoutKind.Sequential)]

键盘实现鼠标右键 js获取桌面鼠标键盘事件怎么实现

public class POINT

{

public int x;

public int y;

}

/// <summary>

///

/// </summary>

[StructLayout(LayoutKind.Sequential)]

public class MouseHookStruct

{

public POINT pt;

public int hwnd;

public int wHitTestCode;

public int dwExtraInfo;

}

/// <summary>

///

/// </summary>

[StructLayout(LayoutKind.Sequential)]

public class KeyboardHookStruct

{

public int vkCode;

public int scanCode;

public int flags;

public int time;

public int dwExtraInfo;

}

/// <summary>

///

/// </summary>

public class GlobalHook

{

public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);

public GlobalHook()

{

Start();

}

~GlobalHook()

{

Stop();

}

public event MouseEventHandler OnMouseActivity;

public event KeyEventHandler KeyDown;

public event KeyPressEventHandler KeyPress;

public event KeyEventHandler KeyUp;

public delegate int GlobalHookProc(int nCode, Int32 wParam, IntPtr lParam);

static int hMouseHook = 0;

static int hKeyboardHook = 0;

public const int WH_MOUSE_LL = 14;

public const int WH_KEYBOARD_LL = 13;

GlobalHookProc MouseHookProcedure;

GlobalHookProc KeyboardHookProcedure;

[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]

public static extern int SetWindowsHookEx(int idHook, GlobalHookProc 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,

Int32 wParam, IntPtr lParam);

public void Start()

{

if (hMouseHook == 0)

{

MouseHookProcedure = new GlobalHookProc(MouseHookProc);

try

{

hMouseHook = SetWindowsHookEx(WH_MOUSE_LL,

MouseHookProcedure,

Marshal.GetHINSTANCE(

Assembly.GetExecutingAssembly().GetModules()[0]),

0);

}

catch (Exception err)

{ }

if (hMouseHook == 0)

{

Stop();

throw new Exception("SetWindowsHookEx failed.");

}

}

if (hKeyboardHook == 0)

{

KeyboardHookProcedure = new GlobalHookProc(KeyboardHookProc);

try

{

hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL,

KeyboardHookProcedure,

Marshal.GetHINSTANCE(

Assembly.GetExecutingAssembly().GetModules()[0]),

0);

}

catch (Exception err2)

{ }

if (hKeyboardHook == 0)

{

Stop();

throw new Exception("SetWindowsHookEx ist 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 (!(retMouse && retKeyboard))

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 ((nCode >= 0) && (OnMouseActivity != null))

{

MouseButtons button = MouseButtons.None;

switch (wParam)

{

case WM_LBUTTONDOWN:

//case WM_LBUTTONUP:

//case WM_LBUTTONDBLCLK:

button = MouseButtons.Left;

break;

case WM_RBUTTONDOWN:

//case WM_RBUTTONUP:

//case WM_RBUTTONDBLCLK:

button = MouseButtons.Right;

break;

}

int clickCount = 0;

if (button != MouseButtons.None)

if (wParam == WM_LBUTTONDBLCLK || wParam == WM_RBUTTONDBLCLK)

clickCount = 2;

else

clickCount = 1;

MouseHookStruct MyMouseHookStruct =

(MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));

MouseEventArgs e = new MouseEventArgs(

button,

clickCount,

MyMouseHookStruct.pt.x,

MyMouseHookStruct.pt.y,

0);

OnMouseActivity(this, e);

}

return CallNextHookEx(hMouseHook, nCode, wParam, 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);

private const int WM_KEYDOWN = 0x100;

private const int WM_KEYUP = 0x101;

private const int WM_SYSKEYDOWN = 0x104;

private const int WM_SYSKEYUP = 0x105;

private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)

{

if ((nCode >= 0) && (KeyDown != null || KeyUp != null || KeyPress != null))

{

KeyboardHookStruct MyKeyboardHookStruct =

(KeyboardHookStruct)Marshal.PtrToStructure(lParam,

typeof(KeyboardHookStruct));

if (KeyDown != null && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN))

{

Keys keyData = (Keys)MyKeyboardHookStruct.vkCode;

KeyEventArgs e = new KeyEventArgs(keyData);

KeyDown(this, e);

}

if (KeyPress != null && wParam == WM_KEYDOWN)

{

byte[] keyState = new byte[256];

GetKeyboardState(keyState);

byte[] inBuffer = new byte[2];

if (ToAscii(MyKeyboardHookStruct.vkCode,

MyKeyboardHookStruct.scanCode,

keyState,

inBuffer,

MyKeyboardHookStruct.flags) == 1)

{

KeyPressEventArgs e = new KeyPressEventArgs((char)inBuffer[0]);

KeyPress(this, e);

}

}

if (KeyUp != null && (wParam == WM_KEYUP || wParam == WM_SYSKEYUP))

{

Keys keyData = (Keys)MyKeyboardHookStruct.vkCode;

KeyEventArgs e = new KeyEventArgs(keyData);

KeyUp(this, e);

}

}

return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);

}

}

}

猜你喜欢:

1.怎么用js获取鼠标移动坐标

2.怎么实现js跨浏览器获取鼠标按键的值

3.js怎么获取鼠标在div中的相对位置

4.win8.1 rtm有什么改进

5.怎么用js实现简单鼠标跟随效果

6.如何设置桌面鼠标图案

  

爱华网本文地址 » http://www.aihuau.com/a/240561/144283452.html

更多阅读

键盘实现鼠标右键 java鼠标和键盘事件怎么实现

学习java的人都一定会学习鼠标和键盘事件,不知道的话跟着小编一起来学习了解鼠标和键盘事件。java鼠标和键盘事件的实现方法1、使用MouseListener借口处理鼠标事件鼠标事件有5种:按下鼠标键,释放鼠标键,点击鼠标键,鼠标进入和鼠标退出

桌面空白右击没有属性 鼠标右键怎么没有属性

想知道鼠标右键没反应怎么办吗?下面是小编带来鼠标右键怎么没有属性的内容,欢迎阅读!鼠标右键没有属性解决方法:故障现象:右键点击桌面空白处,选择属性后没有反应;解决步骤:点击“开始”-“运行”,输入regedit后点击确定打开注册表编

键盘怎么代替鼠标右键 鼠标右键坏了怎么代替

想知道电脑的鼠标右键坏了有什么可以代替的吗?下面是小编带来鼠标右键坏了怎么代替的内容,欢迎阅读!鼠标右键坏了代替方法:鼠标左键失灵了,不要担心,跟着我一步步操作,即可应急于危难:首先,在键盘上同时按下Win+R两个键(Win键就是键盘下

鼠标右键在桌面上失灵 鼠标在桌面右键失灵怎么办

关于鼠标在桌面右键失灵的问题,很多网友还不太了解,那么下面就由小编来给你们说说鼠标在桌面右键失灵的解决方法吧,希望可以帮到你们哦!鼠标在桌面右键失灵的解决方法:修改注册表,注册表路径为:HKEY_CURRENT_USERSoftwareMicrosoftWind

怎么修改鼠标右键菜单 怎么修改桌面鼠标右键

网友们知道鼠标的右键怎么修改吗?下面是小编带来怎么修改桌面鼠标右键的内容,欢迎阅读!修改桌面鼠标右键方法:在“组织-文件夹和搜索选项”进行设置​打开文件夹选项选择查看选项卡设置在“文件夹选项”对话框中选择“查看”

声明:《键盘实现鼠标右键 js获取桌面鼠标键盘事件怎么实现》为网友十里温柔分享!如侵犯到您的合法权益请联系我们删除