AutoHotkey Community

It is currently May 27th, 2012, 4:04 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 12 posts ] 
Author Message
PostPosted: September 1st, 2009, 2:29 am 
Offline

Joined: December 21st, 2008, 7:29 pm
Posts: 181
This has been discussed on the forum before but I think it's worth bringing up again.

You can make a hotkey that calls a function, like this:

Code:
!a::MyFunction()


but you can't do this

Code:
Hotkey, !a, MyFunction()


instead you have to do something like this

Code:
Hotkey, !a, MyLabel

MyLabel:
    MyFunction()
Return



Also, any ideas on how difficult it would be to code?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 1st, 2009, 7:01 am 
That's an interesting perspective. However, !a::MyFunction() is actually a label, its first line of code and a "hotkey instruction" (call this label when !a is pressed) in one.

I think it would be difficult to code.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: September 1st, 2009, 8:34 am 
Offline
User avatar

Joined: August 30th, 2005, 8:43 pm
Posts: 8667
Location: Salem, MA
+1

Also, it'd be nice if Gui gLables caoud call functions (or make a new fFunction() option)

_________________
Image
(Common Answers) - New Tutorials Forum - Humongous FAQ


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 1st, 2009, 11:18 am 
Offline
User avatar

Joined: October 7th, 2006, 8:45 am
Posts: 3330
Location: Simi Valley, CA
You can script a 'lookup table' which holds hotkey names along with their associated function. Then, assign all hotkeys to one label, which gets the function name from the lookup table (using A_ThisHotkey) and calls the function dynamically.

A similar setup could work for gui g-labels, though any workaround would be much less elegant than simply specifying a function in the gui control's options (or instead of the hotkey label).

_________________
Ternary (a ? b : c) guide     TSV Table Manipulation Library
Post code inside [code][/code] tags!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 1st, 2009, 1:34 pm 
Offline
User avatar

Joined: August 30th, 2005, 8:43 pm
Posts: 8667
Location: Salem, MA
A good example of that is the MenuHandler example in the Menu docs.

I just AHK could be a little more function centric sometimes.

_________________
Image
(Common Answers) - New Tutorials Forum - Humongous FAQ


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 1st, 2009, 4:33 pm 
Offline

Joined: December 21st, 2008, 7:29 pm
Posts: 181
[VxE] wrote:
You can script a 'lookup table' which holds hotkey names along with their associated function. Then, assign all hotkeys to one label, which gets the function name from the lookup table (using A_ThisHotkey) and calls the function dynamically.

A similar setup could work for gui g-labels, though any workaround would be much less elegant than simply specifying a function in the gui control's options (or instead of the hotkey label).


Lol, I use a similar workaround.

I have 10 hotkeys that are registered at runtime with the Hotkey command, they all send different key combinations to a window, and I have a function to send whatever keys I want to the window.

MyHotkey(1-10) is the Hotkeys
KeysToSend(1-10) is the corresponding keys to send

Code:
SendSomeKeys:
    Loop, 10   ; Loop through the ten possible hotkeys
    {
        TempHotkey := vMyHotkey%A_Index%           ;vMyHotkey(1-10) stores the Hotkey combinations       
        If (TempHotkey = A_ThisHotkey)
        {
            TempKeysToSend := vKeysToSend%A_Index%  ;vKeysToSend(1-10) stores what keys will be sent
            TempKeysToSend = "%TempKeysToSend%"     ;In order for it to work correctly it must be enclosed in quotes
            SendToMyWindow(TempKeysToSend)
            Break                                   ;Break exits the Loop
        }

    }
return


Report this post
Top
 Profile  
Reply with quote  
PostPosted: June 7th, 2011, 8:29 am 
Offline

Joined: September 15th, 2006, 10:25 am
Posts: 567
entropic wrote:
This has been discussed on the forum before but I think it's worth bringing up again.

You can make a hotkey that calls a function, like this:

Code:
!a::MyFunction()


but you can't do this

Code:
Hotkey, !a, MyFunction()


instead you have to do something like this

Code:
Hotkey, !a, MyLabel

MyLabel:
    MyFunction()
Return



Also, any ideas on how difficult it would be to code?


Bump. I am not sure if i have missed any recent discussion on this.

_________________
If i've seen further it is by standing on the shoulders of giants

my site | ~shajul | WYSIWYG BBCode Editor


Report this post
Top
 Profile  
Reply with quote  
PostPosted: June 7th, 2011, 9:20 am 
Offline
User avatar

Joined: May 18th, 2010, 3:10 pm
Posts: 1179
Location: Sweden
shajul wrote:
Bump. I am not sure if i have missed any recent discussion on this.


Although the workaround works (obviously), it is not as elegant. I think an improvement on this part would help both novices and pros alike.

_________________
~sumon Appifyer AHK Nova halted Recommended: AHK_L (Why?)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 7th, 2011, 5:43 pm 
Offline

Joined: May 3rd, 2009, 7:16 pm
Posts: 345
Location: OH, USA
Label names can contain brackets. Thus, there's no way to distinguish between label names and function names:

Code:
Hotkey, !a, MyFunction()
return

MyFunction():
   MsgBox Oops, I'm a label. Better luck next time.
return

This could happen for v2 but it wouldn't be possible for v1 without breaking backward compatibility.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 7th, 2011, 6:21 pm 
Offline

Joined: March 10th, 2008, 12:55 am
Posts: 1907
Location: Minnesota, USA
Backward compatibility has been broken for many things already.

and well, there could be function detection. if string (label) contains a ( and ), do function stuff instead.
same with menus.

as for GUI's, fFunction() would be nice.

_________________
rawr. be very afraid
*poke*
Note: My name is all lowercase for a reason.
"I think Bigfoot is blurry, that's the problem. It's not the photographer's fault, Bigfoot is blurry. So there's a large, out-of-focus monster roaming the countryside."


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 7th, 2011, 6:43 pm 
Offline

Joined: May 3rd, 2009, 7:16 pm
Posts: 345
Location: OH, USA
tidbit wrote:
Backward compatibility has been broken for many things already.

Not many things. Just [ ] and ? characters in variable names and syntax validation. Which are both justified and very minor changes. I'm speaking purely from an ANSI perspective as Unicode comes with it's own costs.

tidbit wrote:
and well, there could be function detection. if string (label) contains a ( and ), do function stuff instead.
same with menus.

Well, it would break compatibility for anyone using ( or ) in their label names. In this case, I don't believe it's justified to break compatibility. Unless somehow adding the ability to pass parameters along with the function call, it would have no benefit over the workarounds mentioned above. I don't think a few extra lines are going to kill you.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 8th, 2011, 4:21 pm 
Offline

Joined: October 13th, 2009, 10:09 pm
Posts: 1389
I don't think those characters should have been allowed in label names at the first place because they have other meanings.


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

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 6 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