Simulate Click and Drag in OS X - objective-c

I am trying to simulate mouse action via programming.
I am not able to do click and drag action reliably.
For click and drag I am doing the following actions in the order mentioned below
Post left mouse button pressed event at current mouse pointer position (only once)
Move the mouse (as mouse down event is already sent, it must act as mouse down and drag, selecting text from the initial mouse postion)
Post left mouse button release event to end mouse hold action.
In apple applications such as Xcode, or Safari, this method does not work reliably (It docent highlight text when dragged, but with actual mouse I can highlight text by click and drag). Cannot drag icons either.
However in Firefox, this technique works. The selection follows mouse! That is as we move mouse pointer, the selection also changes accordingly, which is what I need.
The following is the code I use to send mouse tap events.
-(void)_sendMouseEvent:(int)mouseEvent withMouseButton:(CGMouseButton)mouseBtn
{
CGEventRef ourEvent = CGEventCreate(NULL);
NSPoint mouseLoc = CGEventGetLocation(ourEvent); //get current mouse position
CGEventRef mouseClick = CGEventCreateMouseEvent(
NULL,
mouseEvent,
mouseLoc,
mouseBtn
);
CGEventPost(kCGHIDEventTap, mouseClick);
CFRelease(ourEvent);
CFRelease(mouseClick);
}
I did try sending mouse down events at regular intervals, but it made no change.
Do you have any ideas on what I am doing wrong? Please suggest any workarounds that come to your mind.

I found out that we need to send mouse event kCGEventLeftMouseDragged first, to start dragging and selecting.
[self _sendMouseEvent:kCGEventLeftMouseDragged withMouseButton:kCGMouseButtonLeft]; //Start draging.
-(void)_sendMouseEvent:(int)mouseEvent withMouseButton:(CGMouseButton)mouseBtn
{
CGEventRef ourEvent = CGEventCreate(NULL);
NSPoint mouseLoc = CGEventGetLocation(ourEvent); //get current mouse position
CGEventRef mouseClick = CGEventCreateMouseEvent(
NULL,
mouseEvent,
mouseLoc,
mouseBtn
);
CGEventPost(kCGHIDEventTap, mouseClick);
CFRelease(ourEvent);
CFRelease(mouseClick);
}

Related

Capture other mouse button events on OS X with NSEvent

