Jump to content

Simple Script - key pressed - send left click


Recommended Posts

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?

Link to comment
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?

Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
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!

Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
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.

Link to comment
Share on other sites

  • 3 years later...

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
Link to comment
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?

Link to comment
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?

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

 

Link to comment
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/

Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

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