学习啦 > 学习电脑 > 电脑硬件知识 > 键盘鼠标 > js获取桌面鼠标键盘事件怎么实现

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

时间: 沈迪豪908 分享

学习啦在线学习网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)]

  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.如何设置桌面鼠标图案

2006955