Jump to content

Proactive Mouse Event Trapping


Guest Sundown
 Share

Recommended Posts

All -

Longtime user of AutoIT here, however I feel as if I'm getting ready to ask a rather newbish question, so apologies in advance (it's even my first post here, yikes!).

The vast majority of my experience with AutoIT was related to file shares, login scripts, etc. Now I need to trap all mouse click events, with the potential of either redirecting them or otherwise disguarding them. In one instance, I need to trap for a single mouse click, and replace it with a double mouse click.

Using the help, it's easy to locate function to PERFORM these actions, however I cannot find a way to "trap" the mouse events so that I can evaulate if it was a left mouse, right mouse click, etc.

Can someone point me in the right direction? I even downloaded the beta in hopes of _mousetrap was what I was looking for, but no such luck ;)

Thanks for your help.

Link to comment
Share on other sites

  • Moderators

The newest Beta has a UDF called _IsPressed(), try looking that up in the search and get the details. Also, I'm sure they've added it to the help file. This may be just what your looking for. I also assume you've used Larry's and Gary's MouseTrap() to trap it, nice little UDF also.

Take Care / Good Luck

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Ron -

Thanks, that gives me what I'm looking for. That, coupled with some additional research should give me what I'm looking for. Thanks for the help!

Link to comment
Share on other sites

  • 4 months later...

The newest Beta has a UDF called _IsPressed(), try looking that up in the search and get the details. Also, I'm sure they've added it to the help file. This may be just what your looking for. I also assume you've used Larry's and Gary's MouseTrap() to trap it, nice little UDF also.

Take Care / Good Luck

How would you use _IsPressed to "trap" the mouseclick only, not the mouse movement? In other words, if I want be able to move the mouse, but then evaluate the type of mouseclick (i.e. right-click, middle-click, wheel-up, wheel-down, etc.) before executing a function?

The problem with _IsPressed is the mouse-click is not intercepted, but rather it passes through before I have a chance to evaluate which button it is.

Link to comment
Share on other sites

If you have a GUI, I suggest GUISetOnEvent functions. Some possible events:

$GUI_EVENT_MOUSEMOVE - the mouse cursor has moved.

$GUI_EVENT_PRIMARYDOWN - the primary mouse button was pressed.

$GUI_EVENT_PRIMARYUP - the primary mouse button was released.

$GUI_EVENT_SECONDARYDOWN - the secondary mouse button was pressed.

$GUI_EVENT_SECONDARYUP - the secondary mouse button was released.

That includes mousemove, but I figured it'd be cool to have anyway. I'm not sure about middle click or mouse wheel.. I'm sure they're somewhere.

You're right about _IsPressed not trapping mouse clicks - that's what it's designed to do. It is supposed to record a movement, not hinder it in any way.

_IsPressed values for mouse buttons:

01 Left mouse button

02 Right mouse button

04 Middle mouse button (three-button mouse)

05 Windows 2000/XP: X1 mouse button

06 Windows 2000/XP: X2 mouse button

Link to comment
Share on other sites

  • Moderators

How would you use _IsPressed to "trap" the mouseclick only, not the mouse movement? In other words, if I want be able to move the mouse, but then evaluate the type of mouseclick (i.e. right-click, middle-click, wheel-up, wheel-down, etc.) before executing a function?

The problem with _IsPressed is the mouse-click is not intercepted, but rather it passes through before I have a chance to evaluate which button it is.

If _IsPressed('01') Then
    MsgBox(0, 'Test', 'Left was clicked')
EndIf
If _IsPressed('02') Then
    MsgBox(0, 'Test', 'Right was clicked')
EndIf
If _IsPressed('04') Then
    MsgBox(0, 'Test', 'Middle was clicked')
EndIf
Is that what you are after?

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

If you have a GUI, I suggest GUISetOnEvent functions. Some possible events:

$GUI_EVENT_MOUSEMOVE - the mouse cursor has moved.

$GUI_EVENT_PRIMARYDOWN - the primary mouse button was pressed.

$GUI_EVENT_PRIMARYUP - the primary mouse button was released.

$GUI_EVENT_SECONDARYDOWN - the secondary mouse button was pressed.

$GUI_EVENT_SECONDARYUP - the secondary mouse button was released.

That includes mousemove, but I figured it'd be cool to have anyway. I'm not sure about middle click or mouse wheel.. I'm sure they're somewhere.

