InputHook

キーボード入力の収集や傍受に使用できるオブジェクトを作成します。

InputHookObj := InputHook(Options, EndKeys, MatchList)

パラメータ

Options

型:文字列

A string of zero or more of the following options (in any order, with optional spaces in between):

B: Sets BackspaceIsUndo to 0 (false), which causes Backspace to be ignored.

C: Sets CaseSensitive to 1 (true), making MatchList case-sensitive.

I: Sets MinSendLevel to 1 or a given value, causing any input with send level below this value to be ignored. 例えば、I2は、レベルが0(デフォルト)または1の入力は無視し、レベル2の入力を捕捉することができます。

L: Length limit (e.g. L5). 入力の最大許容長を示します。When the text reaches this length, the Input is terminated and EndReason is set to the word Max (unless the text matches one of the MatchList phrases, in which case EndReason is set to the word Match). 未指定の場合、長さの上限は1023となります。

L0を指定すると、テキストの収集と長さの制限が無効になりますが、どのキーがテキストを生成しているとカウントされるかは影響しません(「VisibleText」を参照)。This can be useful in combination with OnChar, OnKeyDown, KeyOpt or the EndKeys parameter.

M: Modified keystrokes such as Ctrl+A through Ctrl+Z are recognized and transcribed if they correspond to real ASCII characters. Ctrl+Cを認識するこの例について考えてみましょう:

CtrlC := Chr(3)  ; CtrlCの文字をCtrlC varに格納します。
ih := InputHook("L1 M")
ih.Start()
ih.Wait()
if (ih.Input = CtrlC)
    MsgBox "You pressed Control-C."

Note: The characters Ctrl+A through Ctrl+Z correspond to Chr(1) through Chr(26). Also, the M option might cause some keyboard shortcuts such as Ctrl+ to misbehave while an Input is in progress.

T: Sets Timeout (e.g. T3 or T2.5).

V: Sets VisibleText and VisibleNonText to 1 (true). 通常、ユーザーの入力はブロックされます(システムから隠されます)。ユーザーのキー入力をアクティブウィンドウに送るには、このオプションを使用します。

*: Wildcard. Sets FindAnywhere to 1 (true), allowing matches to be found anywhere within what the user types.

E: Handle single-character end keys by character code instead of by keycode. これにより、アクティブウィンドウのキーボードレイアウトがスクリプトのキーボードレイアウトと異なる場合、より一貫した結果が得られます。It also prevents key combinations which don't actually produce the given end characters from ending input; for example, if @ is an end key, on the US layout Shift+2 will trigger it but Ctrl+Shift+2 will not (if the E option is used). If the C option is also used, the end character is case-sensitive.

EndKeys

型:文字列

0個以上のキーのリストで、どれかが押されると入力が終了します(終了キー自体は入力バッファに書き込まれません)。When an Input is terminated this way, EndReason is set to the word EndKey and EndKey is set to the name of the key.

EndKeys uses a format similar to the Send function. 例えば、{Enter}.{Esc}と指定すると、Enter.Escのいずれかが入力を終了させることになる。中括弧そのものを終了キーとして使用する場合は、{{}{}}を指定します。

To use Ctrl, Alt, or Shift as end keys, specify the left and/or right version of the key, not the neutral version. 例えば、{Control}ではなく、{LControl}{RControl}を指定します。

Although modified keys such as Alt+C (!c) are not supported, non-alphanumeric characters such as ?!:@&{} by default require Shift or AltGr to be pressed or not pressed depending on how the character is normally typed. If the E option is present, single character key names are interpreted as characters instead, and in those cases the modifier keys must be in the correct state to produce that character. When both the E option and M option are used, Ctrl+A through Ctrl+Z are supported by including the corresponding ASCII control characters in EndKeys.

vkFF}や {sc001}のような明示的なキーコードを指定することも可能です。これは、キーに名前がなく、押しても何も表示されないような場合に便利です。Its key code can be determined by following the steps at the bottom of the key list page.

MatchList

型:文字列

