AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Killswitch - how to stop my macros from eating my computer.

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
Mellagon



Joined: 20 Feb 2008
Posts: 10

PostPosted: Fri Apr 11, 2008 6:20 pm    Post subject: Killswitch - how to stop my macros from eating my computer. Reply with quote

I have a macro that takes orders from the store website and enters them into our database without human interaction. Works like a charm and saves us hours and hours a day.

Until yesterday, when it got loose. It switched to the wrong window (whoops, my bad) and kept write on entering data. Yikes. Big mess.

What I'm trying to make (unsucessfully) is a killswitch that ends a hotkey immediately when pressed.

I have tried the following:

Code:
#z::exitapp
#s::goto startmyprogram

startmyprogram:
;rest of code here


The issue is that when the data is being inputed, the keystrokes being generated by the macro are overriding my attempts to hit #Z

I have tried making an external program that simply kills the AutoHotKey process, but it ran into the same error.

Suggestions?
Back to top
View user's profile Send private message
Rhys



Joined: 17 Apr 2007
Posts: 761
Location: Florida

PostPosted: Fri Apr 11, 2008 6:51 pm    Post subject: Reply with quote

If the PC is not being used by humans while that script is running, do this:
Esc::ExitApp
_________________
[Join IRC!]
Back to top
View user's profile Send private message
Mellagon



Joined: 20 Feb 2008
Posts: 10

PostPosted: Fri Apr 11, 2008 7:30 pm    Post subject: Reply with quote

Turning it into a single keystroke seems to solve part of the problem, but..

The problem is defining a hotkey early on makes the entire script into a hotkey script - i.e. I have to define a hotkey that launches the rest of the script.
Is there any way to define this 'killswitch' hotkey, and then proceed with the script without waiting for the 'start' hotkey to be pressed?

Here's what I have now.
Code:
msgbox Hit CTRL + WIN + S to start.
esc::exitapp
^#s::goto startitup


startitup:
Back to top
View user's profile Send private message
TheIrishThug



Joined: 19 Mar 2006
Posts: 419

PostPosted: Fri Apr 11, 2008 7:59 pm    Post subject: Reply with quote

I believe that hotkeys are initialized immediately regardless of where they are located in the script. Also, there is a Hotkey command.
Back to top
View user's profile Send private message Visit poster's website AIM Address
[VxE]



Joined: 07 Oct 2006
Posts: 3254
Location: Simi Valley, CA

PostPosted: Fri Apr 11, 2008 11:01 pm    Post subject: Reply with quote

Mellagon wrote:
Is there any way to define this 'killswitch' hotkey, and then proceed with the script without waiting for the 'start' hotkey to be pressed?

Code:
;.... some code....
Goto KeepExecutingAfterThisHotkey

; a hotkey
esc::exitapp
;...any other hotkeys you want to specify...

KeepExecutingAfterThisHotkey:
;.... more code to be executed.......

_________________
Ternary (a ? b : c) guide     TSV Table Manipulation Library
Post code inside [code][/code] tags!
Back to top
View user's profile Send private message
infogulch



Joined: 27 Mar 2008
Posts: 649

PostPosted: Sat Apr 12, 2008 3:35 am    Post subject: Reply with quote

You're talking about the Auto-execute section.

look here:

http://www.autohotkey.com/docs/Scripts.htm

Hotkeys you put in any part of the script will work anywhere, excluding any ifwin's, so maybe what you're looking for is:
Code:
#IfWinAcitve   ;<this blanks any previous ifwin's.
Esc::ExitApp  ;notice, this might not work right if you have multiple scripts running with this same hotkey


Put it at the very bottom of the script.

Hope that helps! Very Happy
_________________
Scripts - License
Back to top
View user's profile Send private message
orbik



Joined: 09 Apr 2008
Posts: 35
Location: Espoo, Finland

PostPosted: Sat Apr 12, 2008 4:54 am    Post subject: Reply with quote

If you use SendMode Input, any key presses while a send command is doing its work are postponed until the send is done. I don't know if using sendplay or sendevent changes this. What you might consider is using a loop to send smaller chunks of text at a time so that other hotkeys get a chance to intervene.

But honestly I also don't think AutoHotkey is a good solution for automatically sending data to a database, since the data is sent by simulating keystrokes. A better method would be to write data to buffers and notify the target application when a buffer is full.
Back to top
View user's profile Send private message
Rhys



Joined: 17 Apr 2007
Posts: 761
Location: Florida

PostPosted: Sat Apr 12, 2008 7:39 am    Post subject: Reply with quote

orbik wrote:
But honestly I also don't think AutoHotkey is a good solution for automatically sending data to a database, since the data is sent by simulating keystrokes. A better method would be to write data to buffers and notify the target application when a buffer is full.
I agree that relying on AHK to send data to a DB this way isn't a good practice - If you're using an Access DB, it would be much more reliable to append entries to a csv file that is linked as a table in that DB and have a query that runs once a day/hour/whenever that appends all of the data in that table and then clears the table*.

