I'm trying to write a script to store the value of the current clipboard to a variable, where I can do this with multiple variables.
The idea is that I can "paste" multiple pieces of text by storing them to multiple variables with clipboard.
#Requires AutoHotkey v2.0
varNumpadEnd := "1"
varNumpadDown := "2"
varNumpadPgDn := "3"
^NumpadEnd::{
A_Clipboard := ""
Send "^c"
ClipWait
varNumpadEnd := A_Clipboard
}
NumpadEnd::{
Send varNumpadEnd
}
NumpadDown::{
Send varNumpadDown
}
NumpadPgDn::{
Send varNumpadPgDn
}
The script doesn't seem to be updating the variables. When I press ^NumpadEnd the script does copy the selected text, I know this because when I press ctrl + v it does paste the previously selected text.
However, when I press NumpadEnd, it will always "paste" "1".
Help would be appreciated, it seems to me that the script is unable to update the variable (maybe it's a scope issue?).
I tried to remove the variable declaration on the top, but I would be faced with error when starting the script, stating that the variables have not been assigned a value.
The reason that this is not working is that in v2 all hotkeys are treated as a function and so all variables within the braces are local. You can achieve your desired effect without creating global variables like this:
*NumpadEnd:: {
static varNumpadEnd := 1
if (GetKeyState("Ctrl", "P")) {
old := A_Clipboard
A_Clipboard := ""
Send("^c")
ClipWait
varNumpadEnd := A_Clipboard
A_Clipboard := old
}
else {
old := A_Clipboard
A_Clipboard := ""
A_Clipboard := varNumpadEnd
ClipWait
Send("^v")
Sleep(100)
A_Clipboard := old
}
}
And if you want to combine all three:
*NumpadEnd::
*NumpadDown::
*NumpadPgDn:: {
static varNumpadEnd := 1, varNumpadDown := 2, varNumpadPgDn := 3
static __Strip(keyName) {
return (RegExReplace(keyName, "[~*$+^! &]|AppsKey"))
}
if (GetKeyState("Ctrl", "P")) {
old := A_Clipboard
A_Clipboard := ""
Send("^c")
ClipWait
var%__Strip(A_ThisHotkey)% := A_Clipboard
A_Clipboard := old
}
else {
old := A_Clipboard
A_Clipboard := ""
A_Clipboard := var%__Strip(A_ThisHotkey)%
ClipWait
Send("^v")
Sleep(100)
A_Clipboard := old
}
}
The hotkey names (NumpadEnd, NumpadDown, ...) must match up with the static var names (varNumpadEnd, varNumpadDown, ...) for it to dereference A_ThisHotkey
properly.