Create HotKey inside a function (AutoHotKey) - function

So, I want my function to create a HotKey and returns the corresponding text of the hotkey when i press
here is my code below
global Object := {a:[1,"a","alexa"],b:[2,"b","battle"]}
global key_var1 :="!a"
global key_var2 := "!b"
create(key)
{
HotKey, %key%, myKey
return
myKey:
MsgBox, % Object.key[3]
return
}
create(key_var1)
create(key_var2)
The issue here is, when i press the hotkey, the message box displays nothing just empty.
When i press the HotKey the message box must display the corresponding text inside my Object array (text is in position 3)

Displays associative array element keyed to current hotkey:
global Object := {"!a":[1,"a","alexa"], "!b":[2,"b","battle"]}
global key_var1 := "!a"
global key_var2 := "!b"
create(key)
{
HotKey, %key%, myKey
return
myKey:
MsgBox, % A_ThisHotkey ":" Object[A_ThisHotkey][3]
return
}
create(key_var1)
create(key_var2)
Output:

Related

AutoHotkey - Building a Clipboardsaving Function

What I want to do is to build a function, that I can use to paste something (in this case URLs). Normally I would just use the send command or the sendinput command, but they are kind of slow which is a little bit annoying. That's why I want to avoid it and use the clipboard instead.
Here my function:
ClipPaster(CustomClip){
ClipSaved := ClipboardAll ;Saving the current clipboard
Clipboard := %CustomClip% ;Overwriting the current clipboard
Send, ^{v}{Enter} ;pasting it into the search bar
Clipboard := Clipsaved ;Recovering the old clipboard
}
Here how I'm using the function:
RAlt & b::
Send, ^{t} ;Open a new tab
ClipPaster("chrome://settings/content/images") ;Activating my clipboard
return
RAlt & g::
Send, ^{t} ;Open a new tab
ClipPaster("https://translate.google.com/#en/es/violin") ;Activating
my clipboard function
return
Then when I'm trying to use the function. I get an error:
Error: The following variable name contains an illegal character: "chrome://settings/content/images"
Line:
-->1934: Clipboard := %CustomClip%
What am I doing wrong here?
You get this error message because
Variable names in an expression are NOT enclosed in percent signs.
https://www.autohotkey.com/docs/Variables.htm#Expressions
ClipPaster(CustomClip){
ClipSaved := ClipboardAll ; Saving the current clipboard
Clipboard := "" ; empty the clipboard (start off empty to allow ClipWait to detect when the text has arrived)
; Variable names in an expression are NOT enclosed in percent signs:
Clipboard := CustomClip ; Overwriting the current clipboard
ClipWait 1 ; wait max. 1 second for the clipboard to contain data
if (!ErrorLevel) ; If NOT ErrorLevel clipwait found data on the clipboard
Send, ^v{Enter} ; pasting it into the search bar
Sleep, 300
Clipboard := Clipsaved ; Recovering the old clipboard
ClipSaved := "" ; Free the memory
}
https://www.autohotkey.com/docs/misc/Clipboard.htm#ClipboardAll

Autohot key variable function not global

I'm trying to make a function that takes coordinates, copies that field and name a variable, but the variable seems to be local and doesn't transfer out of the function. The coordinates (x,y) and delay seem to be working fine, but the desc variable always comes out blank. Please help!
newest(x, y, delay, variable)
{
sleep, %delay%
click, %x%,%y% ;desc machine field
clipboard = ; empty the clipboard
Loop
{
If GetKeyState("F2","P") ; terminate the loop whenever you want by pressing F2
break
Send {Ctrl Down}c{Ctrl Up}
if clipboard is not space
break ; Terminate the loop
}
variable := clipboard ;
msgbox %variable%
return
}
^k::
newest(654, 199, 200, desc)
msgbox %desc%
return
From the function's point of view, parameters are essentially the same
as local variables unless they are defined as ByRef.
ByRef makes one parameter of the function an alias for a variable, allowing the function to assign a new value to this variable.
newest(x, y, delay, ByRef variable){
sleep, %delay%
click, %x%,%y% ; desc machine field
clipboard = ; empty the clipboard
Loop
{
If GetKeyState("F2","P") ; terminate the loop whenever you want by pressing F2
break
Send {Ctrl Down}c{Ctrl Up}
If clipboard is not space
break ; Terminate the loop
}
variable := clipboard
msgbox %variable%
return
}
^k::
newest(54, 199, 200, desc)
msgbox %desc%
return

