无法从管理员进程下运行的进程获取Windows消息

时间:2018-09-04 06:41:36

标签: c# .net winforms raw-input

我正在使用RegisterRawInputDevices收听Windows键盘和鼠标钩子

 public static KeyboardMouseHook GetInstance()
  {
    if (_instance == null)
      {
        _instance = new KeyboardMouseHook();

      }
      return _instance;
   }

   /// <summary>
   /// This method is used to add the keyboard and mouse devices 
   /// </summary>
   /// <param name="windowHandle"></param>
   public void AddKeyboardMouseDevice(IntPtr windowHandle)
   {
     devices.Add(new RAWINPUTDEVICE(1, 6, 0x100, windowHandle));
     devices.Add(new RAWINPUTDEVICE(1, 2, 0x100, windowHandle));
   }

   /// <summary>
   /// This method is used to register the mouse and keyboard events
   /// </summary>
   /// <returns></returns>
   public bool RegisterDevices()
   {
     if (devices.Count == 0) return false;
     log.DebugFormat("Registering for keyboard and Mouse Events.");
     RAWINPUTDEVICE[] d = devices.ToArray();
     bool result = RegisterRawInputDevices(d, devices.Count, Marshal.SizeOf(typeof(RAWINPUTDEVICE)));
     return result;
   }

    /// <summary>
    /// This method will get trigged when the premessage filter occurs and 
    /// it will process the message and raise an event according to it.
    /// </summary>
    /// <param name="hRawInput"></param>
    /// <param name="wparam"></param>
    void ProcessRawInput(RawInput pData, IntPtr lparm)
    {
        try
        {
            if (pData.Header.Type == RawInputType.Keyboard)
            {
                if ((pData.Data.Keyboard.Flags == 0) ||
                    (pData.Data.Keyboard.Flags == 2))
                {
                    int asciiCode = VKeyToChar(pData.Data.Keyboard.VirtualKey, pData.Data.Keyboard.MakeCode);
                    if (KeyHookEvent != null)
                    {
                        KeyHookEvent(this, new KeyHookEventArgs((Keys)asciiCode, IsAltPressed, IsCtrlPressed, IsShiftPressed));
                    }
                }
            }
            else if (pData.Header.Type == RawInputType.Mouse)
            {
                POINT pt;
                GetCursorPos(out pt);
                if (MouseHookEvent != null)
                {
                    MouseHookEvent(this, new MouseHookEventArgs(pt.x, pt.y));
                }
            }
        }
        catch (Exception ex)
        {
            log.ErrorFormat("Exception occurred while processing the mouse event {0}, {1}", ex.Message, ex.StackTrace);
        }
    }

我的应用程序将以用户帐户运行。我能够从普通应用程序中获取所有鼠标和键盘事件。如果应用程序在管理员权限下运行,则我无法获取事件。欢迎如何实现此建议。

0 个答案:

没有答案