I tried using a transparent full-screen GUI to capture mouse-button presses to open up a Message Box. However, two things:

1. It wasn't super responsive, even if I was using a While 1...Sleep(1)...WEnd. It would always work if I "held" the mouse-button down for a split second. It would sometimes work if I quickly "tapped" the mouse-button.

2. I didn't test this, but perhaps you know how it would behave. I imagine it would intercept all the mouse-button clicks, i.e. right-click, left-click, middle-click. However, could I allow left-click to pass through to the underlying application? Would I have to do something clunky like detect left-click, save mouse position, minimize the invisible GUI, mouseclick on that same position, restore invisible GUI?

Thanks for super-fast responses!

Link to comment
Share on other sites

That's what the _IsPressed() is for - it doesn't steal the click.

If you're trying to go back and forth between stealing the click and not stealing it, that seems difficult to me (at least off the top of my head). Maybe..

If _IsPressed ($key) Then

DoFunction()

Endif

Where you'd go off and call another function. Maybe some code from you could help me figure out what you're doing?

Or - if you want just left-click to go through, just don't create a GUI event for it.

Edited by greenmachine
Link to comment
Share on other sites

  • Moderators

Yes you did beat me green!! ;)

@crush - Are you not wanting to allow the mouse to click at all where the end-user wants to click it? Have to say... I don't understand exactly what you want :lmao:

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

If _IsPressed('01') Then
    MsgBox(0, 'Test', 'Left was clicked')
EndIf
If _IsPressed('02') Then
    MsgBox(0, 'Test', 'Right was clicked')
EndIf
If _IsPressed('04') Then
    MsgBox(0, 'Test', 'Middle was clicked')
EndIf
Is that what you are after?
Thanks for replying, perhaps it would make more sense if I described what I want to do.

I want to open up Internet Explorer and browse a web page. While I am browsing, I want to add certain web-links to my bookmarks, or I want to save certain web-links to a file. This normally involves going to a variety of menu and dialog box interfaces. Also, I need to type unique names into the dialog box depending on various criteria. AutoIt is a great way to automize these mundane tasks, right? I can do this easily with HotKeySet and a keyboard. But how could I assign "right-mouse-button" to perform a function? If I use _IsPressed, then the original right-mouse-button gets passed through before I have a chance to evaluate it and perform my function instead.

In fact, I'd imagine this would be useful to anyone who wants to lazily browse the internet with a mouse or laptop touchpad only, but doesn't want to move the cursor all the way up to the upper-left-corner just to hit the "BACK" button on the browser.

Link to comment
Share on other sites

For true Internet Explorer automation, DaleHolm's IE.au3. That's all I really have to say about that one.

So I'm trying to figure this out... you want to be able to use the mouse freely, but then you also want clicks to perform certain functions? Wouldn't that imply having a mouse button do two things at the same time?

If it is only 1 mouse button you want to have control over, the ispressed would definitely catch it and run the function, but still let it leak through. That sounds right to me. GUI events wouldn't be practical because you're working in IE rather than a GUI.... Sounds like the mouse is a fairly limited tool for this kind of use. Could just be me though.

Link to comment
Share on other sites

  • Moderators

Those that want to automate Internet Explorer generally will use Dale's IE.au3, you can find it in the scripts and scraps forum.

