Jump to content

Problem with _IsPressed


Go to solution Solved by SadBunny,

Recommended Posts

Trying to make different small scripts to learn autoit, this time I'm trying to make a script that when a button is pressed it will click in 1 place then click in another place then return the mouse to it's original position.

This is the original one that I made which works.

HotKeySet("c", "practice")

while 1
    sleep(1)
WEnd

Func practice()
        $pos=MouseGetPos()
        MouseClick("left",100,922,1,0)
        MouseClick("left",200,921,1,0)
        MouseMove($pos[0],$pos[1],0)
EndFunc

The problem is when I try to implement an On/Off hotkey, I can no longer find a way to make it work. (ideally just 1 hotkey for turning on and off but would be nice to learn with 2 hotkeys as well)

Here is what I tried:

#include <Misc.au3>

HotKeySet("{F1}", "_Pause")
Global $Paused = False

HotKeySet("c")

While 1
    If Not $Paused Then
        If _IsPressed("c") Then
            $pos=MouseGetPos()
            MouseClick("left",100,922,1,0)
            MouseClick("left",200,921,1,0)
            MouseMove($pos[0],$pos[1],0)
        EndIf
    EndIf
WEnd


Func _Pause()
    $Paused = Not $Paused
EndFunc

But this code doesn't seem to be doing anything at all  :

Link to comment
Share on other sites

Firstly: you need to set a function to call on your "HotKeySet("c")". Running it like this just unsets the hotkey.

Secondly: so what is it wou want the script to do exactly?

