Я слушаю хуки для клавиатуры и мыши с помощью RegisterRawInputDevices
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);
}
}
мое приложение будет работать под учетной записью пользователя. Я могу получить все события мыши и клавиатуры из обычного приложения. если приложение работает с правами администратора, я не могу получить события. Как этого добиться, приветствуются любые предложения.