1

我是 AutoHotKey 的新手,不熟悉它的语法。我在网上搜索过,但找不到有用的例子。

看看下面的图表。

+---+---+---+---+-------------------
| a | b | c | d |                         (1)
+---+---+---+---+-------------------
              ^
              |


+---+---+---+---+-------------------
| a | b | c | d |                         (2)
+---+---+---+---+-------------------
          ^
          |


+---+---+---+---+-------------------
| a | b | c | d |                         (3)
+---+---+---+---+-------------------
  ^
  |

+---+---+---+---+-------------------
| a | b | c | d |                         (4)
+---+---+---+---+-------------------
              ^
              |

我想记录一些字符串并在其中导航。例如,在程序启动时创建了一个数组 my_array。如果用户按下 ctrl+c,那么选定的文本(假设它是字符串 a)被复制到剪贴板,并且它也被附加到 my_array。如果用户按下另一个 ctrl+c,则 b 被复制到剪贴板,并且它也被附加到 my_array。参见 (1),现在 a、b、c、d 已附加到 my_array 并且 d 在剪贴板中。现在,如果用户按下 alt+left_arrow,则 c 被复制到剪贴板,参见 (2)。现在,如果用户按 alt+left_arrow 2 次,a 在剪贴板中,参见 (3)。用户可以按 alt+right_arrow 3 次返回剪贴板中的 d,参见 (4)。此时,用户仍然可以按 ctrl+c 将数据附加到 my_array 并按 alt+left_arrow 或 alt+right_arrow 在数组中移动以获取数据。

实际上对我来说这很容易用其他一些熟悉的语言实现,但我很难在 AutoHotKey 中实现它。任何人都可以帮忙吗?

提前致谢。

4

2 回答 2

1

这是将复制的 Excel 数据放入数组的示例。

MyClipBoard=%ClipBoard% ; Remove any non-text (format) data

Loop, parse, MyClipBoard, `n,`r ; Place Excel content from clipboard in 2 dimentional Array
{ ; Start-Loop Read through each Line (record) in the ClipBoard data from Excel
  FullRecord:=A_LoopField ; Put a whole row in the variable FullRecord
  RecordNumber:=A_Index ; Store the current record (row) number.
  Loop, parse, FullRecord, %A_Tab% ; Parse the row content (FullRecord) in separate fields
  { ; Start-Loop Read through each Field in the ClipBoard data from Excel
    FieldNumber:=A_Index ; Store the current Field number.
    Array%RecordNumber%_%FieldNumber% := A_LoopField ; Array1_1 is Excel Row 1 column A, Array1_2 = Excel Row 1 column B
  } ; End-Loop Read through each Field in the ClipBoard data from Excel
} ; End-Loop Read through each Line (record) in the ClipBoard data from Excel

如果您需要更多示例来帮助您前进,请告诉我。
好的,这里有更多示例,您可以通过查看其他答案中的示例来清理它,例如使用 Variable++ 而不是 Variable +=1

; Read data from text file into one dimentional Array
ArrayCount = 0
Loop, Read, %A_ScriptDir%\SearchTerms.txt ; This loop retrieves each line from the file, one at a time.
{
    ArrayCount += 1  ; Keep track of how many items are in the array.
    Array%ArrayCount% := A_LoopReadLine ; Store this line in the next array element.
}


TextCounter = 1
IniWrite, %TextCounter%, C:\Temp\SearchTerms.ini, Counter, Nr ; Write counter in ini so I can restart at latest counter

SearchText:= Array1 ; Put data from Array1 into variable SearchText
MouseClick, left
Send, %SearchText%{Enter}
SplashTextOn, 200, 20,Searchterm,F%TextCounter% %SearchText%
WinMove, Searchterm, , , 0
Return

Browser_Favorites::
IniRead, TextCounter, C:\Temp\SearchTerms.ini, Counter, Nr ; Read latest counter
TextCounter += 1
IniWrite, %TextCounter%, C:\Temp\SearchTerms.ini, Counter, Nr
SearchText:=Array%TextCounter% ; Put data from Array+number into variable SearchText

; Examples with Array of 2 deep.

MyClipBoard=%ClipBoard% ; Remove any non-text (format) data

Loop, parse, MyClipBoard, `n,`r ; Place Excel content from clipboard in 2 dimentional Array
{ ; Start-Loop Read through each Line (record) in the ClipBoard data from Excel
  FullRecord:=A_LoopField ; Put a whole row in the variable FullRecord
  RecordNumber:=A_Index ; Store the current record (row) number.
  Loop, parse, FullRecord, %A_Tab% ; Parse the row content (FullRecord) in separate fields
  { ; Start-Loop Read through each Field in the ClipBoard data from Excel
    FieldNumber:=A_Index ; Store the current Field number.
    Array%RecordNumber%_%FieldNumber% := A_LoopField ; Array1_1 is Excel Row 1 column A, Array1_2 = Excel Row 1 column B
  } ; End-Loop Read through each Field in the ClipBoard data from Excel
} ; End-Loop Read through each Line (record) in the ClipBoard data from Excel



DLCounter:=1
DLSub:=1
DLData:=Array%DLCounter%_1
While (DLData <> "")
{
    While (DLData <> "")
    {
        MsgBox, %DLData%
        DLSub += 1
        DLData:=Array%DLCounter%_%DLSub%
    }
    DLSub:=1
    DLCounter += 1
    DLData:=Array%DLCounter%_%DLSub%
}
Return

它可能不是最干净的代码,但它会让你知道你能做什么。

于 2013-01-29T08:40:24.113 回答
0

Autohotkey 没有数组,但您可以像这样模拟一个:

global counterArray := 1
localCounter := counterArray
YourArray%localCounter %:= your_value

当您向数组添加新元素时;

counterArray++
anotherLocalCounter := counterArray
YourArray%localCounter%:= your_value

您也可以将此代码放入一个函数中,它会自增

ArrayExpand()
{
counterArray++
counter :=  counterArray
YourArray%counter %:=
}

重要提示:YourArray 不能是全局的,YourArray%counter% 中的计数器不能是全局的,其余的无关紧要。

于 2013-01-29T08:20:34.233 回答