キーのリマッピング

少し修正したので貼っておこー

バグ修正

  • デスクトップのアイコンを移動させてしまう動作を修正
  • 最大化した画面を移動する場合、最大化を解除してから移動するように変更

機能追加

  • 「Alt」+「1」「3」の再押下で画面サイズを変更
  • マルチモニターに対応(コードは入れたけどテストは未実施)

その他

  • タスクトレイのアイコンを1つにまとめるため、以下のスクリプトが存在していたら読み込むように修正。
    「C:\tool\taskview\taskview_ext.ahk」

作ったもの

keyremap_20240620.zip

「キーのリマッピング」のソースファイル

https://ahkscript.github.io/ja/docs/v2/misc/Remap.htm

#Requires AutoHotkey v2.0+
#SingleInstance Force

; ----------------------------------------
; define
; ----------------------------------------
; デスクトップ情報
DESKTOP_CLASS := "ahk_class Progman"
DESKTOP_CLASS2 := "ahk_class WorkerW"
DESKTOP_EXE := "ahk_exe explorer.exe"
; Alt「1」/「3」
ROTATION_WIDTH_MINI := 0.3
ROTATION_WIDTH_MAX := 1 - ROTATION_WIDTH_MINI
; Alt「2」
CENTER_WIDTH_SIZE := 0.8
CENTER_WIDTH_START := (1 - CENTER_WIDTH_SIZE) / 2

; グローバル変数
previousKey := ""

; ----------------------------------------
; helper function
; ----------------------------------------

; OS情報取得
getWinOSVersion()
{
	osVersion := StrSplit(A_OSVersion, ".")
	major := osVersion[1]
	minor := osVersion[2]
	build := osVersion[3]
	if (major == "10")
		if (minor == "0")
			if (build >= "20000")
				return 11
	return 10
}

; アクティブ画面のモニター番号取得
getMonitorNumberOfActiveWindow()
{
	WinGetPos(&x, &y, , , "A")
	monitorCount := MonitorGetCount()
	Loop monitorCount {
		MonitorGet(A_Index, &left, &top, &right, &bottom)
		if (x >= left
			and x <= right
			and y >= top
			and y <= bottom) {
			return A_Index
		}
	}
	return 1
}

; ディスプレイ情報取得
getDisplayInfo(monitorNum, &x, &y, &width, &height)
{
	if (monitorNum.Length > 0) {
		displayNum := monitorNum[1]
	} else {
		displayNum := getMonitorNumberOfActiveWindow()
	}
	MonitorGetWorkArea(displayNum, &left, &top, &right, &bottom)
	x := left
	y := top
	width := right - left
	height := bottom - top
}

; 画面位置取得
getWindowPosition(monitorNum, windowPosition, &x, &y, &width, &height)
{
	; ディスプレイ情報取得
	getDisplayInfo(monitorNum, &x, &y, &width, &height)

	; 画面サイズ補正
	; 画面の左,右,下に隙間が空くので補正する。
	; ウインドスタイル「WS_SYSMENU」が設定されていない画面は補正なし。(例:VSCode)
	edge := 0
	if (getWinOSVersion() == 11) {
		style := WinGetStyle("A")
		if (style & 0x00080000) { ; WS_SYSMENU
			edge := 7 ; px
		}
	}

	; 画面位置計算
	if (windowPosition == "1min") {
		x := x - edge
		y := y
		width := width * ROTATION_WIDTH_MINI + edge * 2
		height := height + edge
	}
	if (windowPosition == 1) {
		x := x - edge
		y := y
		width := width / 2 + edge * 2
		height := height + edge
	}
	if (windowPosition == "1max") {
		x := x - edge
		y := y
		width := width * ROTATION_WIDTH_MAX + edge * 2
		height := height + edge
	}
	if (windowPosition == 2) {
		x := width * CENTER_WIDTH_START
		y := y
		width := width * CENTER_WIDTH_SIZE + edge * 2
		height := height + edge
	}
	if (windowPosition == "3min") {
		x := width * ROTATION_WIDTH_MINI - edge
		y := y
		width := width * ROTATION_WIDTH_MAX + edge * 2
		height := height + edge
	}
	if (windowPosition == 3) {
		x := width / 2 - edge
		y := y
		width := width / 2 + edge * 2
		height := height + edge
	}
	if (windowPosition == "3max") {
		x := width * ROTATION_WIDTH_MAX - edge
		y := y
		width := width * ROTATION_WIDTH_MINI + edge * 2
		height := height + edge
	}
	if (windowPosition == "q") {
		x := x - edge
		y := y
		width := width / 2 + edge * 2
		height := height / 2 + edge
	}
	if (windowPosition == "a") {
		x := x - edge
		y := height / 2 + y
		width := width / 2 + edge * 2
		height := height / 2 + edge
	}
	if (windowPosition == "w") {
		x := width / 2 - edge
		y := y
		width := width / 2 + edge * 2
		height := height / 2 + edge
	}
	if (windowPosition == "s") {
		x := width / 2 - edge
		y := height / 2 + y
		width := width / 2 + edge * 2
		height := height / 2 + edge
	}
	OutputDebug("x=" x " y=" y " width=" width " height=" height " windowPosition=" windowPosition)
}