1. Just <click position 1 - click position 2 - return to current position> ad infinitum as fast as possible when the script is unpaused? (In which case I'd ask, why the c hotkey?)

2. Do the click-click-move once every time the hotkey is pressed? (In which case I'd ask what it is that you want to pause?)

Your code kind of looks like it tries both...

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Firstly: you need to set a function to call on your "HotKeySet("c")". Running it like this just unsets the hotkey.

Secondly: so what is it wou want the script to do exactly?

1. Just <click position 1 - click position 2 - return to current position> ad infinitum as fast as possible when the script is unpaused? (In which case I'd ask, why the c hotkey?)

2. Do the click-click-move once every time the hotkey is pressed? (In which case I'd ask what it is that you want to pause?)

Your code kind of looks like it tries both...

 

It doesn't have to be "c" it can be anything I just randomly chose "c". It's not a script aimed to do something concrete it's only for practice purposes. I want the script to click at 2 different places when I press the hotkey "c" and then return the mouse to it's original position. I'll try to give an example of it's application although it wasn't the purpose so you can visualize it. If we assume that we have 2 monitors and each monitor has a maximized chrome tab open, you could take the coordinates of the refresh button on each tab, or the X button, or minimize button it doesn't really matter which, and when you press the hotkey, in this case "c" it will click on those 2 buttons and both chrome tabs will refresh, minimize or close depending which coordinates you chose. Again this is just a script for practice, it doesn't have a real purpose other than that. An application for the on/off button in this particular example would be to prevent the script from clicking at those cordinates while you're typing in the chrome adress bar, or on facebook, google etc, if you type a word with "c" it will automatically click unintentionally, so if there is a ON/OFF button you can turn it off just before typing, then turn it back on when you're done typing, therefore the loop.

Link to comment
Share on other sites

  • Solution

Ah, ok, so if I understand you correctly, you'd like hotkey 1 ("c") to do the clickyclicky and hotkey 2 ("{f1}") to enable/disable hotkey 1.

This is easy to build, see the code below. But what I would prefer in your case is just a more "complicated" hotkey, like ctrl+x or alt+x instead of just c. To set these combos as hotkey, use respectively "^x" or "!x" or even "^!x" for ctrl+alt+x. There are many more combinations possible, see the SetHotKey and Send helpfiles for examples.

But the code that actually does what you described in the last post would be something like this. (Note that the console logs can be removed - it's just to show you what's going on while testing from SciTE :) )

#include <Misc.au3>

HotKeySet("{F1}", "toggleOtherHotkey") ; link hotkey F1 to a function 

HotKeySet("c", "doClickyClicky") ; link hotkey c to a function 

Global $Paused = False

While 1
    sleep(1000) ; just do nothing at infinitum, the hotkeys will trigger immediately anyway
WEnd

Func toggleOtherHotkey()

    $Paused = Not $Paused

    if $Paused Then
        HotKeySet("c") ; unset/disable hotkey c
        ConsoleWrite("Hotkey c disabled." & @CRLF)
    Else
        HotKeySet("c", "doClickyClicky") ; link hotkey c to a function 
        ConsoleWrite("Hotkey c enabled." & @CRLF)
    EndIf
EndFunc

Func doClickyClicky()
    ConsoleWrite("Doing the clickyclicky thing!" & @CRLF)
        ; insert your clicky clicky code here :)
EndFunc
Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Thanks, it's what I was looking for...it works like a charm and also helped me discover new stuff. Can you explain me this line of code? ConsoleWrite("Hotkey c enabled." & @CRLF). I looked it up but don't fully understand. 

Edited by mattherat909
Link to comment
Share on other sites

Thanks, it's what I was looking for...it works like a charm and also helped me discover new stuff. Can you explain me this line of code? ConsoleWrite("Hotkey c enabled." & @CRLF). I looked it up but don't fully understand. 

When run from SciTE you will see this as output in the console area.

Link to comment
Share on other sites

Continued rewording of a locked topic for game automation.

 

I edited my post because I couldn't understand the code, and as I kept reading it and testing it in the editor I figured out more and more like the console code. Is there a purpose to your post other than being a douche, how is my question related to game automation???

Link to comment
Share on other sites

What does that prove? That my question isn't related to gaming? Thank you very much. As seen in the post above I asked a question about learning autoit, not about how to script a game because people in these forums tend to be very unhelpful and only look to discredit you like your post above just showed. Also get a life.

Edited by mattherat909
Link to comment
Share on other sites

I don't see mention of game automation in this topic either, and no mod has locked it yet. Let's leave the torches and pitchforks in the shed for now, shall we? That spambot is causing enough enmity as it is.

Having said that, mousemoving and -clicking is rarely the best way to go when automating software. (Moving windows, changing resolutions, applications blocking/owning the mouse and many other possible problems.) When you explain more about what you are actually trying to automate, forumers here can often offer better ways to approach the problem.

Back on topic:

Thanks, it's what I was looking for...it works like a charm and also helped me discover new stuff. Can you explain me this line of code? ConsoleWrite("Hotkey c enabled." & @CRLF). I looked it up but don't fully understand. 

 

Are you using SciTE? If so, then ConsoleWrite will write a string to the console panel (on the bottom).

If your question is about the & @CRLF part: it attaches (the & concatenates strings) a Windows newline (@CRLF, Carriage Return + Line Feed) to the written string. Keeps it readable. Remove it and see what happens in the console panel.

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Yes thank you, I understood that part. I'm not sure if I should post this in a new thread since it's related to this same code. After experimenting with the code, I tried altering to send a message instead of a click. If you use a different hotkey than "c" like F2 or F3 etc it's fairly easy to do however if you use keyboard letters, it might cause problems if the letters are repeated in the message that you are sending. For instance, if you type "Hello world" everytime you press "c", there will be no problems, but if you type "Chocolate paradise" it will cause a loop because the message itself contains the letter "c", is there a way to keep the same On/Off functionality and "c" as the activating key but not cause a loop? So basically you turn on/off with F1, and you send the "Chocolate paradise" message with "c" only once per "c" press.

#include <Misc.au3>

HotKeySet("{F1}", "toggleOtherHotkey") ; link hotkey F1 to a function

HotKeySet("c", "doClickyClicky") ; link hotkey c to a function

Global $Paused = False

While 1
    sleep(1000) ; just do nothing at infinitum, the hotkeys will trigger immediately anyway
WEnd

Func toggleOtherHotkey()

    $Paused = Not $Paused

    if $Paused Then
        HotKeySet("c") ; unset/disable hotkey c
        ConsoleWrite("Hotkey c disabled." & @CRLF)
    Else
        HotKeySet("c", "doClickyClicky") ; link hotkey c to a function
        ConsoleWrite("Hotkey c enabled." & @CRLF)
    EndIf
EndFunc

Func doClickyClicky()
    ConsoleWrite("Doing the clickyclicky thing!" & @CRLF)
    send("Hello world")
EndFunc
Edited by mattherat909
Link to comment
Share on other sites

  • Developers

I edited my post because I couldn't understand the code, and as I kept reading it and testing it in the editor I figured out more and more like the console code. Is there a purpose to your post other than being a douche, how is my question related to game automation???

I have been clear so you know the consequences.

 

Bye bye

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

  • Recently Browsing   0 members

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