A comma-separated list of key phrases, any of which will cause the Input to be terminated (in which case EndReason will be set to the word Match). The entirety of what the user types must exactly match one of the phrases for a match to occur (unless the * option is present). In addition, any spaces or tabs around the delimiting commas are significant, meaning that they are part of the match string. For example, if MatchList is ABC , XYZ, the user must type a space after ABC or before XYZ to cause a match.

カンマが2つ続くと、文字通りのカンマ1つとなります。例えば、次のようにすると、文字列の末尾にリテラルコンマが1つ入ることになります:string1,,,string2. 同様に、次のリストには、その中にリテラルなカンマを持つ項目が1つだけ含まれています:single,,item.

MatchListの項目は個々のパラメータとして扱われないため、リストをすべて変数に格納することができます。例えば、MatchListList1 "," List2 "," List3からなり、それぞれの変数にマッチフレーズの大きなサブリストが含まれていることがあります。

Input Stack

InputHookオブジェクトは、いつでもいくつでも作成でき、進行中ですが、開始する順番によって、入力の収集方法に影響があります。

Startメソッドで入力が開始されると、スタックの一番上に置かれ、入力が終了したときだけスタックから取り除かれます。キーボードイベントは、最近開始されたものから順に各Inputに渡されます。入力が与えられたキーボードイベントを抑制する場合、そのイベントはそれ以上スタック下に渡されません。

送信されたキーストロークの送信レベルがInputHookのMinSendLevel以下の場合、送信されたキーストロークは無視されます。このような場合、キーストロークは、スタックの下位にある入力によって処理される可能性がある。

複数のInputHooksをMinSendLevelと組み合わせて使用することで、送信されたキーストロークと実際のキーストロークの両方を個別に収集することができます。

InputHook Object

InputHook関数は、以下のメソッドとプロパティを持つInputHookオブジェクトを返します。

"InputHookObj" is used below as a placeholder for any InputHook object, as "InputHook" is the class itself.

メソッド

KeyOpt

キーまたはキーのリストに対するオプションを設定します。

InputHookObj.KeyOpt(Keys, KeyOptions)

パラメータ

Keys

型:文字列

