Sign in to follow this  
Followers 0
markronz

Simple Script - key pressed - send left click

16 posts in this topic

Hello everyone-

I am just trying to write a simple script. All that it does, is when someone presses the 'h' key that it still sends the 'h' keypress and then simulates a left mouse click (at the current position). I would like this scipt to run continuously, monitoring for this h keypress. From searching the forums here, this is what I have written up so far:

HotKeySet("h", "Actions")

While 1
    Sleep(100)
WEnd


Func Actions()
        send("h")
        MouseClick("left")
        Sleep(5)
EndFunc

Is this going to form a continuous loop? When I put a message box within the Actions function, it certainly seems like there is an infinite loop type situation going on. Is there some other way I should go about this?

Share this post


Link to post
Share on other sites



Wait I think I figured it out! This is my new script:

HotKeySet("h", "Actions")

While 1
    Sleep(100)
WEnd


Func Actions()
    HotKeySet("h")
    Send("h")
    MouseClick("left")
    HotKeySet("h", "Actions")
    Sleep(5)
EndFunc

A few questions though:

1. Is the While/sleep loop needed at the top? I just want to be sure that this script runs continuously.

2. Any tips on how I would proceed if I only wanted this action to take place if a specific process was running on the machine?

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

markronz,

Welcome to the AutoIt Forum. :x

Is this going to form a continuous loop?

Yes. :P

Is there some other way I should go about this?

There are several ways to run infinite loops, but what you have is probably the most common format.

However I would add another HotKey to end the script - saves opening Task Manager to do it - along these lines: :shifty:

HotKeySet("{ESC}", "On_Exit")
Func On_Exit()
    Exit
EndFunc

M23

Edit: Your second atttempt is not good - should have stuck with the first one! :nuke:

As to running for a particular proces, just check in the function if the process exists and return if not:

Func Actions()

        ; If process does not exist then return
        If Not ProcessExists("fred.exe") Then Return
        ; So if the process does exist we carry on
        send("h")
        MouseClick("left")
        Sleep(5) ; Not really necessary, but does no harm
EndFunc
Edited by Melba23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Hey Melba, thank you for your quick response! I have some follow up questions, but before I do, here is my script as it stands now:

Opt("TrayIconHide", 1)          ;0=show, 1=hide tray icon

HotKeySet("h", "Actions")

While 1
    Sleep(100)
WEnd


Func Actions()
    If ProcessExists("TextPad.exe") Then
        HotKeySet("h")
        Send("h")
        MouseClick("left")
        HotKeySet("h", "Actions")
        Sleep(5)
    EndIf
EndFunc

As you can see I did use the ProcessExists funtion as you mentioned. Found it right after I posted, sorry about that. I think this accomplishes the same thing as what your code does above.

Now you said that my second attempt was not good. I think you may be mistaken, unless I am not understanding something. Coming from the AutoIt Help file, it says directly in there this:

; capture and pass along a keypress

HotKeySet("{Esc}", "captureEsc")

Func captureEsc()

; ... can do stuff here

HotKeySet("{Esc}")

Send("{Esc}")

HotKeySet("{Esc}", "captureEsc")

EndFunc

So that was what I was copying. I want to capture the 'h' keypress and perform some actions, however, it seems that if you dont alter the HotKeySet statement as in the above example, that it would just call itself over and over, that continuous loop I was talking about. So you press 'h', it gets caught, the script sends an 'h', which in turn gets caught again and repeats! So I think you need to have those other HotKeySet statements in there so that this does not occur. Hopefully that makes sense.

Also, duely noted about the exit (Esc) key.

My current script above seems to perform as I desired it to. Do you have any concerns about it being incorrect? Besides the current lack of an escape key?

Edited by markronz

Share this post


Link to post
Share on other sites

markronz,

You missed an inportant part of the line I used:

If Not ProcessExists("fred.exe") Then Return

See the Not in there? It will return only if there is no process - if the process exists it carries on. :x

However, you get your own back with the HotKey setting and unsetting - I did not notice that you were sending the same key as the HotKey itself. Your current code is absolutely correct as you must unset it or you get into recursion problems! :P

We will settle on 15-all for that exchange I think! :shifty:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

:x Thanks again for the quick response.

about the ProcessExists stuff, can you tell me what the difference between your code and mine is? Mine also seems to work:

;YOURS
If Not ProcessExists("fred.exe") Then Return
;stuff

;MINE
If Not ProcessExists("fred.exe") Then
;stuff
EndIf

I've tried both yours and mine and both seem to work. Just want to make sure they do the same thing, and one is not better than the other! Thanks!

Share this post


Link to post
Share on other sites

markronz,

Your code is Not what you posted before (pun fully intended!) - look carefully at your earlier post. :P

These will produce the same result:

If Not ProcessExists("fred.exe") Then Return
;stuff

If ProcessExists("fred.exe") Then
;stuff
EndIf

What you just posted will not. :x

It is the Not that makes the difference. :shifty:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

lol oops, sorry, didnt mean to add that Not in there that second time around.

Well, anyway, I think I'm all set. Thanks for the help!

