Hotkey

スクリプトの実行中にホットキーを作成、変更、有効化、または無効化します。

Hotkey KeyName , Action, Options

パラメータ

KeyName

型:文字列

修飾記号を含む、ホットキーの起動キーの名前。例えば、Win+Cのホットキーには#cを指定します。

If KeyName already exists as a hotkey -- either by the Hotkey function or a double-colon label in the script -- that hotkey will be updated with the values of the function's other parameters.

When specifying an existing hotkey, KeyName is not case-sensitive. ただし、キーの名前は既存のホットキーと同じスペルでなければなりません(例:この目的ではEscはEscapeと同じではありません)。Also, the order of modifier symbols such as ^!+# does not matter. GetKeyName can be used to retrieve the standard spelling of a key name.

When a hotkey is first created -- either by the Hotkey function or the double-colon syntax in the script -- its key name and the ordering of its modifier symbols becomes the permanent name of that hotkey as reflected by ThisHotkey. This name is shared by all variants of the hotkey, and does not change even if the Hotkey function later accesses the hotkey with a different symbol ordering.

ホットキーバリアントがすでに存在する場合、KeyNameチルダ (~) 接頭辞を含むか含まないかによって、その動作が更新されます。

既存のホットキーにユースフック($)プレフィックスを追加することができます。この接頭辞は、ホットキーのすべてのバリエーションに影響し、削除することはできません。

アクション

型:Function Object or String

If omitted and KeyName already exists as a hotkey, its action will not be changed. ホットキーのOptionsのみを変更したい場合に便利です。Otherwise, specify a callback, a hotkey name without trailing colons, or one of the special values listed below.


Specify the function to call (as a new thread) when the hotkey is pressed.

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

MyCallback(HotkeyName) { ...

Although the name you give the parameter does not matter, it is assigned the hotkey name.

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(*).

Hotkeys defined with the double-colon syntax automatically use the parameter name ThisHotkey. Hotkeys can also be assigned a function name without the Hotkey function.

Note: If a callback is specified but the hotkey is disabled from a previous use of the Hotkey function, the hotkey will remain disabled. これを防ぐには、「オプション」に「ON」を入れてください。


Specify a hotkey name to use its original function; specifically, the original function of the hotkey variant corresponding to the current HotIf criteria. 通常、ホットキーの機能を変更した後に元の機能に戻すために使用しますが、両方のホットキーが同じHotIf基準を使用している場合に、別のホットキーの機能を割り当てるために使用することもできます。


Specify one of the following special values:

On: The hotkey becomes enabled. ホットキーがすでにOnになっている場合は、何もしません。

Off: The hotkey becomes disabled. ホットキーがすでにOffになっている場合は、何もしません。

Toggle: The hotkey is set to the opposite state (enabled or disabled).

AltTab(その他):これらは、ここで説明するAlt-Tabの特殊なホットキーアクションです。

Options

型:文字列

A string of zero or more of the following options with optional spaces in between. 事例:"On B0".

On: Enables the hotkey if it is currently disabled.

Off: Disables the hotkey if it is currently enabled. これは、通常、初期状態では無効な状態のホットキーを作成するために使用されます。

B または B0:Bを指定すると、#MaxThreadsBufferで説明したように、ホットキーをバッファリングします。Specify B0 (B with the number 0) to disable this type of buffering.

Pn:Specify the letter P followed by the hotkey's thread priority. ホットキー作成時にPオプションが省略された場合は、0が使用されます。

S または S0:Sを指定すると、ホットキーがサスペンド免除するようになり、サスペンドをオフにするためにホットキーを使用できるようになります。S0(Sに数字の0)を指定すると、免除が解除され、ホットキーが中断できるようになります。

Tn:Specify the letter T followed by a the number of threads to allow for this hotkey as described in #MaxThreadsPerHotkey. 事例:T5.

In (InputLevel):Specify the letter I (or i) followed by the hotkey's input level. 事例:I1.

オプション文字が省略され、ホットキーがすでに存在する場合、それらのオプションは変更されません。しかし、ホットキーがまだ存在しない場合、つまりこの機能によって作成されようとしている場合、オプションは直近で有効だったものがデフォルトとなります。例えば、スクリプトの一番下にある#MaxThreadsBufferのインスタンスが使用されます。MaxThreadsBufferがスクリプトに表示されていない場合は、そのデフォルト設定(この場合はOFF)が使用されます。

エラー処理

パラメータが無効な場合、またはメモリ確保に失敗した場合は例外がスローされます。

ホットキーが無効であったり、作成できなかったりすると、以下のいずれかの例外が発生することがあります:

エラークラス .Message 説明
ValueError 無効なキー名です。 KeyNameパラメータは、現在のキーボードレイアウト/言語で認識されていない、またはサポートされていない1つまたは複数のキーを指定します。Exception.Extraには、キー名が含まれます。
未対応のプレフィックスキーです。 例えば、WheelDown & Enterのようなホットキーの接頭辞としてマウスホイールを使用することはサポートされていません。Exception.Extraにはプレフィックスキーが含まれています。
This AltTab hotkey must have exactly one modifier/prefix. KeyNameパラメータは、AltTabまたはShiftAltTabアクションでの使用には適していません。(最大で)2つのキーの組み合わせが必要です。事例:RControl & RShift::AltTab. Exception.Extra contains KeyName.
このAltTabホットキーは、どのキー(LまたはR)を指定する必要があります。
TargetError ホットキーが存在しない。 この関数は、存在しないホットキーを変更しようとしました。Exception.Extra contains KeyName.
存在しないホットキーバリアント(IfWin)。 この関数は、既存のホットキーの存在しない変種を変更しようとしました。これを解決するには、HotIfを使用して、変更するホットキーの条件と一致するように設定します。Exception.ExtraKeyNameが含まれています。
Error ホットキーを最大にします。 このホットキーを作成すると、スクリプトごとに32762個のホットキーという制限を超えてしまいます(ただし、各ホットキーには無制限のバリエーションがあり、ホットストリングの数には制限がありません)。

Tip:Try-Catch は、ホットキーのバリアントが存在するかどうかをテストするために使用することができます。事例:

try
    Hotkey "^!p"
catch TargetError
    MsgBox "The hotkey does not exist or it has no variant for the current HotIf criteria."

備考

現在のHotIf設定は、Hotkey機能が動作するホットキーのバリアントを決定します。

アクティブなウィンドウの種類に応じて選択したホットキーやホットストリングを自動的に無効にすることを目的とする場合、Hotkey "^!c", "Off" は通常、WinActive/WinExist(またはその動的対応であるHotIfWinActive/Exist)で#HotIfを使用するより利便性が低くなります。

ダブルコロン構文でホットキーを作成すると、スクリプトの起動時に一括してホットキーを有効にできるため、ホットキー機能を使用するよりもパフォーマンスが向上します(1つずつ有効にするのではありません)。したがって、この機能は、スクリプトの実行が開始されるまでキー名がわからないホットキーだけを作成するために使用するとよいでしょう。例えば、スクリプトのホットキーで様々なアクションを行う場合、INIファイルで設定することが可能です。

スクリプトがサスペンドされている場合、新しく追加/有効になったホットキーも、サスペンドがオフになるまでサスペンドされます(サスペンドセクションで説明したように免除される場合を除く)。

キーボードマウスのフックは、本機能による変更で正当化される場合、取り付けまたは取り外しが可能です。

ホットキー機能は、自分以外のスクリプトのホットキーを直接有効にしたり無効にしたりすることはできませんが、ほとんどの場合、同じホットキーを作成したり有効にしたりすることで、それらをオーバーライドできます。これがうまくいくかどうかは、さまざまな要因が重なって決まります:1) オーバーライドするホットキーが他のスクリプトのフックホットキーであるかどうか(フックでないホットキーは常にオーバーライド可能)、2) 最も最近起動したスクリプトのホットキーが他のスクリプトのそれよりも一般的に優先されるという事実(したがって、オーバーライドしようとするスクリプトが最も最近起動した場合、そのオーバーライドは常に成功します)、 3) このホットキーを有効または作成することによってキーボードまたはマウスフックを新たにアクティブ化するかどうか(その場合、オーバーライドは必ず成功します)。

