• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

mousePressed Problem

 
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a code like this one



Here i have to detect mousePress (i.e.,) Mouse Button DOWN event.When i try to hold down mouse button i can see that it executes mousePressed() method but it gets executed only once eventhough i am holding down that mouse button without releasing it.

Is there a way i can know that a mouse button has been down for some duration.(Please note that i am not talking about mouseDragged Event).

Any help guys?
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> but it gets executed only once eventhough i am holding down that mouse
> button without releasing it.

how many times have you pressed it?

ever thought about mousePressed starting a timer to do what you want,
and mouseReleased to stop the timer.
 
Balasubramanian Chandrasekaran
Ranch Hand
Posts: 215
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Michael Dunn:

how many times have you pressed it?
........



I did it only once and i will just HOLD DOWN.
In case of KeyEvent if i HOLD DOWN a key it gets detected bt keyPress() method whereas in case of mouse this operation is not working.
And also i want to do autodetection instead of using Timer's.
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean by 'autodetection'?

Michael's solution works well. If you know what time the mouse was pressed (which you do), and that it hasn't been released yet (because there hasn't been a mouseReleased action), then surely you know how long it's been down for?
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Balasubramanian Chandrasekaran:

Here i have to detect mousePress (i.e.,) Mouse Button DOWN event.When i try to hold down mouse button i can see that it executes mousePressed() method but it gets executed only once eventhough i am holding down that mouse button without releasing it.



I dont get this. Why do you expect the mousePressed multiple times when you have actually pressed it only once? Remember 1 mouse click = 1 mouse press + 1 mouse release.

If you are interested the duration for which the mouse button is kept pressed, the MouseEvent has a getWhen() method which returns a long which represents the time stamp when the mouse pressed occoured. So you just compare it with your current time stamp and process it accordingly.
 
Balasubramanian Chandrasekaran
Ranch Hand
Posts: 215
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by David Payne:
What do you mean by 'autodetection'?

Michael's solution works well. If you know what time the mouse was pressed (which you do), and that it hasn't been released yet (because there hasn't been a mouseReleased action), then surely you know how long it's been down for?



Here what i need to do is, just to detect that mouse press event continuously.
I don't want that timer functionality.

I am going in detail about my query to make it understandable

Consider this piece of code


Here if i hold a key DOWN for some time it invokes keyPressed( ) method continuously. (i.e.,)It prints Key Press Event until i hold a key DOWN.

Likewise i want to do the same operation in case of mouse buttons.

In this code, whenever i hold a mouse button DOWN it must print Mouse Pressed continuously until i release that button.

But what actually happens is if i hold DOWN a button it prints Mouse Pressed only once.
Why is it not working like that of the keyboard event.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I ought to know better than to butt in, but three people have already told you that mousePressed() is working correctly.
If you hold a key down, the OS usually repeats the keystroke, so it would register several times. If you want repeated printouts when you hold the mouse button down, put a loop in whatever you call. If you want some sort of record where the mouse goes while you hold it down, find the mouseDragged() method of MouseMotionListener.
 
Mark Newton
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess the answer is because a keyboard key works like that, and a mouse doesn't. When you're typing, if you hold down a letter, you get that letter over and over again, like:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

That doesn't happen with a mouse click, if you hold the mouse down once, you only get one (half) click - if it kept clicking repeatedly, you'd end up double-clicking all over the place.

Again - why is it that you need this functionality?

If you just want something like the above, that is, the method to loop constantly until you release the mouse, then what about something like:

Please note that I haven't tried this, and I would imagine that it won't work - I suspect that you will just enter an infinite loop, because the while loop is monopolising the thread. I would guess that you need to put your System.out.println("Mouse pressed") in a new Thread and start it from the mousePressed() method.

[Edit: Sorry Campbell - done it again ]
[ January 17, 2008: Message edited by: David Payne ]
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's nothing to apologise about, David Payne.
 
Balasubramanian Chandrasekaran
Ranch Hand
Posts: 215
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by David Payne:

.......... if you hold the mouse down once, you only get one (half) click - if it kept clicking repeatedly, you'd end up double-clicking all over the place............

Again - why is it that you need this functionality?

......



I have a doubt here Double click is two successive mouse press and release (i.e.,) Mouse press+Mouse release and then again followed by another Mouse press+Mouse release immediately.

Whereas here i am just holding on the mouse button without releasing it.

Anyway,i will try to implement that while loop concept which David Payne stated.

And why i need this functionality is that i am implementing a remote system management application which has to detect a mouse event in client and send that event to server.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Balasubramanian Chandrasekaran:
And why i need this functionality is that i am implementing a remote system management application which has to detect a mouse event in client and send that event to server.

I think you have already achieved that in this little example.
 
reply
    Bookmark Topic Watch Topic
  • New Topic