As far as it passing before you had time to evaluate... why not have the first ('02) go to another function with another Loop waiting to see if it will be another ('02) or ('01) or ('04')?

Edit: Dammit Green!!! I'm gonna do some work now... you seem to have things covered quite nicely :lmao:

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

For true Internet Explorer automation, DaleHolm's IE.au3. That's all I really have to say about that one.

So I'm trying to figure this out... you want to be able to use the mouse freely, but then you also want clicks to perform certain functions? Wouldn't that imply having a mouse button do two things at the same time?

If it is only 1 mouse button you want to have control over, the ispressed would definitely catch it and run the function, but still let it leak through. That sounds right to me. GUI events wouldn't be practical because you're working in IE rather than a GUI.... Sounds like the mouse is a fairly limited tool for this kind of use. Could just be me though.

Okay, obviously I am being unclear. Let me try again:

I appreciate the Internet Explorer suggestions, but to be honest, I use Firefox, so there is the reason I have not used the IE.au3 functions.

Regarding the matter of letting some mouse-clicks pass through, while trapping other mouse-clicks, let's imagine this scenario:

1. I am using a standard mouse with only left-click, right-click, middle-click, wheel up/down. (The mouse does NOT have extra buttons for BACK, FORWARD, VOLUME UP/DOWN, etc.)

2. I am using Firefox, browsing www.yahoo.com.

3. I left-click on Sports and that send me to sports.yahoo.com, as expected.

4. The cursor arrow is vaguely in the middle of the page (just where I left it).

5. I want to go BACK to the previous page. Normally I would have to move the mouse all the way up to the upper-left-corner of the screen to click on the tiny BACK arrow button. Or alternatively, I would move my hand off the mouse, to the keyboard, and press BACKSPACE key on the keyboard. Or I would type ALT-LEFTARROW on the keyboard.

5-alternative. I use AutoIt to detect my right-mouse-button click, and it clicks the BACK arrow up in the corner for me, without moving the cursor manually.

However, in order to make "5-alternative" occur, I have to detect the right-mouse-button without letting it leak through. If it leaks, it ends up pulling up the right-click-context menu in the middle of the screen first, then _IsPressed detects the right-click, then AutoIt executes the left-click. This might simply release the context menu, instead of performing the BACK arrow function.

I know this is a simplistic example maybe can be performed more robustly in other ways, but it illustrates the gist of what I'm trying to do. Another example would be: If I use _IsPressed("LeftClick") to Send("a") into a Notepad window, it will successfully type "a" upon each "LeftClick", but it will also move the insertion point to wherever the click occurs in the Notepad window each time I "LeftClick". How could I intercept the "LeftClick" so that it only sends "a"?

oof, i'm out of breath now

Link to comment
Share on other sites

5. I want to go BACK to the previous page. Normally I would have to move the mouse all the way up to the upper-left-corner of the screen to click on the tiny BACK arrow button. Or alternatively, I would move my hand off the mouse, to the keyboard, and press BACKSPACE key on the keyboard. Or I would type ALT-LEFTARROW on the keyboard.

Do you have two hands? Keep one hand on the backspace....

I really don't think it's going to work like that. It doesn't seem that mouse buttons can be made into hotkeys or anything that would work on all windows (not just GUIs, since you're obviously not using a GUI). I'm not sure hot HotKeySet works in terms of blocking keys, so I can't help with that. Looks like the mouse isn't going to be stealable unless there's some spiffy DllCall that I don't know about.

P.S. - For future reference, it helps to specify all the information correctly the first time. Saying you're using IE when you're using Firefox doesn't help. It seems we could have gotten to a simple "no" answer much sooner if we had the proper information. Just a heads up.

Link to comment
Share on other sites

  • Moderators

I want to open up Internet Explorer and browse a web page.

We gave you the suggestion of IE.au3 because of the specific statement above. You seem a bit confused on what it is truly that you want, or confused in fact on how to explain it.

Here is my suggestion.

1. Write some code

2. If said code fails provide the failing code to the forum for review and possible suggestions.

What your explaining above is almost understandable, however unless someone has already done it, your basically asking us to write it. Hope this makes sense.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Okay, fair enough. I was actually asking whether the concept is possible, rather than requesting specific code. However, because I am failing to express my needs, I will pose another hypothetical situation to illustrate the concept.

Imagine that I am playing a computer game where default keys are:

"w" is the "move forward"

"left-click" is "shoot gun"

"right-click" is "show inventory"

However, instead I want to hold "left-mouse-button" to move forward instead, and use "right-mouse-button" to shoot gun. So I make a script to type "w" repeatedly when "left-mouse-button" is pressed, and "left-click" when "right-mouse-button" is pressed.

Here is some code, nothing fancy.

#include <_IsPressed.au3>

While 1

    If _IsPressed('01') Then; 01 is left-click
        Send("w")
        Sleep(100)
    EndIf

    If _IsPressed('02') Then; 02 is right-click
        MouseClick("left")
        Sleep(100)
    EndIf

    If _IsPressed('1b') = 1 Then Exit;1b is ESC

WEnd

This works, but there are two problems:

1. When I click left-mouse-button, it succesfully sends "w" and "moves forward" but the "left-click" leaks through and accidentally "shoots gun" also.

