Jump to content

Start/Stop mode on KeyPress.


Recommended Posts

I've got a little problem with the script below what I started to edit, I need a script to have a start/stop mode to it... but so when it's started it loops over and over until it's pressed agaom/

#include <misc.au3>

Local $hDLL = DllOpen("user32.dll")

Global $Toggle = False

$KPressed = "13"
$Win01 = "01"
$Win02 = "02"

While 1

    ; Start and Stop
    If _IsPressed($KPressed, $hDLL) Then
        Switch $Toggle
            ; Started
Case False

; Windows Exist
If WinExists($Win01) Then
WinMove($Win01, "", 0, 0)
EndIf
If WinExists($Win02) Then
WinMove($Win02, "", 1920, 0)
EndIf

                $Toggle = True
                ConsoleWrite("Started" & @CRLF)
            ; Stopped
            Case True
                $Toggle = False
                ConsoleWrite("Stopped" & @CRLF)
        EndSwitch
    EndIf

    ; Wait until key is released or it will keep firing
    While _IsPressed($KPressed, $hDLL)
        Sleep(10)
    WEnd

    ; Idle Sleeping
    Sleep(10)
WEnd

Func Terminate()
; Close DLL
DllClose($hDLL)
; Exit
Exit
EndFunc

My start on modifying:

How do i go about making it work that way, I can't seem to put a while loop inside because it will just run forever...

Link to comment
Share on other sites

First of all. Is it by purpose you are using the Pause key (13) to stop start the script. I think the Pause key can have some impact on your running script. I don't see that impact when using another key f.ex. the 1-key (31).

Second. Your Terminate function is never called. You should test f.ex. the Esc key and then call the terminate function. Exactly as in post #2 in the subject that you refers to.

Link to comment
Share on other sites

Sorry for the quick and bad answer but I'm in hurry and can't lose much time. i'll try to help

#include <misc.au3>
Local $hDLL = DllOpen("user32.dll")
Global $Stop = False
$KPressed = "13"
While 1
    If _IsPressed($KPressed, $hDLL) Then Stop= True
Switch $Stop
Case True
;Code here
Case False
Sleep(10)
Wend
DllClose($hDLL)

More or less like this. It's a very basic scheme of how it works.

Note that this loop won't end.

To focus more on your problem:

You gotta do an If like this to make the program exit

If _IsPressed(ESC BUTTON CODE HERE, $hDLL) Then Exit

and PUT IT INSIDE the looping while or it will never be called because the While loop keeps going but Exit instruction is out of it.

Also, a little note:

By making if conditions, it will pause or exit only if the corresponding button is pressed in the moment the IF statement is running.

So, or you hold the button until it does what you want, or you gotta do it in another way.

At this point I don't remember how to do in a efficient way, If I remember well it has something to do with WM_MESSAGES but I may be wrong. You gotta catch with them button pressions on the fly.

Another solution i can think of, is making the whole program running into a GUI with buttons in OnEvent mode so you assign start and stop function to 2 different buttons with a specific function and so it will stop right when you press it because functions overrides each other in OnEvent.

All this must be confirmed by someone more expert, because I had to use those once, but it has passed much time and I don't remember well...

I'm a compulsive poster. When I post something, come to read it at least 5 minutes later after the posting, because I will edit it. I edited even this signature a few minutes later after I wrote it.

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