Re: Mouse click vs. mouse held down
Re: Mouse click vs. mouse held down
- Subject: Re: Mouse click vs. mouse held down
- From: Ken Tabb <email@hidden>
- Date: Tue, 8 Jun 2004 16:18:50 +0100
Thanks for that Cricket, yes works like a charm, with the proviso being
that all of the clever functionality is within the mouseUp method (as
it's the mouseUp that determines whether it was a single click, or
click and hold).
So for those who need to do the same, here is my code. Hope it helps,
Ken
//NB the NSTimer is a class variable so declared in the .h
//the popup menu appears after 1.0 seconds of holding the mouse down -
//provide a different time interval to the NSTimer (in the mouseDown
codeif you need it longer / shorter
- (void)mouseDown:(NSEvent *)theEvent
{
popupMenuTimer = [[NSTimer scheduledTimerWithTimeInterval:1.0
target:self selector:@selector(showMyPopupMenu) userInfo:nil
repeats:NO] retain];
//the 1.0 shows the popup after 1 second of the mouse being pressed
down
}
- (void)mouseUp:(NSEvent *)theEvent
{
if(popupMenuTimer!=nil && [popupMenuTimer isValid]==YES)
{
[popupMenuTimer invalidate];
[popupMenuTimer release];
//do code for single click mouse actions here
}
else
{
//do code after showing the popup (i.e. after a mouse click &
hold is released) here
}
}
- (void)showMyPopupMenu
{
//do something cool and funky here.
//This method gets called by the NSTimer when the mouse has been
held down
//for (in code above) 1.0 seconds
}
On 7 Jun 2004, at 5:02 pm, cricket wrote:
An easy way to do this (IMHO) is to create an NSTimer in mouseDown:
that fires after the 'hold' interval. In mouseUp: you can invalidate
and release the timer. In the method that the timer calls, pop up the
menu.
- cricket
On Jun 7, 2004, at 8:17 AM, Ken Tabb wrote:
Hi folks,
how does one discriminate between a single click (i.e. click mouse
down, then let it go) and a click-and-hold (i.e. click mouse down and
keep it there, kind of like a mouse drag but without necessarily
moving it around the screen)? I'm only needing left button
functionality but cannot see a way of telling when the mouse is being
held down vs. when it is clicked and released.
In case you're wondering, I want to make a button kind of like
Xcode's toolbar buttons, where clicking on them (and letting go) does
something, whereas clicking on them and holding brings up a popup
menu for the button. Making the menu etc. is fine, but discriminating
between types of mouse click is driving me crazy.
NSEvent's clickCount: method returns 1 whether the mouse is still
being held down or not. Unless I double click it of course in which
case clickCount: returns 2 on the 2nd click (but 1 between clicks).
mouseDown: works as expected, but it doesn't tell me whether the
mouse is _still_ down or not. And mouseDragged: only works if the
mouse moves while down (which is not guaranteed to be the case, as
the user won't be dragging stuff from/to it).
I'm guessing I could perform some housekeeping in terms of "did the
mouse go down, if so has there been a mouseUp event since?" etc. but
this seems terribly inefficient (you haven't seen the way I code) and
must be a semi-regular thing that people want to do? So I'm hoping it
doesn't have to come to this.
I know I could use control click, but that seems ugly for those of us
with one button mice. Or of course I could put "2 button mouse" as
one of the system requirements!
If I'm barking up the wrong tree, please feel free to mock me in this
public forum, but please also include the 'obvious' way of doing it
8^)
Thanks in advance for any light you can shed,
Ken
- - - - - - - - - -
Dr. Ken Tabb
Mac & UNIX Technical bloke (C, C++, Obj-C, Java) - Health & Human
Sciences
Machine Vision & Neural Network researcher bloke - Computer Science
Dept
University of Hertfordshire, UK
http://www.health.herts.ac.uk/ken/
Certified non-Microsoft Solution Provider
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
---------->
We salute the lone knife maker of today. He or she is a living
repository for a knowledge and tradition that spans millennia.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
- - - - - - - - - -
Dr. Ken Tabb
Mac & UNIX Technical bloke (C, C++, Obj-C, Java) - Health & Human
Sciences
Machine Vision & Neural Network researcher bloke - Computer Science Dept
University of Hertfordshire, UK
http://www.health.herts.ac.uk/ken/
Certified non-Microsoft Solution Provider
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.