2. When I click right-mouse-button, it leaks through and therefore it will first "show inventory" (its natural function) then perform "left-click" (the AutoIt function). This means instead of "shooting gun", it ends up selecting something from the "inventory window".

I could have accomplished this by using HotKeySet instead of _IsPressed, since HotKeySet will intercept the key and execute a function before it leaks through. However, HotKeySet only seems to work with keyboard presses, not mouse-clicks. Is this correct?

; Press Esc to terminate script, Pause/Break to "pause"

Global $Paused
HotKeySet("{PAUSE}", "TogglePause")
HotKeySet("{ESC}", "Terminate")
HotKeySet("LEFT-CLICK", "MoveForward") ; this is not valid line, for illustration only
HotKeySet("RIGHT-CLICK", "ShootGun") ; this is not valid line, for illustration only 

;;;; Body of program would go here;;;;
While 1
    Sleep(100)
WEnd
;;;;;;;;

Func TogglePause()
    $Paused = NOT $Paused
    While $Paused
        sleep(100)
        ToolTip('Script is "Paused"',0,0)
    WEnd
    ToolTip("")
EndFunc

Func Terminate()
    Exit 0
EndFunc

Func MoveForward()
    Send("w")
EndFunc

Func ShootGun()
    MouseClick("left")
EndFunc

If you think it would be more helpful, I could also describe a hypothetical application of mapping my "right-mouse-button" to act as a BACK BUTTON for web browsing, specifically for Firefox not IE. If that is still not enough, I could try to describe my original purpose, but honestly I think it would just be a bunch of mumbojumbo details.

Edited by crushinator
Link to comment
Share on other sites

  • Moderators

Okay, fair enough. I was actually asking whether the concept is possible, rather than requesting specific code. However, because I am failing to express my needs, I will pose another hypothetical situation to illustrate the concept.

Imagine that I am playing a computer game where default keys are:

"w" is the "move forward"

"left-click" is "shoot gun"

"right-click" is "show inventory"

However, instead I want to hold "left-mouse-button" to move forward instead, and use "right-mouse-button" to shoot gun. So I make a script to type "w" repeatedly when "left-mouse-button" is pressed, and "left-click" when "right-mouse-button" is pressed.

Here is some code, nothing fancy.

#include <_IsPressed.au3>

While 1

    If _IsPressed('01') Then; 01 is left-click
        Send("w")
        Sleep(100)
    EndIf

    If _IsPressed('02') Then; 02 is right-click
        MouseClick("left")
        Sleep(100)
    EndIf

    If _IsPressed('1b') = 1 Then Exit;1b is ESC

WEnd

This works, but there are two problems:

1. When I click left-mouse-button, it succesfully sends "w" and "moves forward" but the "left-click" leaks through and accidentally "shoots gun" also.

2. When I click right-mouse-button, it leaks through and therefore it will first "show inventory" (its natural function) then perform "left-click" (the AutoIt function). This means instead of "shooting gun", it ends up selecting something from the "inventory window".

I could have accomplished this by using HotKeySet instead of _IsPressed, since HotKeySet will intercept the key and execute a function before it leaks through. However, HotKeySet only seems to work with keyboard presses, not mouse-clicks. Is this correct?

If you think it would be more helpful, I could also describe a hypothetical application of mapping my "right-mouse-button" to act as a BACK BUTTON for web browsing, specifically for Firefox not IE. If that is still not enough, I could try to describe my original purpose, but honestly I think it would just be a bunch of mumbojumbo details.

Here I go again with a guess:
#include <misc.au3>

While 1
    If _IsPressed('01') Then; 01 is left-click
        While _IsPressed('01') = 1
            Send("w")
            Sleep(10)
        WEnd
    EndIf
    If _IsPressed('02') Then; 02 is right-click
        While _IsPressed('02') = 1
            Local $Pos = MouseGetPos()
            MouseClick("left", $Pos[0], $Pos[1], 1, 1)
            Sleep(10)
        WEnd
    EndIf
    If _IsPressed('1b') = 1 Then Exit;1b is ESC
WEnd
Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Might this work?

GUISetOnEvent ($GUI_EVENT_PRIMARYDOWN, "mouseclick", WinGetHandle ("title"))

Where you specify the title of the window you're attempting to control.

Hmm, doesn't look like it. Just tried - being nonGUI, I don't think it's going to work out. But the idea's still there.

Edited by greenmachine
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...