キーの一覧です。中括弧は、送信機能と同様に、キー名、仮想キーコード、スキャンコードを囲むために使用されます。例えば、{Enter}.{{}は、Enter, . および {に適用されます。キーを名前、{vkNN}、または{scNNN}で指定すると、3つの異なる結果が得られます;詳細は以下を参照してください。

Specify the string {All} (case-insensitive) on its own to apply KeyOptions to all VK and all SC, including {vkE7} and {sc000} as described below. その後、KeyOptを2回目に呼び出すと、特定のキーからオプションを削除することができます。

Specify {sc000} to apply KeyOptions to all events which lack a scan code.

Specify {vkE7} to apply KeyOptions to Unicode events, such as those sent by SendEvent "{U+221e}" or SendEvent "{Text}∞".

KeyOptions

型:文字列

以下の1文字オプションのうち1つ以上(スペースとタブは無視されます)。

- (minus):-に続くオプションのうち、次の+まで削除します。

+ (plus):それ以外の場合は、何の効果もなく、以前の-をキャンセルします。

E: End key. If enabled, pressing the key terminates Input, sets EndReason to the word EndKey and EndKey to the key's normalized name. Unlike the EndKeys parameter, the state of Shift or AltGr is ignored. For example, @ and 2 are both equivalent to {vk32} on the US keyboard layout.

I: Ignore text. このキーによって通常生成されるテキストはすべて無視され、キーは非テキストキーとして扱われる(VisibleNonText参照)。そのキーが通常テキストを生成しない場合、効果はありません。

N: Notify. キーが押されるたびにOnKeyDownおよびOnKeyUpコールバックが呼び出されるようにします。

S: Suppresses (blocks) the key after processing it. Sが使用されるまでは、VisibleTextまたはVisibleNonTextに優先します。+S-Vを意味します。

V: Visible. キーが抑制される(ブロックされる)のを防ぐ。Vが使用されるまで、VisibleTextまたはVisibleNonTextをオーバーライドします。+V-Sを意味します。

備考

Options can be set by both virtual key code (VK) and scan code (SC), and are accumulative.

When a key is specified by name, options are added either by VK or by SC. 2つの物理キーが同じVKを共有し、SCによって異なる場合(UpNumpadUpなど)、SCによって扱われます。一方、VK番号を使用する場合は、そのVKを生成するすべての物理キーに適用されます(これは、アクティブなキーボードレイアウトに依存するため、時間と共に変化する可能性があります)。

VK番号でオプションを削除しても、SCで設定されていたオプションには影響しませんし、その逆も同様です。ただし、キー名でオプションが削除され、そのキー名がVKで扱われる場合、対応するSCについてもオプションが削除されます(スクリプトのキーボードレイアウトに準じます)。これにより、すべてのキーにオプションを適用した後に、キー名を指定して除外することができます。

To prevent an option from affecting a key, the option must be removed from both the VK and the SC of that key, or sc000 if the key has no SC.

Unicode events, such as those sent by SendEvent "{U+221e}" or SendEvent "{Text}∞", are affected by options which have been set for either vkE7 or sc000. Any option applied to {All} is applied to both vkE7 and sc000, so to exclude Unicode events, remove the option from both. 事例:

InputHookObj.KeyOpt("{All}", "+I")  ; Ignore text produced by any event
InputHookObj.KeyOpt("{vkE7}{sc000}", "-I")  ; except Unicode events.

Start

入力の収集を開始します。

InputHookObj.Start()

入力がすでに進行している場合は、効果がありません。

新しく開始された入力は、InputHookスタックの最上位に置かれ、以前に開始された入力を上書きすることができます。

このメソッドは、キーボードフックをインストールします(まだインストールされていない場合)。

ストップ

入力を終了し、EndReasonに Stopped という単語を設定します。

InputHookObj.Stop()

入力中でない場合は、効果がありません。

Wait

Inputが終了するまで待機する(InProgressがfalse)。

EndReason := InputHookObj.Wait(MaxTime)

パラメータ

MaxTime

型:浮動小数点数

If omitted, the wait is indefinitely. Otherwise, specify the maximum number of seconds to wait. MaxTime秒後に Input がまだ進行している場合、このメソッドは戻り、Input は終了しません。

戻り値

型:文字列

This method returns EndReason.

一般特性

EndKey

Input を終了させるために押された終了キーの名前を返します。

KeyName := InputHookObj.EndKey

Note that EndKey returns the "normalized" name of the key regardless of how it was written in the EndKeys parameter. 例えば、{Esc}{vk1B}はどちらもEscapeを生成します。GetKeyNameを使用すると、正規化された名前を取得することができます。

E オプションが使用された場合、EndKey は入力された実際の文字を返します(該当する場合)。それ以外の場合は、スクリプトのアクティブなキーボードレイアウトに従って、キー名が決定されます。

EndKey は、EndReasonが "EndKey" でない場合、空文字列を返す。

EndMods

Input終了時に論理的にダウンしていたモディファイアの文字列を返します。

Mods := InputHookObj.EndMods

すべてのモディファイアが論理的にダウン(押された)した場合、完全な文字列は次のようになります:

<^>^<!>!<+>+<#>#

これらの修飾語は、ホットキーの場合と同じ意味を持ちます。各モディファイアは必ず < (左) または > (右)で修飾されます。対応するキー名は以下の通りです:LCtrl, RCtrl, LAlt, RAlt, LShift, RShift, LWin, RWin.

InStrは、与えられた修飾語(>!^など)が存在するかどうかをチェックするために使用することができます。次の行で、Mods^!+#のような中立的な修飾語の文字列に変換することができます:

Mods := RegExReplace(Mods, "[<>](.)(?:>\1)?", "$1")

一瞬のタイミングのため、このプロパティは、Inputが終了した直後やOnEndコールバックで使用されても、GetKeyStateよりも信頼性が高い場合があります。

EndReason

Input がどのように終了したかを示すEndReason の文字列を返す。

Reason := InputHookObj.EndReason

If the Input is still in progress, an empty string is returned.

InProgress

Returns 1 (true) if the Input is in progress, otherwise 0 (false).

Boolean := InputHookObj.InProgress

Input

前回Inputを起動してから収集したテキストを返します。

String := InputHookObj.Input

このプロパティは、Inputの実行中、または終了後に使用することができます。

Match

マッチリスト入力終了の原因となった項目を返します。

String := InputHookObj.Match

This property returns the matched item with its original case, which may differ from what the user typed if the C option was omitted, or an empty string if EndReason is not "Match".

OnEnd

Input終了時に呼び出される関数オブジェクトを取得または設定します。

MyCallback := InputHookObj.OnEnd
InputHookObj.OnEnd := MyCallback

MyCallback is the function object to call. An empty string means no function object.

The callback accepts one parameter and can be defined as follows:

MyCallback(InputHookObj) { ...

Although the name you give the parameter does not matter, it is assigned a reference to the InputHook object.

You can omit the callback's parameter if the corresponding information is not needed, but in this case an asterisk must be specified, e.g. MyCallback(*).

この関数は新しいスレッドとして呼び出されるため、SendModeDetectHiddenWindowsなどの設定にデフォルト値が設定され、新しい状態で開始されます。

OnChar

入力バッファに文字が追加された後に呼び出される関数オブジェクトを取得または設定します。

MyCallback := InputHookObj.OnChar
InputHookObj.OnChar := MyCallback

MyCallback is the function object to call. An empty string means no function object.

The callback accepts two parameters and can be defined as follows:

MyCallback(InputHookObj, Char) { ...

パラメータに与える名前は重要ではないが、以下の値が順次割り当てられる:

  1. A reference to the InputHook object.
  2. A string containing the character (or multiple characters, see below for details).

対応する情報が不要な場合、コールバックのパラメータリストの最後から1つ以上のパラメータを省略することができますが、この場合、MyCallback(Param1, *)のように、最後のパラメータとしてアスタリスクを指定する必要があります。

複数の文字が存在することは、最後のキー押下の前にデッドキーが使用されていたことを示しますが、2つのキーは1つの文字に変換することができませんでした。例えば、あるキーボードレイアウトでは、`eèを、`z`zを出力します。

終了キーが押されても、この関数が呼び出されることはありません。

OnKeyDown

通知可能なキーが押されたときに呼び出される関数オブジェクトを取得または設定します。

MyCallback := InputHookObj.OnKeyDown
InputHookObj.OnKeyDown := MyCallback

キーダウンの通知は、まずKeyOptまたはNotifyNonTextで有効化する必要があります。

MyCallback is the function object to call. An empty string means no function object.

コールバックは3つのパラメーターを受け取り、以下のように定義できる:

MyCallback(InputHookObj, VK, SC) { ...

パラメータに与える名前は重要ではないが、以下の値が順次割り当てられる:

  1. A reference to the InputHook object.
  2. An integer representing the virtual key code of the key.
  3. An integer representing the scan code of the key.

対応する情報が不要な場合、コールバックのパラメータリストの最後から1つ以上のパラメータを省略することができますが、この場合、MyCallback(Param1, *)のように、最後のパラメータとしてアスタリスクを指定する必要があります。

To retrieve the key name (if any), use GetKeyName(Format("vk{:x}sc{:x}", VK, SC)).

この関数は新しいスレッドとして呼び出されるため、SendModeDetectHiddenWindowsなどの設定にデフォルト値が設定され、新しい状態で開始されます。

終了キーが押されても、この関数が呼び出されることはありません。

OnKeyUp

通知可能なキーがリリースされたときに呼び出される関数オブジェクトを取得または設定します。

MyCallback := InputHookObj.OnKeyUp
InputHookObj.OnKeyUp := MyCallback

キーアップ通知は、まずKeyOptまたはNotifyNonTextで有効化する必要があります。キーが押されたときに、テキストとみなされるか非テキストとみなされるかは、キーが押されたときに判断されます。InputHookがキーダウンを検出せずにキーアップを検出した場合、非テキストとみなされます。

MyCallback is the function object to call. An empty string means no function object.

コールバックは3つのパラメーターを受け取り、以下のように定義できる:

MyCallback(InputHookObj, VK, SC) { ...

パラメータに与える名前は重要ではないが、以下の値が順次割り当てられる:

  1. A reference to the InputHook object.
  2. An integer representing the virtual key code of the key.
  3. An integer representing the scan code of the key.

対応する情報が不要な場合、コールバックのパラメータリストの最後から1つ以上のパラメータを省略することができますが、この場合、MyCallback(Param1, *)のように、最後のパラメータとしてアスタリスクを指定する必要があります。

To retrieve the key name (if any), use GetKeyName(Format("vk{:x}sc{:x}", VK, SC)).

この関数は新しいスレッドとして呼び出されるため、SendModeDetectHiddenWindowsなどの設定にデフォルト値が設定され、新しい状態で開始されます。

オプションプロパティ

BackspaceIsUndo

Backspaceが、最近押された文字をInputバッファの最後から削除するかどうかを制御します。

CurrentSetting := InputHookObj.BackspaceIsUndo
InputHookObj.BackspaceIsUndo := NewSetting

CurrentSetting is NewSetting if assigned, otherwise 1 (true) by default unless overwritten by the B option.

NewSetting is a boolean value that enables or disables this setting.

Backspaceがアンドゥとして機能する場合、テキスト入力キーとして扱われます。具体的には、キーが抑制されるかどうかは、VisibleNonTextではなく、VisibleTextに依存します。

Backspaceは、Ctrlなどの修飾キーと組み合わせて押された場合、常に無視されます(物理的な状態ではなく、論理的な修飾の状態がチェックされます)。

注:入力テキストが表示されていて(エディターなど)、その中を矢印キーなどで移動する場合、Backspaceはキャレット(挿入点)の後ろの文字ではなく、やはり最後の文字を削除します。

CaseSensitive

Controls whether MatchList is case-sensitive.

CurrentSetting := InputHookObj.CaseSensitive
InputHookObj.CaseSensitive := NewSetting

CurrentSetting is NewSetting if assigned, otherwise 0 (false) by default unless overwritten by the C option.

NewSetting is a boolean value that enables or disables this setting.

FindAnywhere

各マッチが入力テキストの部分文字列となり得るかどうかを制御します。

CurrentSetting := InputHookObj.FindAnywhere
InputHookObj.FindAnywhere := NewSetting

CurrentSetting is NewSetting if assigned, otherwise 0 (false) by default unless overwritten by the * option.

NewSetting is a boolean value that enables or disables this setting. trueの場合、ユーザーが入力した文字列の範囲内であればどこでもマッチします(マッチするのは入力テキストの部分文字列でもかまいません)。If false, the entirety of what the user types must match one of the MatchList phrases. In both cases, one of the MatchList phrases must be typed in full.

MinSendLevel

収集する入力の最小送信レベルを取得または設定します。

CurrentLevel := InputHookObj.MinSendLevel
InputHookObj.MinSendLevel := NewLevel

CurrentLevel is NewLevel if assigned, otherwise 0 by default unless overwritten by the I option.

NewLevel should be an integer between 0 and 101. Events which have a send level lower than this value are ignored. For example, a value of 101 causes all input generated by SendEvent to be ignored, while a value of 1 only ignores input at the default send level (zero).

SendInputおよびSendPlayメソッドは、この設定にかかわらず、常に無視されます。AutoHotkey以外のソースで生成された入力は、この設定の結果、決して無視されることはありません。

NotifyNonText

テキスト以外のキーが押されたときに、OnKeyDownおよびOnKeyUpコールバックを呼び出すかどうかを制御します。

CurrentSetting := InputHookObj.NotifyNonText
InputHookObj.NotifyNonText := NewSetting

CurrentSetting is NewSetting if assigned, otherwise 0 (false) by default.

NewSetting is a boolean value that enables or disables this setting. If true, notifications are enabled for all keypresses which do not produce text, such as when pressing or Alt+F. Setting this property does not affect a key's options, since the production of text depends on the active window's keyboard layout at the time the key is pressed.

NotifyNonText は、VK コードが一致する前のキーダウンがテキストと非テキストのどちらに分類されるかを考慮して、キーアップイベントに適用されます。例えば、NotifyNonTextがtrueの場合、Ctrl+Aを押すと、CtrlAの両方でOnKeyDownOnKeyUpが呼び出されますが、KeyOptでそのキーに対する通知を有効にしていなければ、A単独で押してもOnKeyDownやOnKeyUpは呼ばれません。

どのキーがテキストを生成するものとしてカウントされるかの詳細については、VisibleTextを参照してください。

タイムアウト

タイムアウト値を秒単位で取得または設定します。

CurrentSeconds := InputHookObj.Timeout
InputHookObj.Timeout := NewSeconds

CurrentSeconds is NewSeconds if assigned, otherwise 0 by default unless overwritten by the T option.

NewSeconds is a floating-point number representing the timeout. 0 means no timeout.

通常、タイムアウトはStartが呼ばれた時点で開始されるが、Inputの実行中に本プロパティに値が代入された場合は、再開されます。タイムアウト時間が経過しても入力が継続している場合は、入力は終了し、EndReasonに Timeout という単語が設定されます。

VisibleNonText

テキストを生成しないキーまたはキーの組み合わせを表示するかどうかを制御します(ブロックされません)。

CurrentSetting := InputHookObj.VisibleNonText
InputHookObj.VisibleNonText := NewSetting

CurrentSetting is NewSetting if assigned, otherwise 1 (true) by default. The V option sets this to 1 (true).

NewSetting is a boolean value that enables or disables this setting. trueの場合、テキストを生成しないキーやキーの組み合わせは、ホットキーのトリガーになったり、アクティブウィンドウに渡されたりすることがあります。false の場合、ブロックされます。

どのキーがテキストを生成するものとしてカウントされるかの詳細については、VisibleTextを参照してください。

VisibleText

テキストを生成するキーまたはキーの組み合わせを表示するかどうかを制御します(ブロックされません)。

CurrentSetting := InputHookObj.VisibleText
InputHookObj.VisibleText := NewSetting

CurrentSetting is NewSetting if assigned, otherwise 0 (false) by default unless overwritten by the V option.

NewSetting is a boolean value that enables or disables this setting. trueの場合、テキストを生成するキーとキーの組み合わせは、ホットキーをトリガーしたり、アクティブウィンドウに渡されることがあります。false の場合、ブロックされます。

入力バッファにテキストが追加されるようなキー操作は、他のアプリケーションでは通常そうでなくても、テキストを生成したものとしてカウントされます。For instance, Ctrl+A produces text if the M option is used, and Esc produces the control character Chr(27).

デッドキーは、通常、即効性はありませんが、テキストを生成するものとしてカウントされます。デッドキーを押すと、次のキーでも文字が出るかもしれません(デッドキーの文字だけなら)。

Backspaceアンドゥとして機能する場合のみ、テキストを生成するものとしてカウントされます。

The standard modifier keys and CapsLock, NumLock and ScrollLock are always visible (not blocked).

EndReason

The EndReason property returns one of the following strings:

String 説明
Stopped The Stop method was called or the Start method has not yet been called for the first time.
Max The Input reached the maximum allowed length and it does not match any of the items in MatchList.
タイムアウト 入力のタイムアウト
Match The Input matches one of the items in MatchList. The Match property contains the matched item.
EndKey

EndKeyのいずれかが押され、入力が終了EndKeyプロパティには、中括弧のない終端キー名または文字が格納されます。

入力中の場合、EndReason は空白となります。

備考

Startメソッドは、入力が収集される前に呼び出される必要があります。

InputHookは、スクリプトの異なる部分が最小限の競合で入力を監視できるように設計されています。任意の単語や、その他のパターンを監視するなど、連続的に動作させることも可能です。また、ホットキーに干渉することなく、ユーザー入力の収集や特定(または非特定)キーの一時的な上書きなど、一時的に動作させることも可能です。

キーボードホットキーは、入力中も有効ですが、必要な修飾キーが抑制されている場合、またはホットキーがregメソッドを使用し、そのサフィックスキーが抑制されている場合は、有効にすることができません。例えば、ホットキー^+a::はInputHookによって上書きされるかもしれませんが、InputHookがCtrlまたはShiftを抑制しない限り、ホットキー$^+a::は優先されます。

キーは、以下の要素(順番)により、抑制(ブロック)されるか、されないかが決まります:

キーボードフックは、入力中は必要ですが、入力終了時に不要になれば自動的にアンインストールされます。

このスクリプトは、入力が進行している間は自動的に永続化されるため、実行中のスレッドがない場合でも入力の監視を継続します。入力が終了すると、スクリプトは自動的に終了することがあります(実行中のスレッドがなく、他の理由でスクリプトが永続的でない場合)。

AutoHotkeyは、Input Method Editors(IME)をサポートしていません。キーボードフックは、キーボードイベントを受信し、ToUnicodeExまたはToAsciiExを使用してテキストに変換します(1文字をカプセル化するVK_PACKETイベントの場合を除きます)。

If you use multiple languages or keyboard layouts, InputHook uses the keyboard layout of the active window rather than the script's (regardless of whether the Input is visible).

柔軟性には欠けるが、一般的にホットストリングは使いやすいです。

InputHookとInputの比較(v1)

AutoHotkey v1.1では、InputHookがInputコマンドの代わりとなり、より柔軟な操作性を提供します。Inputコマンドはv2.0で削除されましたが、以下のコードはほぼ同等です:

; Input OutputVar, % Options, % EndKeys, % MatchList  ; v1
ih := InputHook(Options, EndKeys, MatchList)
ih.Start()
ErrorLevel := ih.Wait()
if (ErrorLevel = "EndKey")
    ErrorLevel .= ":" ih.EndKey
OutputVar := ih.Input

Inputコマンドは、開始した前のInputを終了させますが、InputHookは、一度に複数のInputを行うことができます。

Optionsの解釈は同じですが、初期設定が異なります:

Inputコマンドは進行中のスレッドをブロックするのに対し、InputHookはスレッドを継続、あるいは終了(中断したスレッドを再開させる)させることができます。スクリプトは待つ代わりに、Inputが終了したときに呼び出されるOnEnd関数を登録することができます。

InputコマンドはInputが終了した後にしかユーザーの入力を返さないが、InputHookのInputプロパティはいつでも取得できます。スクリプトは、Inputプロパティを継続的にチェックする代わりに、文字が追加されるたびに呼び出されるOnChar関数を登録することができます。

InputHookでは、KeyOptメソッドにより、個々のキーに対してより多くの制御を行うことができます。これには、エンドキーの追加や削除、特定のキーの抑制や非抑制、特定のキーによって生成されるテキストの無視などが含まれます。

Inputコマンドとは異なり、InputHookはInputを終了させることなく、テキストを生成しないキーを検出するために使用することができます。OnKeyDown関数を登録し、KeyOptまたはNotifyNonTextを使用して、どのキーが注目されるかを指定することで実現します。

MatchListの項目が原因でInputが終了した場合、Matchプロパティを参照することで、正確にどのようなマッチがあったかを判断することができます(これは、* optionがある場合に便利です)。

Inputコマンドの終了後、スクリプトはGetKeyStateを参照することができますが、Inputの終了時にどのキーが押されたかが正確に反映されない場合があります。InputHookのEndModsプロパティは、Inputが終了した時点の修飾キーの論理状態を反映しています。

後方互換性に関連するいくつかの相違点があります:

KeyWait, Hotstrings, InputBox, InstallKeybdHook, Threads

ユーザーが任意の1つのキーを押すのを待ちます。

MsgBox KeyWaitAny()

; また同じですが、キーをふさがないようにします。
MsgBox KeyWaitAny("V")

KeyWaitAny(Options:="")
{
    ih := InputHook(Options)
    if !InStr(Options, "V")
        ih.VisibleNonText := false
    ih.KeyOpt("{All}", "E")  ; End
    ih.Start()
    ih.Wait()
    return ih.EndKey  ; Return the key name
}

Ctrl/Alt/Shift/Winと組み合わせた任意のキーで待機します。

MsgBox KeyWaitCombo()

KeyWaitCombo(Options:="")
{
    ih := InputHook(Options)
    if !InStr(Options, "V")
        ih.VisibleNonText := false
    ih.KeyOpt("{All}", "E")  ; End
    ; Exclude the modifiers
    ih.KeyOpt("{LCtrl}{RCtrl}{LAlt}{RAlt}{LShift}{RShift}{LWin}{RWin}", "-E")
    ih.Start()
    ih.Wait()
    return ih.EndMods . ih.EndKey  ; Return a string like <^<+Esc
}

シンプルなオートコンプリート:曜日を問わず駄洒落はさておき、これはほぼ機能的な例です。スクリプトを実行し、今日の入力を開始し、Tabキーを押して完了するか、Escキーを押して終了するだけでよいです。

WordList := "Monday`nTuesday`nWednesday`nThursday`nFriday`nSaturday`nSunday"

Suffix := ""

SacHook := InputHook("V", "{Esc}")
SacHook.OnChar := SacChar
SacHook.OnKeyDown := SacKeyDown
SacHook.KeyOpt("{Backspace}", "N")
SacHook.Start()

SacChar(ih, char)  ; Called when a character is added to SacHook.Input.
{
    global Suffix := ""
    if RegExMatch(ih.Input, "`nm)\w+$", &prefix)
        && RegExMatch(WordList, "`nmi)^" prefix[0] "\K.*", &Suffix)
        Suffix := Suffix[0]
    
if CaretGetPos(&cx, &cy)
        ToolTip Suffix, cx + 15, cy
    else
        ToolTip Suffix

; ツールチップを表示している間だけ、インターセプトタブを表示します。
    ih.KeyOpt("{Tab}", Suffix = "" ? "-NS" : "+NS")
}

SacKeyDown(ih, vk, sc)
{
    if (vk = 8) ; Backspace
        SacChar(ih, "")
    else if (vk = 9) ; Tab
        Send "{Text}" Suffix
}

Waits for the user to press any key. Keys that produce no visible character -- such as the modifier keys, function keys, and arrow keys -- are listed as end keys so that they will be detected too.

ih := InputHook("L1", "{LControl}{RControl}{LAlt}{RAlt}{LShift}{RShift}{LWin}{RWin}{AppsKey}{F1}{F2}{F3}{F4}{F5}{F6}{F7}{F8}{F9}{F10}{F11}{F12}{Left}{Right}{Up}{Down}{Home}{End}{PgUp}{PgDn}{Del}{Ins}{BS}{CapsLock}{NumLock}{PrintScreen}{Pause}")
ih.Start()
ih.Wait()

This is a working hotkey example. Since the hotkey has the tilde (~) prefix, its own keystroke will pass through to the active window. Thus, if you type [btw (or one of the other match phrases) in any editor, the script will automatically perform an action of your choice (such as replacing the typed text). For an alternative version of this example, see Switch.

~[::
{
    msg := ""
    ih := InputHook("V T5 L4 C", "{enter}.{esc}{tab}", "btw,otoh,fl,ahk,ca")
    ih.Start()
    ih.Wait()
    if (ih.EndReason = "Max")
        msg := 'You entered "{1}", which is the maximum length of text.'
    else if (ih.EndReason = "Timeout")
        msg := 'You entered "{1}" at which time the input timed out.'
    else if (ih.EndReason = "EndKey")
        msg := 'You entered "{1}" and terminated the input with {2}.'

if msg  ; If an EndReason was found, skip the rest below.
    {
        MsgBox Format(msg, ih.Input, ih.EndKey)
        return
    }

; Otherwise, a match was found.
    if (ih.Input = "btw")
        Send("{backspace 4}by the way")
    else if (ih.Input = "otoh")
        Send("{backspace 5}on the other hand")
    else if (ih.Input = "fl")
        Send("{backspace 3}Florida")
    else if (ih.Input = "ca")
        Send("{backspace 3}California")
    else if (ih.Input = "ahk")
        Run("https://www.autohotkey.com")
}