Jump to content

Isn't omitting parameters in a (built-in) function call equivalent to calling using the Default keyword?


Recommended Posts

For instance,

#include <AutoItConstants.au3>

MouseMove(10,10)    ;I have Program Icon in the top left my screen that will highlight when clicked once.

Sleep(2000)
MouseClick($MOUSE_CLICK_LEFT, Default, Default, Default, Default)    ;Doesn't Work

Sleep(2000)
MouseClick($MOUSE_CLICK_LEFT)     ;Works

Note: I understand that there are many reasons why a mouse click may not register, but my question is generally asking about the use of the Default keyword vs omitting the variable altogether.

Code hard, but don’t hard code...

Link to comment
Share on other sites

Based on a couple of quick tests it looks like there might be a bug in MouseClick() if you use the Default keyword for the "Number of clicks,".

Normally, you would only use the Default keyword if you follow it with further parameter that you wish to specify. So, the last parameter would not normally be "Default".

1 hour ago, JockoDundee said:

MouseClick($MOUSE_CLICK_LEFT, Default, Default, Default, Default)    ;Doesn't Work

The above Default keywords are unnecessary.

It's not necessarily wrong to add them but it's probably best not to.

MODS: The help for MouseClick() appears somewhat ambiguous as it shows four arguments but the second argument consists of two parts, X and Y, so there are really five arguments to the function. Does the "Default" keyword cover both X and Y or would you need a separate "Default" for each.

Phil Seakins

Link to comment
Share on other sites

In fact, there is no automatic conversion from Default entry parameter and the actual default parameter inscribed in the func statement.

It is all reserved to the programmer of the function to assign the defaulted value when the function is called with Default keyword.

The programmer must do something like this :

If $FunctionParameter = Default Then $FunctionParameter = <Default Value> ; where <Default Value> is the value appearing in the Func statement

This is why you can use Default in some functions, and not in some others...

Link to comment
Share on other sites

11 hours ago, Nine said:

In fact, there is no automatic conversion from Default entry parameter and the actual default parameter inscribed in the func statement.

It is all reserved to the programmer of the function to assign the defaulted value when the function is called with Default keyword.

The programmer must do something like this :

If $FunctionParameter = Default Then $FunctionParameter = <Default Value> ; where <Default Value> is the value appearing in the Func statement

This is why you can use Default in some functions, and not in some others...

You are right, there is no automatic conversion, certainly for UDF's, as we can all test for ourselves.

However, I was asking specifically about built-in functions, and there it would appear you are right as well, though assumedly the code would be written in C.

Now that you have answered that, the question remains, is there a good or useful reason for this behavior, or is it just the way it is?

And if it is the as you describe, how is one to write a wrapper function?   For instance, I have some code like this:

#include <AutoItConstants.au3>

If MouseNotInUse() Then
   MouseClick($MOUSE_CLICK_LEFT); sometimes like this
   ....
EndIf

If MouseNotInUse() Then
   MouseClick($MOUSE_CLICK_LEFT, $x, $y) ; or this
   ....
EndIf

If MouseNotInUse() Then
   MouseClick($MOUSE_CLICK_LEFT, $x, $y, 1, 5) ; or this
   ....
EndIf

I say, hey maybe I should just create a MyMouseClick, since I always need to do certain processing before and after a mouse click.

So, like this:

#include <AutoItConstants.au3>

MyMouseClick($MOUSE_CLICK_LEFT); sometimes like this

MyMouseClick($MOUSE_CLICK_LEFT, $x, $y) ; or this

MyMouseClick($MOUSE_CLICK_LEFT, $x, $y, 1, 5) ; or this

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Func MyMouseClick($button, $x=Default, $y=Default, $c=Default, $t=Default)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

If MouseNotInUse() Then
   MouseClick($button, $x, $y, $c, $t)
   ...
EndIf

EndFunc

but if you are right, is there really no way to write a universal wrapper except explicitly coding something like:

