Detecting mouse button state

Talk about add-ons and extension development.
Post Reply
User avatar
CAFxX
Posts: 278
Joined: May 8th, 2005, 9:59 am

Detecting mouse button state

Post by CAFxX »

I'd need to know if there's a method to be able to detect the state of the mouse buttons, outside of onmouseup/down.
Consider the following:
1) user clicks (and holds the button) somewhere in the document,
2) user moves the mouse pointer outside of the document
3) user releases button
4) user moves the mouse pointer back on the document
there seems to be no way to detect whenever 3) actually happened or not.
what I'd need is either
- knowing the button states from within onmousemove
- knowing the button states from within onmouseover (fired on 4))
- knowing the button states by means of some global variable
any suggestion about the above would be very much appreciated.
thanks, CAFxX
max1million
Posts: 2810
Joined: November 15th, 2004, 5:03 am

Post by max1million »

Not getting the mouseup cause the listener can only listen to what what come from inside the element it is placed on.

I know there is event.button but haven't tried it with those events. mousemove mouseover
telega
Posts: 609
Joined: October 20th, 2007, 3:02 am
Contact:

Post by telega »

Algorithm to detect 3):
a) on "mousedown" event set "var isDown = true;"
b) on "mouseup" event set "isDown = false;"
c) when you receive "mousedown" event, check: if isDown == true, then mouse button was released outside the document.
User avatar
CAFxX
Posts: 278
Joined: May 8th, 2005, 9:59 am

Post by CAFxX »

telega wrote:Algorithm to detect 3):
a) on "mousedown" event set "var isDown = true;"
b) on "mouseup" event set "isDown = false;"
c) when you receive "mousedown" event, check: if isDown == true, then mouse button was released outside the document.

I need to know if the button was released outside as soon as the cursor re-enters the document, not on the next mousedown...
User avatar
CAFxX
Posts: 278
Joined: May 8th, 2005, 9:59 am

Post by CAFxX »

max1million wrote:Not getting the mouseup cause the listener can only listen to what what come from inside the element it is placed on.

I know there is event.button but haven't tried it with those events. mousemove mouseover


Somehow it seems not the right way to do it:
Attributes
[...]
button of type unsigned short, readonly
During mouse events caused by the depression or release of a mouse button, button is used to indicate which mouse button changed state. The values for button range from zero to indicate the left button of the mouse, one to indicate the middle button if present, and two to indicate the right button. For mice configured for left handed use in which the button actions are reversed the values are instead read from right to left.
max1million
Posts: 2810
Joined: November 15th, 2004, 5:03 am

Post by max1million »

No matter I decided to try event.button keeps returning 0 with those events. mousemove mouseover.

Just a thought, but if you are referring to an html page you might check if you mouseout (then mouseover?) the html node that is in the page's document.defaultView.top.document (the top document of page or the browser element the document is in), between receiving mousedown and mouseup. Similar to what telega wrote with different event, and some checking to be sure it's the right node.

Note: this gBrowser.getBrowserForDocument(doc) should give the browser element where doc is the page's document
User avatar
CAFxX
Posts: 278
Joined: May 8th, 2005, 9:59 am

Post by CAFxX »

Sorry, I don't understand how that could allow me to know if the button was released outside the document as soon as the cursor re-enters the document (mouseover, mousemove) and not on the next click (mousedown, mouseup, mouseclick, etc.).

Note: "outside the document" means both "outside the document, but within the browser window" and "outside the browser window".
max1million
Posts: 2810
Joined: November 15th, 2004, 5:03 am

Post by max1million »

mousedown set variable to true, no mouseup to set it false before a mouseout of the html node (of document of top window of page), then when mouseover same html node read variable, if it's true, assume the mousedown happed with out a mouseup before a mouseout and reset the variable to false so it's ready for next time around.

If html assume leaving the html node is leaving the document because what is displayed is all in the html node. If some other (or even html) then if leaving the browser element the document is in (each page in a tab is in a browser element) mouse is no longer in the browser element or something within it (any descendent's). That is if I didn't miss something.

I checked mouseout and mouseover with event listeners on the browser's window (the outside window containing all toolbars tabs ect.) and don't remember seeing document or window for any nodeName but xul:browser, HTML (if an html page), xul:toolbarbutton, textbox, xul:toolbarbutton, BODY, xul:slider, xul:thumb, xul:toolbox, div were among them.
User avatar
CAFxX
Posts: 278
Joined: May 8th, 2005, 9:59 am

Post by CAFxX »

max1million wrote:then when mouseover same html node read variable, if it's true, assume the mousedown happed with out a mouseup before a mouseout and reset the variable to false so it's ready for next time around.

but I can't assume that the button was released, I just want to know if it was or not.

Code: Select all

var dragging = false;

function hMouseDown(e) {
  dragging = true;
}
function hMouseUp(e) {
  dragging = false;
}
function hMouseOver(e) {
  if (dragging) dragging = false; // why? I don't need this! beside, the if is pointless...
}


let's say it this way: think you're dragging an image around the window. what happens if the pointer exits the window while dragging? ideally I'd say that if, upon re-entering, the button is still down, the dragging should continue. if not, the dragging should stop. all of this is needed because sometimes the pointer exits the window even if the user does not want it (i.e. the user is not willing to drop the image/whatever somewhere outside the window, but was merely dragging it and the pointer exited the window)
ImageTweak - Zoom and display images against a custom background.
max1million
Posts: 2810
Joined: November 15th, 2004, 5:03 am

Post by max1million »

Have you checked nsDragAndDrop? Maybe you can find something there. mouseout/dragexit, and mouseover/dragenter/dragover I don't know off hand which would be first.
brody
Posts: 890
Joined: October 20th, 2003, 1:24 pm

Post by brody »

I haven't tried this myself but it looks like to me that you are going to need to catch all of the mouse events that you can by attaching your listeners to the top level chrome window. Then you will need to use logic routines to determine what is happening. Reminds me of writing mouse handler routines for MS-DOS applications where the only information available was movement and clicks through interrupt routines - you have to do all of the work.

When the user clicks the "mousedown" event fires.
Store the target element: var gMouseTarget = event.originalTarget

When the user releases the "mouseup" event fires.
Check the target element: if(gMouseTarget != event.originalTarget)

etc.,etc.

Looks like it will be a trial and error process.
User avatar
CAFxX
Posts: 278
Joined: May 8th, 2005, 9:59 am

Post by CAFxX »

I could do that (and partly I'm already doing it), but unless there's a way to ask the runtime some more informations other than that available through the standard event interface, it will all be pointless... I asked on #extdev but nobody knew how to do that. I asked on #developers but everybody was really too busy with their business. In short, I'm stuck.
The only possibility I see is that - maybe - buried somewhere in the mozilla source there's a forgotten/undocumented/hidden interface available to extensions that can do what I need. I still have to examine nsDragAndDrop, but I don't know if it may really help in my case.
ImageTweak - Zoom and display images against a custom background.
max1million
Posts: 2810
Joined: November 15th, 2004, 5:03 am

Post by max1million »

DragAndDrop seems to know where it is, document window desktop.

Hm that's odd, it don't drop the link on desktop, I must have missed something. Some flavor maybe.
Post Reply