RCtrlで入力切替が起きないようにする
タイトルは私にしか理解できないもので、他の方が読んでも意味不明だと思う
というか内容も私の環境独自の話なので、特に誰の役にも立たない気がする
前提
Windows PCでMac風キーバインドを目指す
続 Windows PCでMac風キーバインドを目指す
何が起きているか
前提の中で以下をやっている
レジストリ変更
CapsLockキー → RCtrl(右Ctrl)
右Cmd → RCtrl
AutoHotKeyでマッピング変更
RCtrl単体押しで変換キー
Google日本語入力の設定変更
変換キーで入力切替
以上を踏まえると、CapsLockキーを単体で押すと入力切替が発生する
これはMac風キーバインドを目指す、というゴールに反する挙動であり、実際邪魔になっている
これをなんとかしたい、というのが今回のお題
当初CapsLockで入力切替が起きないようにするというタイトルだったが、これだと一般的な問題のCapsLockをオフにしたい、という問題について書いているように捉えられてしまうため変更した
これによってより意味不明なタイトルになった
展望
CapsLockキーはレジストリ変更でCtrlにしておきたい
右Cmdは私は入力切替のためにしか使わずCtrlとして使うことがないからCtrlをマッピングしておく必要はない
ということで、右CmdをCtrlでない別のキーにレジストリ変更して、そのキーで入力切替するようにすればいい、と考えた
レジストリ変更
右Cmdを変換キーにそもそもレジストリ変更してしまえばいい、と考えたのだが結論できなかった
US配列だと変換キーが存在しないためそもそも設定できない
参考: Windows日本語環境におけるJIS配列とUS配列のスキャンコードを全て列挙してみた - Qiita
2番の注釈に書いてある
次の手として、右CmdをF13みたいな仮想キーにレジストリ変更して、AutoHotKeyでF13で変換キーを打てるようにする方策を考えた
F13のような仮想キーの存在はPowerToysを触ったときにたしか知った
Change KeyではF13へのレジストリ変更はできないと思ったので、自力でレジストリ変更する方法を調べた
ASCII.jp:キーボードのキー入れ替えにおける、仮想キーコードとキーボードスキャンコード (2/2)が分かりやすい
しかし、Change KeyでF13へのレジストリ変更ができることが分かったので、使い続けることにした
参考: WindowsでMacみたいなキーボード操作を。 - Qiita
私がやりたかったことそのまま書いてくれてる
結局やったことは、右CmdをF13にレジストリ変更する
https://gyazo.com/c3b2dd0a970f9bc465e5630aec06ff13
右下の赤く囲まれたキーが見切れてて読めない表示になっているがこれでOKだった
上のQiitaにも書いてあるが、やり方は以下の通り
変更したいキーを選択する
変更先を選択するウィンドウで、右上のScan codeボタンを押す
コードを打つ
今回のF13は0064
余談だが、Change Keyは結局のところ、自力でレジストリ変更するときにつくるScancode Mapというものを生成してくれるツールである、ということを学んだ
AutoHotKeyスクリプトの修正
続 Windows PCでMac風キーバインドを目指すのAutoHotKeyスクリプトを以下のように変更すればOKだった
RCtrl単体押しで変換キーにならないようにする
F13単体押しで変換キーになるようにする
結果できたスクリプトは以下
code:ahk
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
; SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
#USEHOOK On
LCtrl::Send, {vk1D}
F13::Send, {vk1C}
LCtrl & a::send_with_modifiers("a")
LCtrl & b::send_with_modifiers("b")
LCtrl & c::send_with_modifiers("c")
LCtrl & d::send_with_modifiers("d")
LCtrl & e::send_with_modifiers("e")
LCtrl & f::send_with_modifiers("f")
LCtrl & g::send_with_modifiers("g")
LCtrl & h::send_with_modifiers("h")
LCtrl & i::send_with_modifiers("i")
LCtrl & j::send_with_modifiers("j")
LCtrl & k::send_with_modifiers("k")
LCtrl & l::send_with_modifiers("l")
LCtrl & m::send_with_modifiers("m")
LCtrl & n::send_with_modifiers("n")
LCtrl & o::send_with_modifiers("o")
LCtrl & p::send_with_modifiers("p")
LCtrl & q::send_with_modifiers("q")
LCtrl & r::send_with_modifiers("r")
LCtrl & s::send_with_modifiers("s")
LCtrl & t::send_with_modifiers("t")
LCtrl & u::send_with_modifiers("u")
LCtrl & v::send_with_modifiers("v")
LCtrl & w::send_with_modifiers("w")
LCtrl & x::send_with_modifiers("x")
LCtrl & y::send_with_modifiers("y")
LCtrl & z::send_with_modifiers("z")
RCtrl & a::send_custom_command_if_modifiers_are_only_ctrl("a", "{Home}")
RCtrl & b::send_custom_command_if_modifiers_are_only_ctrl("b", "{Left}")
RCtrl & d::send_custom_command_if_modifiers_are_only_ctrl("d", "{Delete}")
RCtrl & e::send_custom_command_if_modifiers_are_only_ctrl("e", "{End}")
RCtrl & f::send_custom_command_if_modifiers_are_only_ctrl("f", "{Right}")
RCtrl & h::send_custom_command_if_modifiers_are_only_ctrl("h", "{Backspace}")
RCtrl & k::send_custom_command_if_modifiers_are_only_ctrl("k", "+{End}{Delete}")
RCtrl & n::send_custom_command_if_modifiers_are_only_ctrl("n", "{Down}")
RCtrl & p::send_custom_command_if_modifiers_are_only_ctrl("p", "{Up}")
; 修飾キーを取得する
get_modifiers() {
m := ""
if GetKeyState("Control", "P")
m = %m%^
if GetKeyState("Shift", "P")
m = %m%+
if GetKeyState("Alt", "P")
m = %m%!
return m
}
; 修飾キーつきでSendする
send_with_modifiers(key) {
m := get_modifiers()
Send, %m%{%key%}
}
; ctrlだけだったら別のコマンドをSendする
send_custom_command_if_modifiers_are_only_ctrl(key, custom_command) {
m := get_modifiers()
if m = ^
Send, %custom_command%
else
Send, %m%{%key%}
}
#USEHOOK Off
続 Windows PCでMac風キーバインドを目指すと比べると以下のリファクタリングも含まれている
RCtrlのアルファベット全部のキーマップを書いていたが不要になったのでいらないものは削除
RCtrl単体押しのマッピングがなくなったから
send_with_modifiers_unless_modifiers_are_not_only_ctrlという関数をsend_custom_command_if_modifiers_are_only_ctrlにrename
そもそもunlessとnotがあるのおかしいなと今さら気づいた
これで満足