private CGEventType GetMoveEventType() { CGEventType eventType = 0; if (GetMouseButtonState(MouseButton.Left)) { eventType |= CGEventType.kCGEventLeftMouseDragged; } if (GetMouseButtonState(MouseButton.Middle)) { eventType |= CGEventType.kCGEventOtherMouseDragged; } if (GetMouseButtonState(MouseButton.Right)) { eventType |= CGEventType.kCGEventRightMouseDragged; } if (GetMouseButtonState(MouseButton.Forward)) { eventType |= CGEventType.kCGEventOtherMouseDragged; } if (GetMouseButtonState(MouseButton.Backward)) { eventType |= CGEventType.kCGEventOtherMouseDragged; } return(eventType == 0 ? CGEventType.kCGEventMouseMoved : eventType); }
private void PostMouseEvent(CGEventType type, CGMouseButton cgButton) { var curPos = GetPosition(); var cgPos = new CGPoint(curPos.X, curPos.Y) - offset; var mouseEventRef = CGEventCreateMouseEvent(IntPtr.Zero, type, cgPos, cgButton); CGEventPost(CGEventTapLocation.kCGHIDEventTap, mouseEventRef); CFRelease(mouseEventRef); }
private void PostMouseEvent(CGEventType type, CGMouseButton cgButton) { var eventRef = CGEventCreate(); var curPos = GetCursorPosition(); var cgPos = new CGPoint(curPos.X, curPos.Y); var mouseEventRef = CGEventCreateMouseEvent(ref eventRef, type, cgPos, cgButton); CGEventPost(ref mouseEventRef, type, cgPos, cgButton); CFRelease(eventRef); CFRelease(mouseEventRef); }
internal static void ToTypeAndButton(MOUSEEVENTF flags, out CGEventType type, out CGMouseButton button) { EventEntry e; if (EventTypes.TryGetValue((MOUSEEVENTF)((uint)flags & 0x1FFFF), out e)) { type = e.type; button = e.button; } else { type = CGEventType.Null; button = 0; } }
IntPtr HandleEventTap(IntPtr proxy, CGEventType eventType, IntPtr eventRef, IntPtr userInfo) { Action <CGPoint> handler = null; switch (eventType) { case CGEventType.LeftMouseUp: case CGEventType.RightMouseUp: case CGEventType.OtherMouseUp: handler = MouseUp; break; case CGEventType.MouseMoved: handler = MouseMoved; break; } if (handler != null) { handler(new CGEvent(eventRef).Location); } return(IntPtr.Zero); }
public extern static CGEventRef CGEventCreateMouseEvent(CGEventSourceRef source, CGEventType mouseType, CGPoint mouseCursorPosition, CGMouseButton mouseButton);
public static extern uint GetCounterForEventType(CGEventSourceStateID stateID, CGEventType eventType);
static IntPtr tapEventCallback(IntPtr proxy, CGEventType type, IntPtr evtHandle, IntPtr info) { using (new NSAutoreleasePool()) { var evt = new CGEvent(evtHandle); if (!SPMediaKeyTap.Shared.ShouldInterceptMediaKeyEvents) { return(evtHandle); } switch (type) { //kCGEventTapDisabledByTimeout case (CGEventType)kCGEventTapDisabledByTimeout: CGEvent.TapEnable(SPMediaKeyTap.Shared.eventPort); return(evtHandle); case (CGEventType)kCGEventTapDisabledByUser: return(evtHandle); } NSEvent nsEvent = null; try{ nsEvent = NSEvent.EventWithCGEvent(evtHandle); } catch (Exception ex) { return(evtHandle); } if (nsEvent.Subtype != SPSystemDefinedEventMediaKeys) { return(evtHandle); } long keyCode = ((nsEvent.Data1 & 0xFFFF0000) >> 16); var keyFlags = (nsEvent.Data1 & 0x0000FFFF); var keyState = (((keyFlags & 0xFF00) >> 8)) == 0xA; if (keyCode == 10 && keyFlags == 6972) { switch (nsEvent.Data2) { case 786608: // Play / Pause on OS < 10.10 Yosemite case 786637: // Play / Pause on OS >= 10.10 Yosemite Console.WriteLine(@"Play/Pause bluetooth keypress detected...sending corresponding media key event"); SPMediaKeyTap.Shared.RecievedEvent(MediaKey.Play); break; case 786611: // Next Console.WriteLine(@"Next bluetooth keypress detected...sending corresponding media key event"); SPMediaKeyTap.Shared.RecievedEvent(MediaKey.Next); break; case 786612: // Previous Console.WriteLine(@"Previous bluetooth keypress detected...sending corresponding media key event"); SPMediaKeyTap.Shared.RecievedEvent(MediaKey.Previous); break; case 786613: // Fast-forward Console.WriteLine(@"Fast-forward bluetooth keypress detected...sending corresponding media key event"); SPMediaKeyTap.Shared.RecievedEvent(MediaKey.Fast); break; case 786614: // Rewind Console.WriteLine(@"Rewind bluetooth keypress detected...sending corresponding media key event"); SPMediaKeyTap.Shared.RecievedEvent(MediaKey.Rewind); break; default: // TODO make this popup a message in the UI (with a link to submit the issue and a "don't show this message again" checkbox) LogManager.Shared.Log($"Unknown bluetooth key received: keyCode:{keyCode} keyFlags:{keyFlags} keyState:{keyState} {nsEvent.Data2}"); return(evtHandle); } } else { if (keyCode != NX_KEYTYPE_PLAY && keyCode != NX_KEYTYPE_FAST && keyCode != NX_KEYTYPE_REWIND && keyCode != NX_KEYTYPE_PREVIOUS && keyCode != NX_KEYTYPE_NEXT) { return(evtHandle); } //These always come in pairs if (lastKeyCode != keyCode) { SPMediaKeyTap.Shared.BeginInvokeOnMainThread(() => { SPMediaKeyTap.Shared.RecievedEvent((MediaKey)keyCode); }); lastKeyCode = keyCode; } else { lastKeyCode = 0; } } evt.Dispose(); return(IntPtr.Zero); } }
public extern static double GetSecondsSinceLastEventType(CGEventSourceStateID stateID, CGEventType eventType);
public CGEvent(CGEventSource source, CGEventType mouseType, CGPoint mouseCursorPosition, CGMouseButton mouseButton) { handle = CGEventCreateMouseEvent(source == null ? IntPtr.Zero : source.Handle, mouseType, mouseCursorPosition, mouseButton); }
extern static void CGEventSetType (IntPtr handle, CGEventType evtType);
public extern static CGEvent CGEventPost(ref CGEvent source, CGEventType mouseType, CGPoint mouseCursorPosition, CGMouseButton mouseButton);
public CGEvent(CGEventSource?source, CGEventType mouseType, CGPoint mouseCursorPosition, CGMouseButton mouseButton) : base(CGEventCreateMouseEvent(source.GetHandle(), mouseType, mouseCursorPosition, mouseButton), true) { }
public static extern double GetSecondsSinceLastEventType(CGEventSourceStateID stateID, CGEventType eventType);
extern static void CGEventSetType(IntPtr handle, CGEventType evtType);
extern static IntPtr CGEventCreateMouseEvent(IntPtr source, CGEventType mouseType, CGPoint mouseCursorPosition, CGMouseButton mouseButton);
public CGEvent (CGEventSource source, CGEventType mouseType, CGPoint mouseCursorPosition, CGMouseButton mouseButton) { handle = CGEventCreateMouseEvent (source == null ? IntPtr.Zero : source.Handle, mouseType, mouseCursorPosition, mouseButton); }
IntPtr MouseEventTapCallback( IntPtr proxy, CGEventType type, IntPtr @event, IntPtr refcon) { try { CursorState.SetIsConnected(true); switch (type) { case CGEventType.MouseMoved: case CGEventType.LeftMouseDragged: case CGEventType.RightMouseDragged: case CGEventType.OtherMouseDragged: { Carbon.HIPoint p = CG.EventGetLocation(@event); CursorState.X = (int)Math.Round(p.X); CursorState.Y = (int)Math.Round(p.Y); } break; case CGEventType.ScrollWheel: { // Note: OpenTK follows the win32 convention, where // (+h, +v) = (right, up). MacOS reports (+h, +v) = (left, up) // so we need to flip the horizontal scroll direction. double h = CG.EventGetDoubleValueField(@event, CGEventField.ScrollWheelEventPointDeltaAxis2) * MacOSFactory.ScrollFactor; double v = CG.EventGetDoubleValueField(@event, CGEventField.ScrollWheelEventPointDeltaAxis1) * MacOSFactory.ScrollFactor; CursorState.SetScrollRelative((float)(-h), (float)v); } break; case CGEventType.LeftMouseDown: case CGEventType.RightMouseDown: case CGEventType.OtherMouseDown: { int n = CG.EventGetIntegerValueField(@event, CGEventField.MouseEventButtonNumber); n = n == 1 ? 2 : n == 2 ? 1 : n; // flip middle and right button numbers to match OpenTK MouseButton b = MouseButton.Left + n; CursorState[b] = true; } break; case CGEventType.LeftMouseUp: case CGEventType.RightMouseUp: case CGEventType.OtherMouseUp: { int n = CG.EventGetIntegerValueField(@event, CGEventField.MouseEventButtonNumber); n = n == 1 ? 2 : n == 2 ? 1 : n; // flip middle and right button numbers to match OpenTK MouseButton b = MouseButton.Left + n; CursorState[b] = false; } break; } } catch (Exception e) { // Do not let any exceptions escape into unmanaged code! Debug.Print(e.ToString()); } return @event; }
public extern static uint /* uint32_t */ GetCounterForEventType(CGEventSourceStateID stateID, CGEventType eventType);
private void SetMouseButtonState(MouseButton button, bool newState) { inputDictionary.UpdateState(button, newState); moveEvent = GetMoveEventType(); pressedButtons = GetPressedCGButtons(); }
IntPtr MouseEventTapCallback( IntPtr proxy, CGEventType type, IntPtr @event, IntPtr refcon) { CursorState.SetIsConnected(true); switch (type) { case CGEventType.MouseMoved: case CGEventType.LeftMouseDragged: case CGEventType.RightMouseDragged: case CGEventType.OtherMouseDragged: { Carbon.HIPoint p = CG.EventGetLocation(@event); CursorState.X = (int)Math.Round(p.X); CursorState.Y = (int)Math.Round(p.Y); } break; case CGEventType.ScrollWheel: CursorState.SetScrollRelative( (float)CG.EventGetDoubleValueField(@event, CGEventField.ScrollWheelEventPointDeltaAxis2) * MacOSFactory.ScrollFactor, (float)CG.EventGetDoubleValueField(@event, CGEventField.ScrollWheelEventPointDeltaAxis1) * MacOSFactory.ScrollFactor); break; case CGEventType.LeftMouseDown: case CGEventType.RightMouseDown: case CGEventType.OtherMouseDown: { int n = CG.EventGetIntegerValueField(@event, CGEventField.MouseEventButtonNumber); MouseButton b = MouseButton.Left + n; CursorState[b] = true; } break; case CGEventType.LeftMouseUp: case CGEventType.RightMouseUp: case CGEventType.OtherMouseUp: { int n = CG.EventGetIntegerValueField(@event, CGEventField.MouseEventButtonNumber); MouseButton b = MouseButton.Left + n; CursorState[b] = false; } break; } return @event; }
private IntPtr EventProc(IntPtr proxy, CGEventType type, IntPtr @event, IntPtr refcon) { if (type == CGEventType.KeyDown) { var code = CG.EventGetIntegerValueField(@event, CGEventField.KeyboardEventKeycode); if (!Enum.IsDefined(typeof(MacOSKeyCode), code)) { Console.WriteLine("THIS KEY IS NOT DEFINED!!!"); return(@event); } var key = MacOSKeyMap.GetKey((MacOSKeyCode)code); OnKeyDown(key); return(KeyDownArgs.Handled ? IntPtr.Zero : @event); } if (type == CGEventType.KeyUp) { var code = CG.EventGetIntegerValueField(@event, CGEventField.KeyboardEventKeycode); if (!Enum.IsDefined(typeof(MacOSKeyCode), code)) { Console.WriteLine("THIS KEY IS NOT DEFINED!!!"); return(@event); } var key = MacOSKeyMap.GetKey((MacOSKeyCode)code); OnKeyUp(key); return(KeyUpArgs.Handled ? IntPtr.Zero : @event); } if (type == CGEventType.FlagsChanged) //this may be important for persistent effects. like a caps lock key press will return a keydown, and not immediately a keyup. This is likely relevant so we can track caps state. //will likely need to sync caps state across clients. How can we enable OSX LED light?? //TODO: caps lock sync and led { var code = CG.EventGetIntegerValueField(@event, CGEventField.KeyboardEventKeycode); if (!Enum.IsDefined(typeof(MacOSKeyCode), code)) { Console.WriteLine("THIS KEY IS NOT DEFINED!!!"); return(@event); } // Console.WriteLine(code); } if (type == CGEventType.MouseMoved) { //using the mouse point here messes us up because of the warp command and inability to cancel mouse //move events. //var pt = _getMousePoint(); var deltaX = CG.EventGetIntegerValueField(@event, CGEventField.MouseEventDeltaX); var deltaY = CG.EventGetIntegerValueField(@event, CGEventField.MouseEventDeltaY); var newX = (int)ClientState._lastPositionX + deltaX; var newY = (int)ClientState._lastPositionY + deltaY; //OSX won't let me handle these events, so I instead post an additional move the event tap. Ignore these if (_ignoreNextMovement) { _ignoreNextMovement = false; return(@event); } this.OnMouseMove(newX, newY); return(MouseMoveArgs.Handled ? IntPtr.Zero : @event); } if (type == CGEventType.LeftMouseDown) { this.OnMouseDown(MouseButton.Left); return(MouseDownArgs.Handled ? IntPtr.Zero : @event); } if (type == CGEventType.RightMouseDown) { this.OnMouseDown(MouseButton.Right); return(MouseDownArgs.Handled ? IntPtr.Zero : @event); } if (type == CGEventType.OtherMouseDown) { var btn = CG.EventGetIntegerValueField(@event, CGEventField.MouseEventButtonNumber); if (btn == 2) { this.OnMouseDown(MouseButton.Middle); return(MouseDownArgs.Handled ? IntPtr.Zero : @event); } if (btn == 3) { this.OnMouseDown(MouseButton.Button1); return(MouseDownArgs.Handled ? IntPtr.Zero : @event); } if (btn == 4) { this.OnMouseDown(MouseButton.Button2); return(MouseDownArgs.Handled ? IntPtr.Zero : @event); } return(@event); } if (type == CGEventType.LeftMouseUp) { this.OnMouseUp(MouseButton.Left); return(MouseUpArgs.Handled ? IntPtr.Zero : @event); } if (type == CGEventType.RightMouseUp) { this.OnMouseUp(MouseButton.Right); return(MouseUpArgs.Handled ? IntPtr.Zero : @event); } if (type == CGEventType.OtherMouseUp) { var btn = CG.EventGetIntegerValueField(@event, CGEventField.MouseEventButtonNumber); if (btn == 2) { this.OnMouseUp(MouseButton.Middle); return(MouseUpArgs.Handled ? IntPtr.Zero : @event); } if (btn == 3) { this.OnMouseUp(MouseButton.Button1); return(MouseUpArgs.Handled ? IntPtr.Zero : @event); } if (btn == 4) { this.OnMouseUp(MouseButton.Button2); return(MouseUpArgs.Handled ? IntPtr.Zero : @event); } return(@event); } if (type == CGEventType.ScrollWheel) { var wheelDeltaY = CG.EventGetIntegerValueField(@event, CGEventField.ScrollWheelEventDeltaAxis1); var wheelDeltaX = CG.EventGetIntegerValueField(@event, CGEventField.ScrollWheelEventDeltaAxis2); this.OnMouseWheel(wheelDeltaX, wheelDeltaY); return(MouseWheelArgs.Handled ? IntPtr.Zero : @event); } return(@event); }