|
|
mouseUp not called
|
|
|
|
Grizzled Veteran
Join Date: Jun 2002
Status:
Offline
|
|
Hi guys,
So I have a NSView, and basically I'm trying to replicate NSPopUpButton, but I decided to create my own view due to it being more flexible. Basically, when I click down on my view (mouseDown:) I do:
Code:
- (void)mouseDown:(NSEvent *)event
{
[self setPressed:YES];
[self setNeedsDisplay:YES];
[self performSelector:@selector(menuShouldOpen)];
}
This works fine, and the view is highlighted. So, on mouseUp, I don't want it to be highlighted, so I do:
Code:
- (void)mouseUp:(NSEvent *)event
{
[self setPressed:NO];
[self setNeedsDisplay:YES];
}
Now, in certain cases, this works fine. For example, if I click on the view, then click again, mouseUp is called and the code is executed. Now, if I were to click and hold my mouse down on the view, and let go, mouseUp is not called for some reason. So my question is, which event should I use to capture this?
Thanks
|
|
|
|
|
|
|
|
|
Junior Member
Join Date: Jun 2002
Location: Springfield
Status:
Offline
|
|
I had a problem like this a while ago, too - but I used some sample code from Apple's documentation to find a way around it:
Instead of calling [self setPressed:NO], etc. in mouseUp:, do it in mouseDown:, like this (based on code from Apple's Cocoa documentation at http://developer.apple.com/documenta...useEvents.html:
- (void)mouseDown:(NSEvent *)theEvent
{
BOOL keepOn = YES;
BOOL isInside = YES;
NSPoint mouseLoc;
while (keepOn) {
theEvent = [[self window] nextEventMatchingMask: NSLeftMouseUpMask];
mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil];
isInside = [self mouse:mouseLoc inRect:[self bounds]];
switch ([theEvent type]) {
case NSLeftMouseUp:
if (isInside) // (ie, if the mouseUp occurred inside the view)
{
/*** You're mouseUp: code HERE***/
[self setPressed:NO];
[self setNeedsDisplay:YES];
}
[self highlight:NO];
keepOn = NO;
break;
default:
/* Ignore any other kind of event. */
break;
}
};
return;
}
So, basically, this code enters a loop while the mouse is down and waits for the mouse to be released. Once released, your code will be called. So, you're mouseUp: method does nothing now, it's all taken care of in mouseDown:
Hope that helps.
|
|
|
|
|
|
|
|
|
Grizzled Veteran
Join Date: Jun 2002
Status:
Offline
|
|
Hi,
Thanks for the code, but its still working the same as before, as in, not .
This is the current code:
- (void)mouseDown NSEvent *)theEvent
{
BOOL keepOn = YES;
BOOL isInside = YES;
NSPoint mouseLoc;
[self setPressed:YES];
[self setNeedsDisplay:YES];
[self performSelector:@selector(menuShouldOpen)];
while (keepOn) {
theEvent = [[self window] nextEventMatchingMask: NSLeftMouseUpMask];
mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil];
isInside = [self mouse:mouseLoc inRect:[self bounds]];
switch ([theEvent type]) {
case NSLeftMouseUp:
if (isInside) // (ie, if the mouseUp occurred inside the view)
{
/*** You're mouseUp: code HERE***/
[self setPressed:NO];
[self setNeedsDisplay:YES];
NSLog(@"U!");
}
//[self setPressed:NO];
//[self setNeedsDisplay:YES];
keepOn = NO;
break;
default:
/* Ignore any other kind of event. */
break;
}
};
return;
}
It seems the same problem as before, mouseUp just isn't called. I am just wondering however if my menuShouldOpen method is to blame:
Code:
- (void)menuShouldOpen
{
NSPoint viewLocation = [self frame].origin;
viewLocation.y = viewLocation.y + 12;
[NSMenu popUpContextMenu:objectMenu withEvent:[NSEvent mouseEventWithType:NSLeftMouseDown location:viewLocation modifierFlags:0 timestamp:1 windowNumber:[[NSApp mainWindow] windowNumber] context:[NSGraphicsContext currentContext] eventNumber:1 clickCount:1 pressure:0.0] forView:self];
}
I can't see any problems code wise, but theory wise, maybe the event for the menu is killing the event for the mouseUp.
Thanks again
|
|
|
|
|
|
|
|
|
Grizzled Veteran
Join Date: Jun 2002
Status:
Offline
|
|
Turns out it is the menu, if I get rid of the menu thing it works correctly. The question is :-), what is wrong with the code that opens the menu?
Thanks
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|