; 画面位置設定
setWindowPosition(windowPosition, monitorNum*) {
	global previousKey
	try {
		; デスクトップのアイコンが移動してしまうのでガード
		activeHwnd := WinGetID("A")
		GroupAdd("Desktop", DESKTOP_CLASS " " DESKTOP_EXE)
		GroupAdd("Desktop", DESKTOP_CLASS2 " " DESKTOP_EXE)
		desktopHwnd := WinGetID("ahk_group Desktop")
		if (activeHwnd == desktopHwnd)
			return

		; 画面の最大化を解除
		if (WinGetMinMax(activeHwnd) == 1)
			WinRestore(activeHwnd)

		; 「1」再押下で画面幅を変更
		if (windowPosition == 1) {
			if (previousKey == 1) {
				windowPosition := "1max"
			} else if (previousKey == "1max") {
				windowPosition := "1min"
			} else if (previousKey == "1min") {
				windowPosition := 1
			}
		}
		; 「3」再押下で画面幅を変更
		if (windowPosition == 3) {
			if (previousKey == 3) {
				windowPosition := "3max"
			} else if (previousKey == "3max") {
				windowPosition := "3min"
			} else if (previousKey == "3min") {
				windowPosition := 3
			}
		}

		; 移動位置取得
		getWindowPosition(monitorNum, windowPosition, &x, &y, &width, &height)

		; 画面移動
		WinMove(x, y, width, height, activeHwnd)

		; 前回の移動先保存
		previousKey := windowPosition
	}
}

; ----------------------------------------
; main
; ----------------------------------------

; 矢印キー × 5
^Up:: Send "{UP 5}"
^Down:: Send "{Down 5}"
^Left:: Send "{Left 5}"
^Right:: Send "{Right 5}"

; 上下左右
^*i:: Send "{UP}"
^*k:: Send "{Down}"
^*j:: Send "{Left}"
^*l:: Send "{Right}"
^*u:: Send "{Home}"
^*o:: Send "{End}"

; 削除
^*h:: Send "{BS}"

; 画面移動(左/中央/右)
!1:: setWindowPosition("1")
!2:: setWindowPosition("2")
!3:: setWindowPosition("3")

; 画面移動(左上/左下/右上/右下)
!q:: setWindowPosition("q")
!a:: setWindowPosition("a")
!w:: setWindowPosition("w")
!s:: setWindowPosition("s")

; 音量調整
^F10:: Send "{VOLUME_MUTE}"
^F11:: Send "{VOLUME_DOWN}"
^F12:: Send "{VOLUME_UP}"

; 仮想デスクトップ
#Include "*i C:\tool\taskview\taskview_ext.ahk"

Ctrl + 矢印キー:矢印キー5回連打
Ctrl + iキー :上キー
Ctrl + kキー :下キー
Ctrl + jキー :左キー
Ctrl + lキー :右キー
Ctrl + uキー :Homeキー
Ctrl + oキー :Endキー
Ctrl + hキー :BackSpace
Alt + 1キー :アクティブ画面左寄せ*
Alt + 2キー :アクティブ画面中央寄せ
Alt + 3キー :アクティブ画面右寄せ*
Alt + qキー :アクティブ画面左上寄せ
Alt + aキー :アクティブ画面左下寄せ
Alt + wキー :アクティブ画面右上寄せ
Alt + sキー :アクティブ画面右下寄せ
Ctrl + F10 :音量ミュート
Ctrl + F11 :音量Down
Ctrl + F12 :音量UP

*:機能追加

アクティブ画面の移動先モニターの指定

setWindowPosition関数の第2引数にモニター番号を指定します。

例:

!1:: setWindowPosition("1", 1)
!2:: setWindowPosition("2", 1)
!3:: setWindowPosition("3", 1)
!4:: setWindowPosition("1", 2)
!5:: setWindowPosition("2", 2)
!6:: setWindowPosition("3", 2)

上記の例では、「Alt」+「1~3」キーでプライマリモニタに、「Alt」+「4~6」キーでセカンダリモニターに、画面移動します。(動作未確認)

例:

!1:: setWindowPosition("1")
!2:: setWindowPosition("2")
!3:: setWindowPosition("3")

省略時は、アクティブ画面が存在しているモニターで画面移動します。

! = Alt
^ = Ctrl
+ = Shift
# = Win

…φ(. . )メモメモ


Comments

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です