#include <AutoItConstants.au3>

MyMouseClick($MOUSE_CLICK_LEFT); sometimes like this

MyMouseClick($MOUSE_CLICK_LEFT, $x, $y) ; or this

MyMouseClick($MOUSE_CLICK_LEFT, $x, $y, 1, 5) ; or this

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Func MyMouseClick($button, $x=Default, $y=Default, $c=Default, $t=Default)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
If MouseNotInUse() Then

   Switch @NumParams
     Case 1
        MouseClick($button)
     Case 2
        MouseClick($button, $x)
     Case 3
        MouseClick($button, $x, $y)
     Case 4
        MouseClick($button, $x, $y, $c)
     Case 5
        MouseClick($button, $x, $y, $c, $t)
    EndSwitch

    ....

EndIf

EndFunc

Thoughts?

Code hard, but don’t hard code...

Link to comment
Share on other sites

Not quite. Here how I would do it :

#include <Constants.au3>

MouseClick($MOUSE_CLICK_LEFT, Default, Default, Default, Default)    ;Doesn't Work

; while all those will works
MyMouseClick($MOUSE_CLICK_LEFT, Default, Default, Default, Default) ; will not do much, but working
MyMouseClick($MOUSE_CLICK_LEFT, Default, Default) ; will select word under mouse because simulate with prev line

Sleep (2000)

MyMouseClick($MOUSE_CLICK_LEFT, Default, Default, 3, Default) ; will select the whole line

Sleep (4000)

MyMouseClick($MOUSE_CLICK_LEFT, 200, 400, Default, 20) ; will move slowly the mouse cursor
MyMouseClick($MOUSE_CLICK_LEFT, Default, Default, 1) ; will click whatever is on that spot and the word

Func MyMouseClick($button, $x=Default, $y=Default, $c=1, $s=10)
  Local $aPos = MouseGetPos()
  If $x = Default Then $x = $aPos[0]
  If $y = Default Then $y = $aPos[1]
  If $c = Default Then $c = 1
  If $s = Default Then $s = 10
  Return MouseClick($button, $x, $y, $c, $s)
EndFunc

 

 

Edited by Nine
Link to comment
Share on other sites

4 hours ago, Nine said:

Not quite. Here how I would do it :

No, what you wrote was a specific wrapper for a particular case.  Also, since you are coding it specifically, what does the MouseGetPos() do? Since the Default,Default can be used for the X,Y per the doc.  But what I actually said was:

 

5 hours ago, JockoDundee said:

is there really no way to write a universal wrapper

When your talking about a universal wrapper certainly an admirable goal is to make it drop-in compatible with the wrapped function.  And as I said 

 

On 12/19/2020 at 5:04 PM, JockoDundee said:

but my question is generally asking about the use of the Default keyword vs omitting the variable altogether.

to that end, I will remove all references to MouseClick:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Func MyFunction($arg1=Default, $arg2=Default, $arg3=Default, $arg4=Default, $arg5=Default)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

   Switch @NumParams
     Case 0
        $r=Function()
     Case 1
        $r=Function($arg1)
     Case 2
        $r=Function($arg1, $arg2)
     Case 3
        $r=Function($arg1, $arg2, $arg3)
     Case 4
        $r=Function($arg1, $arg2, $arg3, $arg4)
     Case 5
        $r=Function($arg1, $arg2, $arg3, $arg4, $arg5)
    EndSwitch
    Return SetError(@Error, @Extended,$r)

EndFunc

This approach is one way to create a universal wrapper that can be used to without fear of breaking the function call, as long as the "real" function call  has the same or less number of arguments and that they are not passed ByRef. 

Its uglier than if Default actually did trigger the Default assignment automatically.

Having said that, how would you go about creating a generic wrapper without researching the default values and using MouseGetPos etc...?

Edited by JockoDundee
fixed mistake with argno added @error

Code hard, but don’t hard code...

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...