If you're really sure you want to stick with simulated keystrokes to do it, controlsend will give you more reliability. Just my 2c.

*I think access can modify a linked csv table, but if not, you could have the script do it.
_________________
[Join IRC!]
Back to top
View user's profile Send private message
infogulch



Joined: 27 Mar 2008
Posts: 649

PostPosted: Sat Apr 12, 2008 6:13 pm    Post subject: Reply with quote

Try "Critical, Off". Like this:
Code:
;auto-exec (very top):
Startup()  ;call function named starup


;Next:
Startup()  ;Function starts here
{
Ciritical, Off

;all the rest of your code......

}           ;And ends here


Esc::ExitApp
[somekey]::Startup()   ;Restart your script instead of exiting it.

I haven't tested it. I'll post an example later. (or I won't if it didn't work:P)
_________________
Scripts - License
Back to top
View user's profile Send private message
infogulch



Joined: 27 Mar 2008
Posts: 649

PostPosted: Sat Apr 12, 2008 6:31 pm    Post subject: Reply with quote

I tested it. This works:
Code:
Startup()


Startup()
{
   Critical, Off
   loop
   {
      ToolTip Test Iteration #%A_Index%
      Sleep 300
   }
}


Esc::ExitApp
^!r::Reload
Reload works better than calling the function again, as recalling it only works once.
_________________
Scripts - License
Back to top
View user's profile Send private message
orbik



Joined: 09 Apr 2008
Posts: 35
Location: Espoo, Finland

PostPosted: Sat Apr 12, 2008 6:46 pm    Post subject: Reply with quote

infogulch wrote:
I tested it. This works:

While that may work, it doesn't directly answer OP's problem:
Mellagon wrote:
The issue is that when the data is being inputed, the keystrokes being generated by the macro are overriding my attempts to hit #Z

I believe he wants to interrupt a Send command that sends a large number of key presses on a single call.
Back to top
View user's profile Send private message
infogulch



Joined: 27 Mar 2008
Posts: 649

PostPosted: Sun Apr 13, 2008 5:47 pm    Post subject: Reply with quote

Sure it will fix his problem, Mellagon will just have to pare down the send a little bit, depending on how instant he wants it to quit.

For example a:
Code:
SendInput abcdefghijklmnopqustuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789_

Would have to be split up a little depending on how much he is willing to have it mess up in case of an emergency.
Maybe like this:
Code:
Send abcdefghijklmnopqustuvwxyz
Send _ABCDEFGHIJKLMNOPQRSTUVWXYZ
Send _0123456789_

Same results, with a space in between to let a cancel come in.

Now in testing, that may work in theory, but as the help file points out: "using "Critical Off" will not result in an immediate interruption of the current thread. Instead, an average of 5 milliseconds will pass before an interruption occurs. This makes it more than 99.999% likely that at least one line after "Critical Off" will execute before an interruption"
(Critical help page is HERE)

So, instead, as that help file pointed out, use "Sleep -1" for it to "force interruptions to occur immediately."

Like this tested sample I made:
Code:
Esc::ExitApp
^!r::Reload
^!h::Startup()

Startup()
{
   Critical, Off
   loop 10
   {
      SendInput abcdefghijklmnopqustuvwxyz
      Sleep -1
      SendInput _ABCDEFGHIJKLMNOPQRSTUVWXYZ
      Sleep -1
      SendInput _0123456789_
      Sleep -1
   }
}

It instantanously stops before the next SendInput when you press Esc. (Sleep help page is HERE)

I noticed that with this little script, sendinput seemed faster than just send. (it probably says that in help somewhere) I also noticed that one key works better for interrupting than a keystroke, try the reload while it's running, for example.

Now, if you wanted to interrupt a send anyway, I don't think that's possible in the same script, but you might be able to have a completely different script force the first to exit with a hotkey, but I believe that the current command has to finish before another can be acitvated.

Note: I knew nothing of critical and sleep -1 before I posted on this thread. I got it all from the help file. It refrences itself very well. Refrence it yourself, read it, reread it, use it!

Hope I helped. Very Happy
_________________
Scripts - License
Back to top
View user's profile Send private message
Mellagon



Joined: 20 Feb 2008
Posts: 10

PostPosted: Fri Apr 18, 2008 4:27 pm    Post subject: Reply with quote

Thanks everyone - the solution I am using for now is a simple one. A single-key interrupt seems to do the trick. I'll likely try some of the other solutions later.

Code:
msgbox Hit CTRL + WIN + S to start.
esc::exitapp
^#s::goto startitup


Quick note on using AHK to input data - you'd be surprised how many companies pay someone to manually input order data from one system into another. AHK is an excellent 'middle-techincal' solution.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group