AHK turn off loop when Key is pressed - autohotkey

I created a code that should send the "E" key until i press the "T" key. Right now the loop only stops when I hold the "T" key. Could someone help me with this.
F12::
loop
{
Send e
Sleep 3500
if (GetKeyState("t")) {
break
}
}
return

I would suggest to use the following code:
F12::
Loop {
Send, e
Sleep 3500
if break
break
}
return
T::
break := !break
return
The above script will stop in a interval between 0 and 3500ms after you press the "T" because you have a "Sleep 3500" in each evaluation.
Your script only stop when you HOLD the "T", because the "T" MUST be pressed while the loop IS ON the GETKEYSTATE function.
P.S:
My english is a work in progress..

Related

autohotkey: getting characters of a string sent, while typing normally, to stay together

coloured_text:
send {^}%rand%
return
~t::
Random, rand, 1, 3
SetTimer, coloured_text, 500
return
~esc::
~enter::
SetTimer, coloured_text, Off
return
Code: When "t" is pressed the variable RAND gains a random number from 1-3. A label is then called 0.5 seconds later. This label sends the text "^%RAND%" i.e. ^1. The Label repeats every .5 seconds until enter/esc is hit. While all of this is happening though, as soon as I press "t", I am also manually typing sentences out i.e. "Big red dog".
So a good output might looks like: *"Bi^1g red ^3dog".
But sometimes I get somethin like: *"Bi^g1 red ^do3g".
In other words I need the label to send the two characters of "{^}%rand%" i.e. "^1" without breaks/gaps. And I need this to be done without interfering with my normal typing, whatever I may be typing. And I can't use any of the other sends (sendinput sendplay sendevent), just normal send.
That looks like color control codes of IRC, you can use something like this either via hotkey or hotstring.
It creates a small Gui with just an Edit so you can input the text you want colorized and when you press enter every 3 characters (configurable) get a random color.
If you still like your approach, you can take a look into BlockInput docs and wrap Send % "{^}" rand between them. Is reliable as long as you run the script with UI Access
F1::Colorize()
:*X:/color::Colorize()
Colorize()
{
Gui C:New, +LastFound -SysMenu
Gui Add, Edit, -WantReturn r5 w200
Gui Add, Button, Default Hidden x0 y0, OK
Gui Show,, Colored Text
}
CButtonOK()
{
GuiControlGet Line,, Edit1
Gui Destroy
Out := "", i := 0
loop parse, Line
{
if (A_LoopField = " ")
{
Out .= " "
continue
}
/* Every 3 characters
*/
if !Mod(i++, 3)
{
Random r, 1, 3
Out .= "{^}" r
}
Out .= A_LoopField
}
Send % Out
}
CGuiEscape()
{
Gui Destroy
ExitApp
}

Why autohotkey deos not send Ctrl-Space where Space is an input

Consider the following assignment:
When I type - followed by a key, the result is Ctrl-key. This work for ordinary keys.
But when the key is whitespace, it does not work.
Any idea why this happens? And how to fix the code?
-::
Input, key, L1,{LCtrl}
send, ^{%key%}
return
Edit.
Try to run the above script a program which has Ctrl-Space as a shortcut to see that it does not work. In fact, if you press - followed by Space, the script is suppose to call Ctrl-Space but it is not the case. For example:
In Microsoft Excel or in Libreoffice Calc, Ctrl-Space can select the current column.
In Emacs Ctrl-Space is reserved for setting a Mark.
Use SendInput instead.
Tested in Excel to mimic ^a, ^x, ^v, ^space
-::
Input, key, L1,{LCtrl}
SendInput, ^%key%
Return
If you want to handle "special" keys, add those keys to the list of endkeys using this syntax
Input [, OutputVar, Options, EndKeys, MatchList]
And then check to see which endkey was pressed
Tested in Firefox to mimic ^PgDn, ^PgUp
Input, key, L1,{LCtrl}{PgUp}{PgDn}
If (ErrorLevel = "EndKey:PgUp") {
SendInput ^{PgUp}
}
Else If (ErrorLevel = "EndKey:PgDn") {
SendInput ^{PgDn}
}
Else If (ErrorLevel = "EndKey:LCtrl") {
Return ;assumed you just want to abort input with the control key
}
Else {
SendInput, ^%key%
}
Return

AutoHotKey KeyWait statements

I'm using AutoHotKey and I want to achieve something particular.
I have an hotkey that should perform a certain action, inside this hotkey, I would like to code something to detect if I only press the "C" key, or if I press "C" then "L" keys.
If there is only the "C" key pressed, then it should perform an action, otherwise , if "C" then "L" keys are pressed it should do another action.
But I can't do this as I don't really understand KeyWait, I mean how can I do something like that :
if(KeyWait, C){
firstAction
else {
if(KeyWait, C){
if(KeyWait, L){
anotherAction
}
}
Solved using the input function.
; Get one character and store it in the Character variable
Input, Character, L1
If Character = C
{
; Give up to half of one second to type L
Input, Character, L1 T0.5
If Character = L
MsgBox % "We have C and L"
else
MsgBox % "Just C here, give an L next time"
}

How do i write a loop that copies and paste from the clipboard

Im trying to write a loop that copies and paste from a clipboard i titled InitialWords.txt what i acutally want is for it to copy the words line by line and press the F2 key after each word. Heres what i tried-
Loop , read C:\InitialWords.txt
{
Loop , parse , A_LoopReadLine , %A_Tab%
{
clipboard= %A_LoopField%
send ^v
send {F2}
}
}
Thanks id appreciate any help.
It appears you were missing a comma after Loop, Read. You can also bypass using the clipboard altogether.
Loop, Read, C:\InitialWords.txt
{
Loop, Parse, A_LoopReadLine, %A_Tab%
{
Send %A_LoopField%
send {F2}
}
}
Loop, Read, InitialWords.txt
{
Loop, Parse, A_LoopReadLine, %A_Tab%
{
SendPlay % A_LoopField
Sleep % strLen(A_LoopField)*2
SendPlay, {F2}
Sleep 300
}
}

Konami Code on autohotkey

up up down down left right left right b a enter :: Msgbox, konami code.
is there a way to do this?
yes its actually pretty simple...
comb := "up|down|down|left|right|left|right|b|a|enter"
~up::
Loop, parse, comb, |
{
input, var,T.900,{%a_loopfield%}
if inStr(ErrorLevel, "Timeout")
return
}
msgbox Konami Code!!!
return
The first "up" is the one that will trigger the sequence hence only one "up" in the combination variable.
you can change the combination to whatever you want, but then you would have to change the hotkey to the first "key" that you want to press.