static CGEventRef callback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) { if(CGEventGetIntegerValueField(event, kCGScrollWheelEventIsContinuous)) return event; CGEventSetDoubleValueField(event, kCGScrollWheelEventFixedPtDeltaAxis1, -CGEventGetDoubleValueField(event, kCGScrollWheelEventFixedPtDeltaAxis1)); CGEventSetDoubleValueField(event, kCGScrollWheelEventFixedPtDeltaAxis2, -CGEventGetDoubleValueField(event, kCGScrollWheelEventFixedPtDeltaAxis2)); return event; }
// Return true if the event is handled, return false otherwise and if the event should not be interrupted bool MouseEventTool::HandleMouseEvent(CGEventType type, CGEventRef event) { // Check our full screen state so we can hackily reset it if it gets messed up if (shouldMakeFullScreenOrNot_) { if (IsMenuBarVisible()) { ::SetSystemUIMode(kUIModeAllHidden, 0); } } if (type == kCGEventKeyUp || type == kCGEventKeyDown) { // Don't do anything to key events return false; } CGPoint mouseLocation = CGEventGetLocation(event); double scrollMotion = CGEventGetDoubleValueField(event, kCGScrollWheelEventDeltaAxis1); if (mouseEventCallback_ != NULL) { int dx = 0; int dy = 0; if (prevX_ >= 0) { dx = mouseLocation.x - prevX_; dy = mouseLocation.y - prevY_; } mouseEventCallback_->MouseEvent(macEventToWindowsEventType(type), mouseLocation.x, mouseLocation.y, dx, dy, scrollMotion); } prevX_ = mouseLocation.x; prevY_ = mouseLocation.y; int eventTypeEnum = macEventToEnum(type); if (eventTypeEnum == -1) { return false; } isFullScreen_ = isFullScreenWindow(); short resultKey = buttonMapping_[eventTypeEnum]; if (resultKey != -1) { if (resultKey != VK_NO_EVENT) { CGEventRef eventDown, eventUp; CGEventRef commandDown, commandUp; CGEventSourceRef eventSource = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState); CGKeyCode keyToSend = resultKey; if (resultKey == VK_BROWSER_BACK && isFullScreen_) { keyToSend = kVK_Escape; } else if (resultKey == VK_BROWSER_BACK) { commandDown = CGEventCreateKeyboardEvent(eventSource, kVK_Command, true); keyToSend = kVK_ANSI_LeftBracket; CGEventPost(kCGHIDEventTap, commandDown); } eventDown = CGEventCreateKeyboardEvent (eventSource, keyToSend, true); eventUp = CGEventCreateKeyboardEvent (eventSource, keyToSend, false); CGEventPost(kCGHIDEventTap, eventDown); CGEventPost(kCGHIDEventTap, eventUp); if (resultKey == VK_BROWSER_BACK && !isFullScreen_) { commandUp = CGEventCreateKeyboardEvent(eventSource, kVK_Command, false); CGEventPost(kCGHIDEventTap, commandUp); CFRelease(commandDown); CFRelease(commandUp); } CFRelease(eventDown); CFRelease(eventUp); CFRelease(eventSource); } return true; } return false; }