3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Powershell+AutoHotkeyでホットキーを便利に登録

Posted at

はじめに

ネットサーフィンをしていたら、AutoHotkeyというものを発見しました。
面白そうだったので、試しに、Powershellと連携させて、ホットキー登録を行ってみました。

ちなみに、AutoHotkeyの使い方等については以下サイトを参考にしました。
http://ahkwiki.net/

実行環境スペック

OS:Microsoft Windows 8
PowerShellバージョン:3.0

手順1:Chocolateyのインストール

・Powershellを「管理者として実行する」
・以下コマンドを実行(参考 https://chocolatey.org/)

iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))

手順2:AutoHotkeyのインストール

以下コマンドを実行。

choco install autohotkey.portable

※なお、AutoHotkey(Portable)を選択しているのは筆者の好みによるもの。無印のAutoHotkeyやAutoHotkey(Install)でも、多分、問題はないと思う。

手順3:AutoHotkeyを使ってみる

例えば、筆者は以下のようなスクリプトを作成してみた。

startup.ps1
&{
$_peek     = "C:\opt\Peek Through\Peek Through.exe"
$_feewhee  = "C:\opt\feewhee\feewhee.exe"
$_ledim    = "C:\opt\LeDimmer\LeDimmer.exe"
$_pause    = "C:\opt\pause_103\pause.exe"
$_ahk      = "C:\ProgramData\chocolatey\lib\autohotkey.portable\tools\AutoHotkey.exe"
$_tempfile = Join-Path (Split-Path $script:myInvocation.MyCommand.path -parent) "temp\startup.ahk"

@"
Run,$_peek
Run,$_feewhee
Run,$_ledim
Run,$_pause
+!p up::Run,powershell.exe
+^!q up::Run,$_ledim
+!/ up::
  WinGetClass, class, A
  WinGetTitle, title, A
  MsgBox, class=%class%/title=%title%
  Return
"@ > $_tempfile
& $_ahk $_tempfile
}

ちょっとだけ解説

上記スクリプトを実行すると、以下処理が行われる。
1.各種プログラム(Peek Through、feewhee、LeDimmer、pause)の実行
※各種プログラムは別途インストールが必要。

2.ホットキー登録
 ・[Shift + Alt + P]押下でPowershellの実行
 ・[Ctrl + Shift + Alt + Q]押下でLeDimmerの実行
 ・[Ctrl + Shift + /]押下でアクティブウィンドウのクラス名、タイトル名の表示

AutoHotkeyの良い所

・スクリプト実行形式なので、Powershellとの相性がいい
・ホットキー登録以外のことも、色々できる。例えば、
 ・クリック操作、キー押下操作などの自動化
 ・クリップボード処理
 など。

・ホットキー設定のために、GUIでポチポチしなくて済む

AutoHotkeyのここが知りたい

・実行時オプションとして、コマンドを文字列として直接渡せないか。
例えば、Powershellで、以下のように書けたら素敵だと思う。

$_ahk      = "C:\ProgramData\chocolatey\lib\autohotkey.portable\tools\AutoHotkey.exe"

# ホットキーを登録しバックグラウンドジョブとして待機させる
$job = Start-Job {"+!p up::Run,powershell.exe" | & $_ahk}

# バックグラウンドジョブを停止しホットキーを解除する
$job | Stop-Job

おわりに

AutoHotkeyに関しては、筆者自身、使い始めたばかりで、まだまだ分からないことが多いです。
「こんな使い方があるよ」とか「この使い方は良くない」などの情報があれば、ご教示いただけるとありがたいです。

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?