I am using CGEvent.TapCreate to monitor for key and mouse events. In the scenario I am using it, I need to be able to allow some click events and key events through and others need to be destroyed. My boolean value works, however every so often my CGEventType is TapDisabledByUserInput and that completely stops monitoring for the events and the callback function isn't called at all. I believe I need to do something similar to Mac event tap just delays discarded events which uses pointers to the event tap. This is c++ code whereas my codebase is written in c# using Xamarin.mac for a background application. What I am trying to do is return null for certain events and post the rest.This is almost achieved, just when TapDisabledByUserInput is returned, it breaks. I have tried to call the initial method but this gives me an error.
public void Start()
{
SetupEventTap();
}
private void SetupEventTap()
{
var call = new CGEvent.CGEventTapCallback(Callback);
var data = new IntPtr();
CGEventMask trackedEvents = CGEventMask.LeftMouseDown | CGEventMask.KeyDown | CGEventMask.LeftMouseUp
| CGEventMask.KeyUp;
var eventTap = CGEvent.CreateTap(CGEventTapLocation.HID, CGEventTapPlacement.HeadInsert, CGEventTapOptions.Default,
trackedEvents, call, data);
var cfMachPort = new CFMachPort(eventTap.Handle);
CFRunLoopSource runLoopSource = cfMachPort.CreateRunLoopSource();
CFRunLoop.Main.AddSource(runLoopSource, CFRunLoop.ModeCommon);
CGEvent.TapEnable(eventTap);
}
private IntPtr Callback(IntPtr eventTap, CGEventType cGEventType, IntPtr eventRef, IntPtr usrInfo)
{
Console.WriteLine("Event Tap" + eventTap + ", Event type: " + cGEventType + ", Event ref: " + eventRef + ", User Info: " + usrInfo);
//if (cGEventType == CGEventType.TapDisabledByUserInput)
//{
//}
if (canType)
{
canType = false;
return eventTap;
}
if (canClick)
{
canClick = false;
return eventTap;
}
return IntPtr.Zero;
}