Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an NSEvent of type NSScrollWheel?

I'd like to construct (fake) an NSEvent of type NSScrollWheel. There are NSEvent constructor methods for a few other types of NSEvents (mouseEvent, keyEvent, enterExitEvent, otherEvent), but none allows me to set, for example, deltaX.

like image 972
smokris Avatar asked May 25 '11 14:05

smokris


2 Answers

NSEvent doesn't provide a way to do this, but you can create a CGEvent and create an NSEvent wrapper around it. The event will automatically use the current location of the cursor. See CGEventCreateScrollWheelEvent. However, posting this event using NSApplication's postEvent:atStart: doesn't work (probably because it doesn't have a window defined). You can either post the CGEvent directly to the event stream, or send the NSEvent directly to a view's scrollWheel: method.

CGWheelCount wheelCount = 2; // 1 for Y-only, 2 for Y-X, 3 for Y-X-Z
int32_t xScroll = −1; // Negative for right
int32_t yScroll = −2; // Negative for down
CGEventRef cgEvent = CGEventCreateScrollWheelEvent(NULL, kCGScrollEventUnitLine, wheelCount, yScroll, xScroll);

// You can post the CGEvent to the event stream to have it automatically sent to the window under the cursor
CGEventPost(kCGHIDEventTap, cgEvent);

NSEvent *theEvent = [NSEvent eventWithCGEvent:cgEvent];
CFRelease(cgEvent);

// Or you can send the NSEvent directly to a view
[theView scrollWheel:theEvent];
like image 136
ughoavgfhw Avatar answered Sep 19 '22 14:09

ughoavgfhw


You can create a CGEventRef using CGEventCreateScrollWheelEvent and convert it to an NSEvent with eventWithCGEvent:. To add information to the CGEvent, use CGEventSetIntegerValueField -- among the possible fields are kCGScrollWheelEventDeltaAxis1, ...2, and ...3.

like image 38
jscs Avatar answered Sep 18 '22 14:09

jscs