AutoHotkey Community

It is currently May 26th, 2012, 2:35 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: January 11th, 2008, 2:06 am 
Offline

Joined: November 23rd, 2007, 10:23 am
Posts: 841
Location: ~/.
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

_________________
Image
    All scripts, unless otherwise noted, are hereby released under CC-BY


Last edited by derRaphael on January 11th, 2008, 11:24 am, edited 3 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 11th, 2008, 3:50 am 
Offline
User avatar

Joined: December 21st, 2007, 3:14 pm
Posts: 3826
Location: Louisville KY USA
you are beautiful.... man?
never know on the internet
how can any solution possibly so... funny....no... clever..... i mean there is some real simple genious behind the script but well "ANY" key i mean wow of all the absolutely sick sick scripting triumphs i am forever your follower you are my new god

_________________
No matter what your oppinion Please join this discussion
Formal request to Polyethene
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 11th, 2008, 5:20 am 
Offline

Joined: November 23rd, 2007, 10:23 am
Posts: 841
Location: ~/.
tank - this is nothing really new since main work is done from the input command and has been demonstrated quite a few times here.

this script still can be improved: it does not provide anything similar to the #ifwinactive directive for example

it was just few minutes work and is a nice demonstration of how passed labels can not only be checked, but also reused. it also shows, that hardcoded hotkey assginments are nothing else than labels, too

so far nothing special about this script. it simply adds a function which might come in handy for whoever has the need of anykey


greets
derRaphael


ps and no im not a god just an more or less advanced n00b!
-----
than more i learn, then better í understand how little i know

_________________
Image
    All scripts, unless otherwise noted, are hereby released under CC-BY


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 11th, 2008, 11:22 am 
Offline

Joined: November 23rd, 2007, 10:23 am
Posts: 841
Location: ~/.
stdlib update - see 1st link

_________________
Image
    All scripts, unless otherwise noted, are hereby released under CC-BY


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 11th, 2008, 2:06 pm 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7501
Location: Australia
Quote:
Code:
D:=((D)&&((D is Integer)||(D is Float)))? "T" D:""
if var is [not] type wrote:
Note: The operators "between", "is", "in", and "contains" are not supported in expressions.

In an expression, (D is Integer) concatenates the values of three variables: D, is and Integer.

Rather than (the erroneous)
Code:
((D is Integer)||(D is Float))
you could use
Code:
if D is number
or in an expression
Code:
(D+0 != "")


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 11th, 2008, 2:14 pm 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
HOME,PRINT, SCROLL etc keys do not work

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 22 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group