Where the heck is the AnyKey?
Since it has been asked in Ask for help lately i decided to post my solution here aswell. It just came over me...
Code:
; da AnyKey.ahk
; (c) 2k8 derRaphael / zLib License Style release
Gosub, Start
Gui, Add, Text,, % "You'll only see this GUI, "
. "when no Key was pressed while asked for`n`n"
. "You'd better press that anykey!"
Gui, Add, Button, gTest1 wp, Map any key to trigger hotkey CTRL+F12
Gui, Add, Button, gTest2 wp, Map any key to access label Testing
Gui, Add, Button, gTest3 wp, Map any key as at script's startup
Gui, Show,, DUH! AnyKey not found, uh`?
return
Testing:
MsgBox This is label "Testing"
return
Test1:
MsgBox,0,Test #1, % "This test maps our anykey to hardcoded`n"
. "Hotkey CTRL+F12 once with no Timeout`n"
. "and no PopupWindow"
AnyKey("^f12",0,"")
return
Test2:
MsgBox,0,Test #2, % "This test maps anykey to label Testing`n"
. "in source for 10 seconds and no PopupWindow"
res := AnyKey("Testing",10,"")
if (res=="Timeout") {
MsgBox,0,Time's up,You didn't press the anykey in 10 seconds
}
return
Test3:
MsgBox,0,Test #3,% "This test is just repeating script's startup anykey:`n"
. "5 seconds Timeout and custom Popup"
Start:
res := AnyKey("GuiClose",5,"Press any Key to exit this App")
if (res=="Timeout") {
MsgBox,0,Time's up,You didn't press the anykey in 5 seconds
}
return
^f12::
MsgBox Hotkey CTRL+F12
return
GuiClose:
ExitApp
return
AnyKey:
; Default Label for AnyKey Action
Return
AnyKey( Label="AnyKey", Duration=0, sText="Press the anykey..." ) {
if (sText!="") {
SplashTextOn,,, % sText
}
defReturnValue := 1
If (Duration) AND ( (Duration is Integer) OR (Duration is Float) ) {
D := "T" Duration
}
EndKeys := "{LControl}{RControl}{LShift}{RShift}{LAlt}{RAlt}"
. "{LWin}{RWin}{LAlt}{RAlt}{AppsKey}"
Input, AnyKey, L1 %D%, % EndKeys
defReturnValue := AnyKey
if (sText!="") {
SplashTextOff
}
If (InStr(ErrorLevel,"Endkey")) {
defReturnValue := SubStr(ErrorLevel,StrLen("EndKey:")+1)
} else If (ErrorLevel=="Timeout") {
defReturnValue := "Timeout"
}
If ( (Label) AND (defReturnValue!="Timeout") AND (Islabel(Label)) ) {
Gosub, % Label
} else {
Return, % defReturnValue
}
}
this code - the anykey function handles most of what you desire
basically it just shows how anykey can be used
popup and custom text aswell as anykey on time are quite
simple to handle
as the demo source shows, even already hardcoded hotkey actions can be mapped to anykey
a classic "whoever finds this one usefull ... " script
greets
derRaphael
Edit: fixed a small bug in function
Update:
i compressed the code a lil more still having above functionality
Code:
AnyKey( L="AnyKey",D=0,sT="Press the anykey..." )
{ ; da AnyKey.ahk stuffed / (c) 2k8 derRaphael / zLib License Style released
ifNotEqual,sT,,SplashTextOn,,,%sT%
D:=((D)&&((D is Integer)||(D is Float)))? "T" D:""
Input,AK,L1 %D%,% "{LCtrl}{RCtrl}{LShift}{RShift}{LAlt}{RAlt}{LWin}{RWin}{AppsKey}"
e:=ErrorLevel,V:=(InStr(e,"Endkey"))? SubStr(e,8):((e="Timeout")? e:AK),I:=Islabel(L)
ifNotEqual,sT,,SplashTextOff
IfNotInString,V,e,IfNotEqual,L,,IfNotEqual,I,0,Gosub, %L%
Return, V
}
code can be used as with given examples from above. also it can be included or put into standard lib, so in your scripts, it'll be only one line
Code:
AnyKey(MyLabelToJumpOn,TimeActive,CustomText)
when everything ommited in parameters you add anykey functionality, displaying "Press the anykey..." and jumping to the label AnyKey: on activation
greets
derRaphael