Sign in to follow this  
Followers 0
OldManJack

Mouse Clicking as a trigger

9 posts in this topic

I'm trying to improve a little program such that, instead of exporting mouse position to a notepad file on a timer, it lists it when the mouse is clicked.

Opt("OnExitFunc", "endscript"); So that the file gest closed on exit

AdLibEnable("_GetPos", 33); Run the _GetPos() function ever 33 ms
$file = "c:\mouseposdata.txt"; Specify the text file for writing co-ordinates to
$file = FileOpen($file, 9); Opens file and test to see that it exists
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

While 1
    Sleep(10); Maintain script
WEnd

Func _GetPos(); Function to get the mouse position, and writes it to the text file
$pos = MouseGetPos()
FileWriteLine($file, $pos[0] & "-" & $pos[1])
EndFunc;==> _GetPos()

Func endscript(); function to run when autoit exists
    FileClose($file); Closes the file handle on exit
EndFunc

It shouldn't be to hard to change the Func _GetPos to an If Then set, but I can't figure out if there's a function for testing if the mouse if clicked or if it does exist, were I would look it up.

Share this post


Link to post
Share on other sites



Look up IsPressed() in the User Defined Functions part of the help file...

#Include <Misc.au3>

_IsPressed ( $s_hexKey [, $v_dll = 'user32.dll' ]

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

Share this post


Link to post
Share on other sites

Very much appreciated. I've found the section and the command, but wow does it create more questions.

I've listed out the function format, parameters and sample coding from the help file for simplicity. The input list really isn't needed because I'm only going to be working with the Windows 2000/XP mouse button 1, left mouse button, listed under the code "05."

Check if key has been pressed

Format

#Include <Misc.au3>

_IsPressed ( $s_hexKey [, $v_dll = 'user32.dll' ] )

Parameters

$s_hexKey key to check for

$v_dll Optional: Handle to dll or default to user32.dll

#include <Misc.au3>

$dll = DllOpen("user32.dll")

While 1
    Sleep ( 250 )
    If _IsPressed("23", $dll) Then
        MsgBox(0,"_IsPressed", "End Key Pressed")
        ExitLoop
    EndIf
WEnd
DllClose($dll)

Just to test my understand of the function.

The first line of the listed _IsPressed format, #Include <Misc.au3>, tells the the script to reference the pre-existing file, Misc.au3, that already contains the correct variables, functions, etc. A scriptlet as the User Defined Functions Notes help section refers to it.

The second line, _IsPressed ( $s_hexKey [, $v_dll = 'user32.dll' ] ), lists the function command (key to check for, [defined variable]). What I don't understand is the Optional parameter listed as $v_dll. I'm a little fuzzy on the purpose of the file user32.dll, but it might be easier just to list what the function might need to refer or change within the user32.dll file. I'm guessing it isn't mandatory for the purpose I'm using the command for.

The other thing I don't understand if how to assign the _IsPressed to to a boolean style variable for use in an If-Then loop. An explaination or a little code would be greatly appreciated. I'll be doing a little experimentation in the meanwhile, right after I backup this computer...

Share this post


Link to post
Share on other sites

The example somewhat confused me as well. I just leave out the optional parameter when using _IsPressed.

You won't need to assign it any value in an if statement because it will just automatically run.

In other words, you could just do this

if _IsPressed("54") Then
     Send("{BACKSPACE}")

Then if you pressed "54", it would execute the code. The boolean is already in there, I guess.

Share this post


Link to post
Share on other sites

Thanks Nevin. That reduced the lines of code by a whole 18%

Here's what I've gotten so far.

#include <Misc.au3>

$file = "c:\mouseposdata.txt"; Specify the text file for writing co-ordinates to
$file = FileOpen($file, 9); Opens file and test to see that it exists

If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
    
While 1
    Sleep ( 30 )
    If _IsPressed("01") Then
        $pos = MouseGetPos()
        FileWriteLine($file, $pos[0] & "-" & $pos[1])
    EndIf
WEnd

Is it possible to specify that the FileWriteLine() only be performed once per click?

I've increased the sleep time on the while loop from 10ms to 30ms, which has reduced the number of redundant (X,Y) coordinate lines I'm getting per click, and there's still two or three, and missing a click would be equally annoying.

I've also copied out the _Ispressed scriplet out of the Misc.Au3 file although it doubt it'll be much help.

;===============================================================================
;
; Description:  _IsPressed
; Parameter(s): $s_hexKey - key to check for
;                       $v_dll = Handle to dll or default to user32.dll
;
; User CallTip:   _IsPressed($s_hexKey[, $v_dll = 'user32.dll']) Check if key has been pressed. (required: <Misc.au3>)
; Return Value(s):  1 if true
;                           0 if false
; Author(s):      ezzetabi and Jon
;
;===============================================================================
Func _IsPressed($s_hexKey, $v_dll = 'user32.dll')
; $hexKey must be the value of one of the keys.
; _Is_Key_Pressed will return 0 if the key is not pressed, 1 if it is.
    Local $a_R = DllCall($v_dll, "int", "GetAsyncKeyState", "int", '0x' & $s_hexKey)
    If Not @error And BitAND($a_R[0], 0x8000) = 0x8000 Then Return 1
    Return 0
EndFunc  ;==>_IsPressed

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

edit: Never mind. I got it.

#include <misc.au3>

While 1
    Sleep ( 30 )
    If _IsPressed("01") Then ;mouse button goes down
        $pos = MouseGetPos() ;grabs coords
        Do
        Until(NOT _IsPressed("01")) ;waits until mouse button goes back up
        Send($pos[0])
        Send(" ")
        Send($pos[1])
    EndIf
WEnd

There might be a better way to wait for a condition, but hey, it works. Now it will wait until the mouse button goes back up before writing anything. I think you could get rid of the Sleep(30) all together now.

Edited by Nevin

Share this post


Link to post
Share on other sites

Brilliant!

Share this post


Link to post
Share on other sites

Short explanation: The purpose of the second variable in the function is so you can use a DllOpen reference to user32.dll.

Longer explanation: The _IsPressed function uses DllCall to execute a function inside the file user32.dll. If you don't use the second parameter then user32.dll is loaded and then unloaded everytime the function is executed. If you DllOpen() user32.dll and then use the handle you receive from that function as your second parameter then the dll is only loaded once, and then it stays in memory until you unload it (with DllClose()).

Example:

This code:

While 1
   _IsPressed('01')
WEnd

Is evaluating something like this:

Load user32.dll
Use loaded dll: Check if key 01 is pressed
Unload loaded dll

Load user32.dll
Use loaded dll: Check if key 01 is pressed
Unload loaded dll

Load user32.dll
Use loaded dll: Check if key 01 is pressed
Unload loaded dll
...etc...

Whereas this code:

$dll = DllOpen('user32.dll')
While 1
   _IsPressed('01', $dll)
WEnd
DllClose($dll)

Evaluates more like this:

Load user32.dll to $dll
Use $dll: check if key 01 is pressed
Use $dll: check if key 01 is pressed
Use $dll: check if key 01 is pressed
...etc...
Unload $dll

See the difference? It makes your script easier on the processor.

Share this post


Link to post
Share on other sites

Sweet. Thanks for the info. Luckily it's Sunday, so I won't be drinking the brain cells away that just stored that.

Share this post


Link to post
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
Sign in to follow this  
Followers 0