Create a list of Button in Cocos2dx

When I create a list of button and I addTouchEventListener for it, like below code
for (int i = 0; i < btmPlay.size(); i++ )
{
btmPlay.at(i)->addTouchEventListener([&](Ref *sender, ui::Widget::TouchEventType type){
if (type == ui::Widget::TouchEventType::ENDED)
{
CCLOG("%i", i);
}
});
}
when I touch to the first button, the result is 12 ( btmPlay.size() = 13).
What errors?
In your closure, you're capturing the variable i by reference, and that's why clicking any button will print the same value, in this case 12. If, instead, you capture the variable i by value (by replacing [&] with [=]) then each button will print a different value in the range 0-12.
Btw, capturing i by reference in your example is also wrong, because by the time the closure is invoked, the variable is already out of scope, and printing it is UB.

Dynamically Create AutoHotkey Hotkey to Function/Subroutine

The AutoHotkey command Hotkey allows for the creation of dynamic hotkeys at runtime, but its syntax and documentation seems to limit it to built-in or existing labels/subroutines, which makes it much less useful:
Hotkey, KeyName [, Label, Options]
Is there a way to get it to work like regular, hard-coded hotkeys? For example:
#z::MsgBox foobar ; Typical, hard-coded hotkey pops up a message-box
Hotkey, z, MsgBox foobar ; Nope; complains about missing label “MsgBox foobar”
It looks like it might be possible due to the following line from the manual, however it is not clear how it would work:
Label - Both normal labels and hotkey/hotstring labels can be used.
This is a refinement of FakeRainBrigand's answer. It is used exactly the same:
Hotkey("x", "Foo", "Bar") ; this defines: x:: Foo("Bar")
Changes from the original:
Prevent accidental auto-execute of the handler subroutine by tucking it into the function.
Allowing me to reduce namespace pollution by narrowing the scope of the hotkeys variable from global to static.
Optimizations: fun is looked up only once (using Func()) at hotkey definition time; At invocation time, object lookups reduced four to two by splitting hotkeys into two objects funs and args;
This still relies of course on the _L version of AutoHotKey because of Object notation and variadic arg* syntax.
Hotkey(hk, fun, arg*) {
Static funs := {}, args := {}
funs[hk] := Func(fun), args[hk] := arg
Hotkey, %hk%, Hotkey_Handle
Return
Hotkey_Handle:
funs[A_ThisHotkey].(args[A_ThisHotkey]*)
Return
}
Doing exactly what you want isn't possible in AutoHotkey. This is the closest way I can think of.
Call this file Hotkeys.ahk, and put it in My Documents/AutoHotkey/Lib. Alternatively make a folder called Lib, and put it in the same directory as your main script.
Hotkeys := {}
Hotkey(hk, fun, p*) {
global hotkeys
hotkeys[hk] := {}
hotkeys[hk].fun := fun
hotkeys[hk].p := p
Hotkey, %hk%, HandleHotkey
}
HandleHotkey:
hotkeys[A_ThisHotkey].fun(hotkeys[A_ThisHotkey].p*)
return
Here's an example script that you could use it with.
Hotkey("e", "msgbox", "foobar")
MsgBox(msg) {
msgbox % msg
}
#Include <Hotkeys>
The first parameter is the hotkey, the second is the function to call, and everything after that is passed to the function.
Is this what you are looking for?
#Persistent
#SingleInstance Force
#installKeybdHook
Hotkey, #z, MyLabel
MyLabel:
MsgBox,OK
Return
With newer ahk version you can now use functions as label argument. See https://www.autohotkey.com/docs/commands/Hotkey.htm

space-bar increments string value

how to handle space-bar in taking input from user in a string.my code is
btnSearch.addEventListener(MouseEvent.CLICK, getData);
function getData(event:MouseEvent)
{
var input:String = textfieldName.text;
if((input != null)&&(input.length != 0)&&(input != ""))
{
func(input);
}
}
when user hits backspace, func is called and input string length increased as many times as the spacebar is clicked.
what you have to di first is to validate the input, e.g. you can trim all white space (you could also do it on the textfield e.g. by applying the allowed characters to the restrict property
textfieldName.restrict = "0-9";//this will restrict to only integer values
to strip in your getData handler you could do as follows:
//trim begin
while(input && input.charAt(0) == " ")
{
input = input.substr(1);
}
placed after you read the input value from textfield.
best regards