Share this post


Link to post
Share on other sites

That Sleep(5) is not doing what you think it's doing.

$iTimer = TimerInit()
For $iX = 1 To 100
    Sleep(5)
Next
ConsoleWrite(TimerDiff($iTimer) & @CRLF)

I've tried both yours and mine and both seem to work. Just want to make sure they do the same thing, and one is not better than the other! Thanks!

Melba's look neater to the eye. Looks are less important if you are blind (yes blind people show up on occasion) but even then you would probably take it for the simplicity. It's up to you how to do it.

Also you may want to look at _IsPressed, no need for that Send then.

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Sorry for the necro, but after trying about 20 different similar scripts this was the ONLY one that compiled correctly. My problem is that the sleep timer seems to have no effect. As I understood this was supposed to pause for 100 milliseconds in between each loop, but even if I set them as high as 30000 I still get way too many loops per second. I estimate my slightly modified script is running 30 times per second, but I need it to be more like 5 times per second.

I just want a left click sent 5 times a second that can be toggled with a hotkey.

Here's a rough outline of what I need:

KEY::Toggle on/off

on=goto loop, else stop

loop{left click mouse, sleep 200}

Actual script that is running much too fast and ignore the sleep paremeter:

Global $Paused, $Runner

HotKeySet("{F10}", "TogglePause")

HotKeySet("{ESC}", "Terminate")

HotKeySet("{F9}", "ShowMe")



;;;; Body of program would go here ;;;;

While 1

   Sleep(20000)

WEnd

;;;;;;;;



Func TogglePause()

   $Paused = Not $Paused

   While $Paused

      Sleep(30000)

      ToolTip('Script is "Paused"', 0, 0)

   WEnd

   ToolTip("")

EndFunc   ;==>TogglePause



Func Terminate()

   Exit 0

EndFunc   ;==>Terminate



Func ShowMe()

   $Runner = Not $Runner

   While $Runner

      MouseClick("left")

   WEnd

EndFunc   ;==>ShowMe
Edited by joe7dust

Share this post


Link to post
Share on other sites

 

Sorry for the necro, but after trying about 20 different similar scripts this was the ONLY one that compiled correctly. My problem is that the sleep timer seems to have no effect. As I understood this was supposed to pause for 100 milliseconds in between each loop, but even if I set them as high as 30000 I still get way too many loops per second. I estimate my slightly modified script is running 30 times per second, but I need it to be more like 5 times per second.

I just want a left click sent 5 times a second that can be toggled with a hotkey.

Here's a rough outline of what I need:

KEY::Toggle on/off

on=goto loop, else stop

loop{left click mouse, sleep 200}

Actual script that is running much too fast and ignore the sleep paremeter:

Global $Paused, $Runner

HotKeySet("{F10}", "TogglePause")

HotKeySet("{ESC}", "Terminate")

HotKeySet("{F9}", "ShowMe")



;;;; Body of program would go here ;;;;

While 1

   Sleep(20000)

WEnd

;;;;;;;;



Func TogglePause()

   $Paused = Not $Paused

   While $Paused

      Sleep(30000)

      ToolTip('Script is "Paused"', 0, 0)

   WEnd

   ToolTip("")

EndFunc   ;==>TogglePause



Func Terminate()

   Exit 0

EndFunc   ;==>Terminate



Func ShowMe()

   $Runner = Not $Runner

   While $Runner

      MouseClick("left")

   WEnd

EndFunc   ;==>ShowMe

 

Should I just make a new thread?

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

HotKeySet("{F1}", "fiveClicks")
HotKeySet("{ESC}", "Quit")

While 1
    ; run indefinitely
WEnd

Func fiveClicks()
    Local $click_Counter = 1
   
    Do
        MouseClick('left')
        $click_Counter += 1
        Sleep(200)
    Until $click_Counter = 5

    $click_Counter = 1
EndFunc

Func Quit()
    Exit 0
EndFunc

something like this?

Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites
HotKeySet("{F1}", "fiveClicks")
HotKeySet("{ESC}", "Quit")

While 1
    ; run indefinitely
WEnd

Func fiveClicks()
    Local $click_Counter = 1
   
    Do
        MouseClick('left')
        $click_Counter += 1
        Sleep(200)
    Until $click_Counter = 5

    $click_Counter = 1
EndFunc

Func Quit()
    Exit 0
EndFunc

something like this?

 

Sort of, when I say "toggle on/off" what I mean is it for to be continuous. Needs to loop endlessly until I cancel it by pressing the hotkey again (or another hotkey if that makes it easier)

I could swear that the one here called "the toggle" used to work perfectly, but there may have been a new version of autoit released or something because I had to format my old windows installation and now it gives an error when compiling. http://www.autohotkey.com/board/topic/64576-the-definitive-autofire-thread/

Share this post


Link to post
Share on other sites

joe7dust,

You do realise that that link is to the AutoHotKey forum and this is the AutoIt forum? Little wonder you had problems compiling. ;)

M23

1 person likes this

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Oh. My. Derp. Sorry for wasting you guys' time.

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