autoit でソフトウェアキーボードもどきを作る

2023年11月7日

スクリプト

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
AutoItSetOption("WinTitleMatchMode", 4)

Opt("GUIOnEventMode", 1)
Global $_title_1 =  "abtn1"

$Form1_1 = GUICreate($_title_1, 200, 50, 0, @DesktopHeight-60, BitOR($WS_MINIMIZEBOX,$WS_POPUP,$WS_GROUP))
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1_1Close")
$Button1 = GUICtrlCreateButton("ページ↑", 0, 0, 50, 50, BitOR($BS_DEFPUSHBUTTON,$WS_BORDER))
GUICtrlSetOnEvent(-1, "Button1Click")
$Button2 = GUICtrlCreateButton("ページ↓", 50, 0, 50, 50, BitOR($BS_DEFPUSHBUTTON,$WS_BORDER))
GUICtrlSetOnEvent(-1, "Button2Click")
$Button3 = GUICtrlCreateButton("戻る", 100, 0, 50, 50, BitOR($BS_DEFPUSHBUTTON,$WS_BORDER))
GUICtrlSetOnEvent(-1, "Button3Click") 
$Button4 = GUICtrlCreateButton("終了", 150, 0, 50, 50, BitOR($BS_DEFPUSHBUTTON,$WS_BORDER))
GUICtrlSetOnEvent(-1, "Button4Click")
GUISetState(@SW_SHOW)

WinWait($_title_1)
WinActivate($_title_1)
WinSetOnTop($_title_1, "", 1)
$texta = WinGetHandle ("[ACTIVE]", "")

$textb=0
While 1
	Sleep(100)
	$text = WinGetHandle ("[ACTIVE]", "")
;MsgBox(0, "Text was:", $text)

if $textb<>	$text Then
if $texta<>	$text Then
if 0<>	$text Then
 $textb=$text
	EndIf
	EndIf
	EndIf
WEnd

Func Button1Click()
	WinActivate($textb)
	Send("{PGUP}")
;MsgBox(0, "it was:", $textb,4)

EndFunc
Func Button2Click()
	WinActivate($textb)
	Send("{PGDN}")

EndFunc
Func Button3Click()
	WinActivate($textb)
	Send("!{LEFT}")

EndFunc
Func Button4Click()
Exit

EndFunc
Func Form1_1Close()
Exit

EndFunc

Func CLOSEClicked()
Exit
EndFunc

ボタンを押すまで3秒間押し続けるスクリプト

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
AutoItSetOption("WinTitleMatchMode", 4)

Opt("GUIOnEventMode", 1)
Global $_title_1 =  "abtn2"
Global $ib =  0
GUICtrlSetDefBkColor(0xf0f0f0)

$Form1_1 = GUICreate($_title_1, 200, 50, 0, @DesktopHeight-60, BitOR($WS_MINIMIZEBOX,$WS_POPUP,$WS_GROUP))
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1_1Close")
$Button1 = GUICtrlCreateButton("ページ↑", 0, 0, 50, 50, BitOR($BS_DEFPUSHBUTTON,$WS_BORDER))
GUICtrlSetOnEvent(-1, "Button1Click")
$Button2 = GUICtrlCreateButton("ページ↓", 50, 0, 50, 50, BitOR($BS_DEFPUSHBUTTON,$WS_BORDER))
GUICtrlSetOnEvent(-1, "Button2Click")
$Button3 = GUICtrlCreateButton("戻る", 100, 0, 50, 50, BitOR($BS_DEFPUSHBUTTON,$WS_BORDER))
GUICtrlSetOnEvent(-1, "Button3Click")
$Button4 = GUICtrlCreateButton("終了", 150, 0, 50, 50, BitOR($BS_DEFPUSHBUTTON,$WS_BORDER))
GUICtrlSetOnEvent(-1, "Button4Click")
GUISetState(@SW_SHOW)

WinWait($_title_1)
WinActivate($_title_1)
WinSetOnTop($_title_1, "", 1)
$texta = WinGetHandle ("[ACTIVE]", "")

$textb=0
While 1
	Sleep(100)
	$text = WinGetHandle ("[ACTIVE]", "")
;MsgBox(0, "Text was:", $text)

if $textb<>	$text Then
if $texta<>	$text Then
if 0<>	$text Then
 $textb=$text
EndIf
EndIf
EndIf
_opt($ib)

WEnd

Func Button1Click()
;MsgBox(0, "it was:", $textb,4)
if $ib =  1 Then
	 $ib =  0
	 GUICtrlSetBkColor($Button1, 0xf0f0f0)
Else
	$ib =  1
	 GUICtrlSetBkColor($Button1, 0x00ff00)     ; 緑
 	 GUICtrlSetBkColor($Button2, 0xf0f0f0)
; 	WinActivate($textb)
;	Send("{PGUP}")
EndIf
EndFunc
Func Button2Click()

if $ib =  2 Then
	$ib =  0
	GUICtrlSetBkColor($Button2, 0xf0f0f0)
 Else
	 $ib =  2
 	 GUICtrlSetBkColor($Button2, 0x00ff00)     ; 緑
 	 GUICtrlSetBkColor($Button1, 0xf0f0f0)

;	WinActivate($textb)
;	Send("{PGDN}")
EndIf

EndFunc
Func Button3Click()
	WinActivate($textb)
	Send("!{LEFT}")

EndFunc
Func Button4Click()
Exit

EndFunc
Func Form1_1Close()
Exit

EndFunc

Func CLOSEClicked()
Exit
EndFunc

Func _opt($ia=0)
Switch Int($ia)
	Case 0 To 0
	Case 1
		WinActivate($textb)
		Send("{PGUP}")
		Sleep(3000)

	Case 2
		WinActivate($textb)
		Send("{PGDN}")
		Sleep(3000)

	Case Else
EndSwitch
EndFunc