Show mouse position after click (objective-c) - objective-c

I search how to create an application which show the cursor position after click. I find how to reports the current mouse position, but I do not know how show tis in my windows!
I want simply click on a position and show this position on windows label
on Xcode documentation I find - (void)mouseUp:(NSEvent *)theEvent But i dont know how use this!

Subclass your NSWindow and then override the mouseDown Event Method
- (void)mouseDown:(NSEvent *)theEvent
{
//get the point position where the mouse was clicked on the NSWindow !
NSPoint event_location = [theEvent locationInWindow];
NSLog(#"Clicked %f %f",event_location.x,event_location.y);
// use event_location.x and event.location.y to show the position whereever you like
}

Related

Mac Cocoa How to scroll another App that under a view?

I'm trying to make a screenshot capture App. The App will first place a semitransparent full screen dark view over other Apps, this will make other Apps not responding to mouse scrolling event. How can I set a view covers other Apps and keep them respond to scrolling event? Thanks.
Ignore MyApp's dark view?
or
Send event to other Apps in MyApp?
-(void)scrollWheel:(NSEvent *)theEvent {
NSLog(#"user scrolled %f horizontally and %f vertically", [theEvent deltaX], [theEvent deltaY]);
}
Edited:
Found a way to get a App's pid and Followed #Willeke's reference, write the following code, but not work.
-(void)scrollWheel:(NSEvent *)theEvent {
[super scrollWheel:theEvent];
CGEventRef wheelevent = CGEventCreateScrollWheelEvent(NULL, kCGScrollEventUnitLine, 1, theEvent.deltaY, theEvent.deltaX);
// AppPid is gotten before mouse wheel scrolls
CGEventPostToPid(AppPid, wheelevent);
CFRelease(wheelevent);
}

Get mouse location from NSScrollView

I have added my NSScrollView over the content view of my NSWindow object. Now I need to know the mouse location over the scrollview.
I have tried the following. But nothing gives the correct location.
- (void)mouseDown:(NSEvent *)theEvent{
NSPoint eventLocation = [theEvent locationInWindow];
NSPoint locationInScroll = [inputScrollView convertPoint:eventLocation toView:nil];
//Both gives the wrong location.
}
The code is correct, assuming that inputScrollView is the document view and the NSScrollView itself.
Another potential problem could be if you change the orientation of the coordinate system in one of the views?

How to differentiate the mouseDown event from mouseDragged in a Transparent NSWindow

I have a Transparent NSWindow with an simple icon in it that can be dragged around the screen.
My code is:
.h:
#interface CustomView : NSWindow{
}
#property (assign) NSPoint initialLocation;
.m
#synthesize initialLocation;
- (id) initWithContentRect: (NSRect) contentRect
styleMask: (NSUInteger) aStyle
backing: (NSBackingStoreType) bufferingType
defer: (BOOL) flag{
if (![super initWithContentRect: contentRect
styleMask: NSBorderlessWindowMask
backing: bufferingType
defer: flag]) return nil;
[self setBackgroundColor: [NSColor clearColor]];
[self setOpaque:NO];
[NSApp activateIgnoringOtherApps:YES];
return self;
}
- (void)mouseDragged:(NSEvent *)theEvent {
NSRect screenVisibleFrame = [[NSScreen mainScreen] visibleFrame];
NSRect windowFrame = [self frame];
NSPoint newOrigin = windowFrame.origin;
// Get the mouse location in window coordinates.
NSPoint currentLocation = [theEvent locationInWindow];
// Update the origin with the difference between the new mouse location and the old mouse location.
newOrigin.x += (currentLocation.x - initialLocation.x);
newOrigin.y += (currentLocation.y - initialLocation.y);
// Don't let window get dragged up under the menu bar
if ((newOrigin.y + windowFrame.size.height) > (screenVisibleFrame.origin.y + screenVisibleFrame.size.height)) {
newOrigin.y = screenVisibleFrame.origin.y + (screenVisibleFrame.size.height - windowFrame.size.height);
}
// Move the window to the new location
[self setFrameOrigin:newOrigin];
}
- (void)mouseDown:(NSEvent *)theEvent {
// Get the mouse location in window coordinates.
self.initialLocation = [theEvent locationInWindow];
}
I want to display a NSPopover when the users clicks the image of the transparent window. But, as you can see in the code, the mouseDown event is used to get the mouse location (the code above was taken from an example).
What can i do to know when the user clicks the icon just to drag it around or simply clicked it to display the NSPopover?
Thank you
This is the classic situation of receiving the defining event after you need it in order to begin the action. Specifically, you can't know if the mouseDown is the beginning of a drag until after the drag starts. However, you want to act upon that mouseDown if a drag doesn't start.
In iOS (I realize that's not directly relevant to the code here, but it is instructional), there's an entire API built around letting iOS attempt to make these kinds of decisions for you. The entire Gesture system is based on the idea that the user starts to do something that might be one of many different actions, and thus needs to be resolved over time, possibly resulting in cancelled actions during the tracking period.
On OS X, we don't have many systems to help out with this, so if you have something that needs to handle a click and a drag differentially, you will need to defer your next action until a guard time has passed, and if that passes, you can perform the original action. In this case, you will likely want to do the following:
In the mouseDown, begin an NSTimer set for an appropriate guard time (not so long that people will accidentally move the pointer, and not so short that you'll trigger before the user drags) in order to call you back later to trigger the popover.
In the mouseDragged, use a guard area to make sure that if the user just twitches a little, it doesn't count as a drag. This can be irritating, as it sometimes results in needing to drag something farther than it seems necessary in order to begin a drag, so you'll want to either find a magic constant somewhere, or do some experimentation. When the guard area is exceeded, then begin your legitimate drag operation by canceling the NSTimer with [timer invalidate] and do your drag.
In the callback for the timer, display your popover. If the user dragged, the NSTimer will have been invalidated, causing it not to fire, and so the popover won't be displayed.

Resizing the NSTableView programmatically

How to implement the feature of resizing NSTableView created programmatically ? Interface builder should not be used. It should be like click and drag to change the size of the NSTableView. Is it possible? If yes, please help. . . .
I am afraid you need to write a bit of code to make it working. This is how I would do it.
Make a special Resize View that will track the mouse events and call delegate methods providing how the tracking position changes.
- (void)mouseDown:(NSEvent *)theEvent
{
_startPoint = [theEvent locationInWindow];
[_delegate resizingDidStart];
}
- (void)mouseDragged:(NSEvent *)theEvent
{
NSPoint hitPoint = [theEvent locationInWindow];
[_delegate resizeWithDeltaX:(hitPoint.x - _startPoint.x) deltaY:(hitPoint.y - _startPoint.y)];
}
Put this view in the right bottom corner of the Base View. Set the autoresizing mask so this view always stays in the right bottom corner.
Put the table view along with its scroll view on to the Base View. Set autoresizing mask of the scroll view so its size and width are sizeable.
In the delegate of the Resize View process changes in the mouse position and set the frame of the Base View.
- (void)resizingDidStart
{
_initialRect = [_baseView frame];
}
- (void)resizeWithDeltaX:(CGFloat)deltaX deltaY:(CGFloat)deltaY
{
[_baseView setFrame:NSMakeRect(_initialRect.origin.x, _initialRect.origin.y + deltaY, _initialRect.size.width + deltaX, _initialRect.size.height - deltaY)];
}
Of course the scroll view should be under the resize view. You can draw some ind of a handle on the resize view, etc.

How to find the location of the mouse in objective-c

I am making an image editor (just a simple editor for a program I am making), and I need to find the position of the mouse. Is it possible to do this in Objective-C? If so, how?
EDIT: I just thought I should mention that I have done some research on this and I haven't found anything that works. The code I have in my header file is as follows:
#import <Cocoa/Cocoa.h>
#interface test : NSWindow <NSWindowDelegate> {
}
#end
I can handle any outlets and actions that are needed; I just need to know how to find the position of the mouse.
If you are catching it through an event, such as mouseDown, it will look like this:
- (void)mouseDown:(NSEvent *)theEvent {
NSPoint mouseDownPos = [theEvent locationInWindow];
}
Otherwise, use:
[NSEvent mouseLocation];
EDIT: (Sorry, I wrote NSPoint *, which is wrong, since it's a struct)
Inside a mouse event handler (mouseDown:, mouseUp:, mouseMoved:, etc.), you can ask the event for its locationInWindow. If you need the mouse location at some arbitrary time (generally you won't want to do that, since it's rare for a program to have a one-off need to discover the mouse location), you can do [NSEvent mouseLocation] and it will return the mouse's location at the time in screen coordinates.
If you want the coordinates from the origin of the view itself, use:
NSPoint locationInView = [self convertPoint:[theEvent locationInWindow] fromView:nil];
You can use this NSPoint directly to draw in the NSView coordinates; otherwise with
[theEvent locationInWindow]
you will have the coordinates of the mouse in the window, which is probably not what you want.