I'm trying to capture mouse clicks on OS X Yosemite.
From what I've read this code should work:
[NSEvent addGlobalMonitorForEventsMatchingMask:NSOtherMouseDown
handler:^(NSEvent *event) {
NSLog(#"Mouse Down, Button: %ld", event.buttonNumber);
}];
However, it only works for events on the right mouse button, not other buttons (i.e. buttons 4, 5, 6, et cetera on multi-button mice).
Using Quartz Event Taps I'm able to detect mouse clicks on these buttons, like so:
CGEventTapCreate(kCGSessionEventTap,
kCGHeadInsertEventTap,
0,
CGEventMaskBit(kCGEventOtherMouseUp) | CGEventMaskBit(kCGEventOtherMouseDown),
MouseEventCallback,
NULL);
But I'd prefer to use the NSEvent API as it is more straightforward.
Any ideas on how to capture other mouse button events with NSEvent?
It's possibly just a typo in your question ?, but your code is missing the NSEventMaskFromType, e.g. :
[NSEvent addGlobalMonitorForEventsMatchingMask:NSEventMaskFromType(NSOtherMouseDown) ..
^^^^^^^^^^^^^^^^^^^

How can I detect if mouse is down outside of window in cocoa?

As the titles says i wonder if it is possible to detect if the mouse button is down. I tried putting this code in my app delegate.m but with no success.
- (void)mouseDown:(NSEvent *)theEvent
{
NSLog(#"hello world!");
}
A quick google search showed me that this method only works inside of NSWindows. However, there most be some way to detect if mouse position is pressed, and if so; how can i do it?
You can use NSEvent addGlobalMonitorForEventsMatchingMask:
define in your control:
id mouseEventMonitor;
-(id)init{
mouseEventMonitor = [NSEvent addGlobalMonitorForEventsMatchingMask:(NSLeftMouseDownMask | NSRightMouseDownMask | NSOtherMouseDownMask)
handler:^(NSEvent *event){
NSLog(#"theEvent->%#",event);
//here you will receive the all mouse DOWN events
if (event.modifierFlags & NSCommandKeyMask)
{
NSLog(#"theEvent1->%#",event);
}else{
NSLog(#"theEvent2->%#",event);
}
}];
return self;
}
Cocoa Event Monitors are the way to go.
You can use them to track pretty much every kind of mouse event outside of your view hierarchy.
Check out the documentation in the Cocoa Event Handling Guide for information on how to use them and similar topics here on SO like
In what way a view or a window could know that mouseDown outside itself in Xcode?
How do i detect keystrokes using objective c?
Although you mention mouseDown in your question and have accepted an answer that describes how to set up a monitor, as far as I can tell from the rest of the question, you don't actually need an event that fires when the mouse button is pressed, but instead just want to check if a mouse button is currently pressed.
NSEvent has a class property you can use for this:
[NSEvent pressedMouseButtons]
This returns the indices of the currently pressed mouse buttons:
A return value of 1 << 0 corresponds to the left mouse button, 1 << 1 corresponds to the right mouse button, 1<< n, n >=2 correspond to other mouse buttons.

Cocoa: Middle mouse click event (global) + gesture API?

Are there any resources that might guide me in how to make my app respond to any type of mouse click that is not the left / right button? Globally, even when my app is not active.
And for magic mouse / trackpad, are there any frameworks or resources available to easily attach my code to a specific gesture?
You can find everything related to mouse events:
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/EventOverview/HandlingMouseEvents/HandlingMouseEvents.html
and trackpad events:
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/EventOverview/HandlingTouchEvents/HandlingTouchEvents.html#//apple_ref/doc/uid/10000060i-CH13-SW10
Here is a stack overflow link that has explanation for handling global events.
Here's a code example based on Shashank's (very helpful) answer.
NSEventMask eventMask = NSOtherMouseDownMask|NSOtherMouseUpMask;
[NSEvent addGlobalMonitorForEventsMatchingMask:eventMask
handler:^(NSEvent *event) {
if (event.type == NSOtherMouseDown) {
NSLog(#"middle click down");
} else if (event.type == NSOtherMouseUp) {
NSLog(#"middle click up");
}
}];

Limiting mouse to one display on Mac (Potentially using Cocoa)

I've been feverishly searching for a method by which to limit the user's mouse to one display in a multi-display setup on a Mac.
I've stumbled upon this question: Cocoa: Limit mouse to screen, I promise I have not duplicated this question.
The question did, however, spark an idea in my mind that it might be possible to write a simple application using Cocoa to restrict the mouse to one screen, run this application in the background, and still use my game which has been developed in AS3/Adobe AIR/Flash.
The game is a full-screen game, and will always be at the same resolution on the same monitor. The other monitor will also always have the same resolution, but is to be just a non-interactive display of information. I need the user to be able to interact with the game, but not accidentally move the mouse off of the game's screen.
Question Summary:
Can I create a basic application for Mac OS X (Lion) using Cocoa/Objective C that will restrict the mouse to one monitor that can be run IN THE BACKGROUND and prevent users from moving the mouse outside of the monitor that will have a full-screen game running on it?
[EDIT:]
I found the basic code necessary to run a loop for the Quartz Event Filter, courtesy of this answer: Modify NSEvent to send a different key than the one that was pressed
I am going to use that code for testing purposes, and I have modified it a bit to detect mouse events like so:
CGEventRef mouse_filter(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) {
NSPoint point = CGEventGetLocation(event);
NSPoint target = NSMakePoint(100,100);
if (point.x >= 500){
CGEventSetLocation(event,target);
}
return event;
}
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
CFRunLoopSourceRef runLoopSource;
CFMachPortRef eventTap = CGEventTapCreate(kCGHIDEventTap, kCGHeadInsertEventTap, kCGEventTapOptionDefault, kCGEventMouseMoved, mouse_filter, NULL);
if (!eventTap) {
NSLog(#"Couldn't create event tap!");
exit(1);
}
runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes);
CGEventTapEnable(eventTap, true);
CFRunLoopRun();
CFRelease(eventTap);
CFRelease(runLoopSource);
[pool release];
exit(0);
}
This, however, doesn't seem to work quite right. It is certainly detecting mouse events properly, and it moves the events properly as well. For instance, if I drag a window past 500 pixels from the left of the screen, the window drag event gets moved to 100,100. However, the mouse itself doesn't get moved to the new location when it is simply being moved around the screen, performing no other actions. IE: You can still move the mouse all around the screen, instead of just on the left 500 pixel column.
Any ideas?
Use a Quartz Event Tap to filter mouse moved and mouse dragged events. In the tap callback, use CGEventSetLocation to modify the location of mouse events that would otherwise move off the main screen.
You may need to run the program as root, or have assistive device access enabled in System Preferences.
Quartz Event Services Reference

Making a Cocoa Button look like is pressed, programmatically

I have some key events, that correspond to buttons also. What property/method to I have to set/call for a button to look depressed (change state?) for say half a second?
The way I solved this is I set the NSButton to a type of 'Push On Push Off' and then used the following code in my key event handler:
NSButton *button = [self.superview viewWithTag:event.keyCode];
if (button != nil && button.state == NSOffState) {
[button performClick:event];
[button performSelector:#selector(performClick:) withObject:event afterDelay:0.5];
}
This will highlight the button as if the user had clicked on it, and then it will click on it again in half a second.
I believe the button cell's -setHighlighted: method controls whether the button looks pressed or not. You may also need to call -setNeedsDisplay: on the button after changing it, and it's possible that the button will change its cell's highlighted state by itself, so I'm afraid you may need to fiddle around to get this working.
(I have to admit, though, that I'm not entirely certain about any of this.)