Once a script has at least one hotkey, it becomes persistent, meaning that ExitApp rather than Exit should be used to terminate it.

バリアント(複製)ホットキー

A particular hotkey can be created more than once if each definition has different HotIf criteria. These are known as hotkey variants. 事例:

HotIfWinActive "ahk_class Notepad"
Hotkey "^!c", MyFuncForNotepad
HotIfWinActive "ahk_class WordPadClass"
Hotkey "^!c", MyFuncForWordPad
HotIfWinActive
Hotkey "^!c", MyFuncForAllOtherWindows

ホットキーの複数のバリエーションが発火の対象となる場合、最も早く作成されたものだけが発火します。The exception to this is the global variant (the one with no HotIf criteria):常に最も低い優先順位を持つので、他のバリアントが適格でない場合にのみ発火します。

When creating duplicate hotkeys, the order of modifier symbols such as ^!+# does not matter. For example, "^!c" is the same as "!^c". ただし、キーは一貫したスペルで表記する必要があります。For example, Esc is not the same as Escape for this purpose (though the case does not matter). Finally, any hotkey with a wildcard prefix (*) is entirely separate from a non-wildcard one; for example, "*F1" and "F1" would each have their own set of variants.

For more information, see HotIf and #HotIf's General Remarks.

Hotkey Symbols, HotIf, A_ThisHotkey, #MaxThreadsBuffer, #MaxThreadsPerHotkey, Suspend, Threads, Thread, Critical, Return, Menu object, SetTimer

Ctrl-Alt-Zのホットキーを作成します。

Hotkey "^!z", MyFunc

MyFunc(ThisHotkey)
{
    MsgBox "You pressed " ThisHotkey
}

RCtrlとRShiftをAlt-Tabのように操作できるようにします。

Hotkey "RCtrl & RShift", "AltTab"

Shift-Win-Cのホットキーを無効にします。

Hotkey "$+#c", "Off"

5つのスレッドを許可するようにホットキーを変更します。

Hotkey "^!a",, "T5"

Alt+Wをメモ帳でのみ動作するホットキーとして作成します。

HotIfWinActive "ahk_class Notepad"
Hotkey "!w", ToggleWordWrap  ; !w = Alt+W

ToggleWordWrap(ThisHotkey)
{
    MenuSelect "A",, "Format", "Word Wrap"
}

プリミティブな3キーの組み合わせのホットキーを登録できるGUIを作成します。

HkGui := Gui()
HkGui.Add("Text", "xm", "Prefix key:")
HkGui.Add("Edit", "yp x100 w100 vPrefix", "Space")
HkGui.Add("Text", "xm", "Suffix hotkey:")
HkGui.Add("Edit", "yp x100 w100 vSuffix", "f & j")
HkGui.Add("Button", "Default", "Register").OnEvent("Click", RegisterHotkey)
HkGui.OnEvent("Close", (*) => ExitApp())
HkGui.OnEvent("Escape", (*) => ExitApp())
HkGui.Show()

RegisterHotkey(*)
{
    Saved := HkGui.Submit(false)
    HotIf (*) => GetKeyState(Saved.Prefix)
    Hotkey Saved.Suffix, (ThisHotkey) => MsgBox(ThisHotkey)
}