OnScreen Keyboard & Separate NumPad - Dark-themed - Responsive/Resizable Design - Template

Post your working scripts, libraries and tools.
sashaatx
Posts: 341
Joined: 27 May 2021, 08:27
Contact:

OnScreen Keyboard & Separate NumPad - Dark-themed - Responsive/Resizable Design - Template

27 Apr 2024, 20:00

I needed this for a commercial application, it'll divulge from this point to something more application specific.

It utilizes GuiResizer which can be found here: viewtopic.php?f=83&t=113921&hilit=guiresizer

I used Easy AutoGui Clone, and my GuiResizer converter https://github.com/samfisherirl/GuiResizer-Addons/blob/main/convert2Resizer.ahk

There are many inefficiencies, I just needed a quick a=>b solution, if you respond with code adjustments or additions, I will add.

Right now it only sends default key values, shift + {key} or any combonation is not functional.

Image

Code: Select all

#Requires Autohotkey v2
#SingleInstance force
#Include <GuiReSizer>
SetTitleMatchMode 2

K := Keyboard()
darkmode(k.gui)

class Keyboard
{
	static hwnd := 0
	static lastWindow := 0
	
	__New()
	{
		local storage := Keyboard.Build()
		this.controls := Map(), this.Gui := Map()
		this.controls := storage['controls']
		this.Gui := storage['gui']
		this.lastWindow := 0
		this.gui.Show("w600 h400")
		this.assignEvents(this.controls)
		SetTimer(enum, 40)
		storage := ""
	}
	static Build()
	{
		myCtrls := {}
		myGui := Gui(), myGui.Opt("+Resize +MinSize250x150 +AlwaysOnTop +E0x8000000")
		myGui.OnEvent("Size", _GuiReSizer)
		controls := Map()
		x := {}
		x := myGui.Add("Button", "", "Esc")
		controls[x.hwnd] := "Esc"
		pushToResizer(x, 0.01, 0.04, 0.05, 0.12)
		x := myGui.Add("Button", "", "F1")
		controls[x.hwnd] := "{F1}"
		pushToResizer(x, 0.09, 0.04, 0.05, 0.12)
		x := myGui.Add("Button", "", "! 1")
		controls[x.hwnd] := "1"
		pushToResizer(x, 0.07, 0.21, 0.05, 0.12)
		x := myGui.Add("Button", "", "F2")
		controls[x.hwnd] := "{F2}"
		pushToResizer(x, 0.145, 0.04, 0.05, 0.12)
		x := myGui.Add("Button", "", "F3")
		controls[x.hwnd] := "{F3}"
		pushToResizer(x, 0.20, 0.04, 0.05, 0.12)
		x := myGui.Add("Button", "", "F4")
		controls[x.hwnd] := "{F4}"
		pushToResizer(x, 0.255, 0.04, 0.05, 0.12)
		x := myGui.Add("Button", "", "F5")
		controls[x.hwnd] := "{F5}"
		pushToResizer(x, 0.32, 0.04, 0.05, 0.12)
		x := myGui.Add("Button", "", "F6")
		controls[x.hwnd] := "{F6}"
		pushToResizer(x, 0.37, 0.04, 0.05, 0.12)
		x := myGui.Add("Button", "", "F7")
		controls[x.hwnd] := "{F7}"
		pushToResizer(x, 0.42, 0.04, 0.05, 0.12)
		x := myGui.Add("Button", "", "F8")
		controls[x.hwnd] := "{F8}"
		pushToResizer(x, 0.48, 0.04, 0.05, 0.12)
		x := myGui.Add("Button", "", "F9")
		controls[x.hwnd] := "{F9}"
		pushToResizer(x, 0.535, 0.04, 0.05, 0.12)
		x := myGui.Add("Button", "", "F10")
		controls[x.hwnd] := "{F10}"
		pushToResizer(x, 0.595, 0.04, 0.05, 0.12)
		x := myGui.Add("Button", "", "F11")
		controls[x.hwnd] := "{F11}"
		pushToResizer(x, 0.65, 0.04, 0.05, 0.12)
		x := myGui.Add("Button", "", "F12")
		controls[x.hwnd] := "{F12}"
		pushToResizer(x, 0.70, 0.04, 0.05, 0.12)
		x := myGui.Add("Button", "", "PrScn")
		controls[x.hwnd] := "{PrScn}"
		pushToResizer(x, 0.77, 0.04, 0.07, 0.12)
		x := myGui.Add("Button", "", "ScrLk")
		controls[x.hwnd] := "{ScrLk}"
		pushToResizer(x, 0.84, 0.04, 0.07, 0.12)
		x := myGui.Add("Button", "", "Pause")
		controls[x.hwnd] := "{Pause}"
		pushToResizer(x, 0.91, 0.04, 0.07, 0.12)
		x := myGui.Add("Button", "", "~ ")
		controls[x.hwnd] := "`""
		pushToResizer(x, 0.01, 0.21, 0.05, 0.12)
		x := myGui.Add("Button", "", "@ 2")
		controls[x.hwnd] := "2"
		pushToResizer(x, 0.125, 0.21, 0.05, 0.12)
		x := myGui.Add("Button", "", "# 3")
		controls[x.hwnd] := "3"
		pushToResizer(x, 0.179, 0.21, 0.05, 0.12)
		x := myGui.Add("Button", "", "$ 4")
		controls[x.hwnd] := "4"
		pushToResizer(x, 0.233, 0.21, 0.05, 0.12)
		x := myGui.Add("Button", "", "% 5")
		controls[x.hwnd] := "5"
		pushToResizer(x, 0.285, 0.21, 0.05, 0.12)
		x := myGui.Add("Button", "", "^ 6")
		controls[x.hwnd] := "6"
		pushToResizer(x, 0.34, 0.21, 0.05, 0.12)
		x := myGui.Add("Button", "", "& 7")
		controls[x.hwnd] := "7"
		pushToResizer(x, 0.393, 0.21, 0.05, 0.12)
		x := myGui.Add("Button", "", "* 8")
		controls[x.hwnd] := "8"
		pushToResizer(x, 0.445, 0.21, 0.05, 0.12)
		x := myGui.Add("Button", "", "( 9")
		controls[x.hwnd] := "9"
		pushToResizer(x, 0.495, 0.21, 0.05, 0.12)
		x := myGui.Add("Button", "", ") 0")
		controls[x.hwnd] := "0"
		pushToResizer(x, 0.5475, 0.21, 0.05, 0.12)
		x := myGui.Add("Button", "", "_ -")
		controls[x.hwnd] := "-"
		pushToResizer(x, 0.603, 0.21, 0.05, 0.12)
		x := myGui.Add("Button", "", "+ =")
		controls[x.hwnd] := "="
		pushToResizer(x, 0.6575, 0.21, 0.05, 0.12)
		x := myGui.Add("Button", "", "Backspace")
		controls[x.hwnd] := "{Backspace}"
		pushToResizer(x, 0.71, 0.21, 0.09, 0.12)
		x := myGui.Add("Button", "", "Tab")
		controls[x.hwnd] := "{Tab}"
		pushToResizer(x, 0.01, 0.35, 0.07, 0.12)
		x := myGui.Add("Button", "", "Q")
		controls[x.hwnd] := "Q"
		pushToResizer(x, 0.085, 0.35, 0.05, 0.12)
		x := myGui.Add("Button", "", "W")
		controls[x.hwnd] := "W"
		pushToResizer(x, 0.14, 0.35, 0.05, 0.12)
		x := myGui.Add("Button", "", "E")
		controls[x.hwnd] := "E"
		pushToResizer(x, 0.195, 0.35, 0.05, 0.12)
		x := myGui.Add("Button", "", "R")
		controls[x.hwnd] := "R"
		pushToResizer(x, 0.25, 0.35, 0.05, 0.12)
		x := myGui.Add("Button", "", "T")
		controls[x.hwnd] := "T"
		pushToResizer(x, 0.305, 0.35, 0.05, 0.12)
		x := myGui.Add("Button", "", "Y")
		controls[x.hwnd] := "Y"
		pushToResizer(x, 0.36, 0.35, 0.05, 0.12)
		x := myGui.Add("Button", "", "U")
		controls[x.hwnd] := "U"
		pushToResizer(x, 0.415, 0.35, 0.05, 0.12)
		x := myGui.Add("Button", "", "I")
		controls[x.hwnd] := "I"
		pushToResizer(x, 0.47, 0.35, 0.05, 0.12)
		x := myGui.Add("Button", "", "O")
		controls[x.hwnd] := "O"
		pushToResizer(x, 0.525, 0.35, 0.05, 0.12)
		x := myGui.Add("Button", "", "P")
		controls[x.hwnd] := "P"
		pushToResizer(x, 0.58, 0.35, 0.05, 0.12)
		x := myGui.Add("Button", "", "{ [")
		controls[x.hwnd] := "{"
		pushToResizer(x, 0.63, 0.35, 0.05, 0.12)
		x := myGui.Add("Button", "", "} ]")
		controls[x.hwnd] := "}"
		pushToResizer(x, 0.68, 0.35, 0.05, 0.12)
		x := myGui.Add("Button", "", "| \")
		controls[x.hwnd] := "\"
		pushToResizer(x, 0.74, 0.35, 0.06, 0.12)
		x := myGui.Add("Button", "", "CapsLock")
		controls[x.hwnd] := "CapsLock"
		pushToResizer(x, 0.01, 0.48, 0.08, 0.12)
		x := myGui.Add("Button", "", "A")
		controls[x.hwnd] := "A"
		pushToResizer(x, 0.10, 0.48, 0.05, 0.12)
		x := myGui.Add("Button", "", "S")
		controls[x.hwnd] := "S"
		pushToResizer(x, 0.16, 0.48, 0.05, 0.12)
		x := myGui.Add("Button", "", "D")
		controls[x.hwnd] := "D"
		pushToResizer(x, 0.22, 0.48, 0.05, 0.12)
		x := myGui.Add("Button", "", "F")
		controls[x.hwnd] := "F"
		pushToResizer(x, 0.28, 0.48, 0.05, 0.12)
		x := myGui.Add("Button", "", "G")
		controls[x.hwnd] := "G"
		pushToResizer(x, 0.34, 0.48, 0.05, 0.12)
		x := myGui.Add("Button", "", "H")
		controls[x.hwnd] := "H"
		pushToResizer(x, 0.40, 0.48, 0.05, 0.12)
		x := myGui.Add("Button", "", "J")
		controls[x.hwnd] := "J"
		pushToResizer(x, 0.46, 0.48, 0.05, 0.12)
		x := myGui.Add("Button", "", "K")
		controls[x.hwnd] := "K"
		pushToResizer(x, 0.52, 0.48, 0.05, 0.12)
		x := myGui.Add("Button", "", "L")
		controls[x.hwnd] := "L"
		pushToResizer(x, 0.58, 0.48, 0.05, 0.12)
		x := myGui.Add("Button", "", ":")
		controls[x.hwnd] := ";"
		pushToResizer(x, 0.63, 0.48, 0.05, 0.12)
		x := myGui.Add("Button", "", "\")
		controls[x.hwnd] := "\"
		pushToResizer(x, 0.68, 0.48, 0.05, 0.12)
		x := myGui.Add("Button", "", "Enter")
		controls[x.hwnd] := "Enter"
		pushToResizer(x, 0.74, 0.48, 0.06, 0.12)
		x := myGui.Add("Button", "", "Shift")
		controls[x.hwnd] := "{LShift}"
		pushToResizer(x, 0.01, 0.62, 0.11, 0.12)
		x := myGui.Add("Button", "", "Z")
		controls[x.hwnd] := "Z"
		pushToResizer(x, 0.13, 0.62, 0.05, 0.12)
		x := myGui.Add("Button", "", "X")
		controls[x.hwnd] := "X"
		pushToResizer(x, 0.19, 0.62, 0.05, 0.12)
		x := myGui.Add("Button", "", "C")
		controls[x.hwnd] := "C"
		pushToResizer(x, 0.25, 0.62, 0.05, 0.12)
		x := myGui.Add("Button", "", "V")
		controls[x.hwnd] := "V"
		pushToResizer(x, 0.31, 0.62, 0.05, 0.12)
		x := myGui.Add("Button", "", "B")
		controls[x.hwnd] := "B"
		pushToResizer(x, 0.37, 0.62, 0.05, 0.12)
		x := myGui.Add("Button", "", "N")
		controls[x.hwnd] := "N"
		pushToResizer(x, 0.43, 0.62, 0.05, 0.12)
		x := myGui.Add("Button", "", "M")
		controls[x.hwnd] := "M"
		pushToResizer(x, 0.49, 0.62, 0.05, 0.12)
		x := myGui.Add("Button", "", "< ,")
		controls[x.hwnd] := ","
		pushToResizer(x, 0.55, 0.62, 0.05, 0.12)
		x := myGui.Add("Button", "", "> .")
		controls[x.hwnd] := "."
		pushToResizer(x, 0.60, 0.62, 0.05, 0.12)
		x := myGui.Add("Button", "", "? /")
		controls[x.hwnd] := "/"
		pushToResizer(x, 0.65, 0.62, 0.05, 0.12)
		x := myGui.Add("Button", "", "Shift")
		controls[x.hwnd] := "{RShift}"
		pushToResizer(x, 0.71, 0.62, 0.09, 0.12)
		x := myGui.Add("Button", "", "Ctrl")
		controls[x.hwnd] := "{LCtrl}"
		pushToResizer(x, 0.01, 0.76, 0.07, 0.12)
		x := myGui.Add("Button", "", "Win")
		controls[x.hwnd] := "{LWin}"
		pushToResizer(x, 0.09, 0.76, 0.07, 0.12)
		x := myGui.Add("Button", "", "Alt")
		controls[x.hwnd] := "{LAlt}"
		pushToResizer(x, 0.17, 0.76, 0.07, 0.12)
		x := myGui.Add("Button", "", " ")
		controls[x.hwnd] := "{Space}"
		pushToResizer(x, 0.25, 0.76, 0.25, 0.12)
		x := myGui.Add("Button", "", "Alt")
		controls[x.hwnd] := "{RAlt}"
		pushToResizer(x, 0.51, 0.76, 0.07, 0.12)
		x := myGui.Add("Button", "", "Win")
		controls[x.hwnd] := "{RWin}"
		pushToResizer(x, 0.59, 0.76, 0.07, 0.12)
		x := myGui.Add("Button", "", "Menu")
		controls[x.hwnd] := "Menu"
		pushToResizer(x, 0.67, 0.76, 0.07, 0.12)
		x := myGui.Add("Button", "", "Ctrl")
		controls[x.hwnd] := "{RCtrl}"
		pushToResizer(x, 0.75, 0.76, 0.07, 0.12)
		Send "{LAlt Down}{Tab}{LAlt Up}"
		local storage := Map()
		storage['gui'] := myGui
		storage['controls'] := controls
		return storage
	}
	assignEvents(controls)
	{
		for ctrl_hwnd, key in controls
		{
			ctrl := GuiCtrlFromHwnd(ctrl_hwnd)
			ctrl.OnEvent("Click", Keyboard.HandleSend.Bind(ctrl_hwnd, key))
		}
	}
	static HandleSend(key, ctrl, *)
	{
		try t := WinGetTitle("ahk_id " keyboard.lastWindow)
		catch
			t := "ahk_id " this.lastWindow
		Send(
			InStr(key, "{") ? key : Keyboard.lowercase(key) ;implement shift case activation here
		)
		if Trim(t) != "ahk_id 0"
			WinActivate(t)
	}
	static lowerCase(Haystack) {
		NeedleRegEx := "[A-Z]" ; Regular expression to find a single uppercase letter.
		Replacement := "$l0" ; Replacement pattern to change the matching uppercase letter to lowercase.
		; Call the RegExReplace function with the provided parameters.
		NewStr := RegExReplace(Haystack, NeedleRegEx, Replacement, &OutputVarCount, 1) ; Limit is set to 1 to replace the first occurrence only.
		; Display the original and modified strings.
		return NewStr
	}
}
enum()
{
	try active := WinExist("A")
	if !(IsSet(active))
		return
	if keyboard != active
		Keyboard.lastWindow := active
}
class _GuiResizer extends GuiReSizer
{
	static resizeCalltime := 0
	static Call(guiobj, params*)
	{
		_GuiResizer.resizeCalltime := A_TickCount
		GuiReSizer.Call(guiobj, params*)
		gu := _GuiResizer.redraw.Bind(guiobj)
		SetTimer gu, -400
	}
	static redraw(guiobj?)
	{
		if _GuiResizer.resizeCalltime + 400 < A_TickCount
			for k, ctrl in this
			{
				try ctrl.redraw()
			}
	}
}
pushToResizer(ctrl, xp, yp, wp, hp)
{
	ctrl.xp := xp
	ctrl.yp := yp
	ctrl.wp := wp
	ctrl.hp := hp
}
blackGuiCtrl(params*)
{
	if params[1].gui.backcolor
	{
		bg := params[1].gui.backcolor
	} else {
		bg := "12121a"
	}
	for ctrl in params
	{
		try
		{
			ctrl.Opt("Background" bg)
		} catch as e {
			continue
		}
		try
		{
			ctrl.SetFont("cWhite")
		} catch as e {
			continue
		}
	}
}
mcColorFunc()
{
	return mcColors := {
		MCSC_BACKGROUND: 0,
		MCSC_TEXT: 1,
		MCSC_TITLEBK: 2,
		MCSC_TITLETEXT: 3,
		MCSC_MONTHBK: 4,
		MCSC_TRAILINGTEXT: 5
	}
}
GetFont(options, fontName)
{
	myGUI := Gui()
	myGUI.SetFont(options, fontName)
	hFont := SendMessage(WM_GETFONT := 0x31, , , myGUI.AddText())
	myGUI.Destroy()
	return hFont
}
class darkMode
{
	static color := "191919"
	static Call(myGUI, color?)
	{
		myGUI.BackColor := IsSet(color) ? color : darkMode.color
		if (VerCompare(A_OSVersion, "10.0.17763") >= 0)
		{
			DWMWA_USE_IMMERSIVE_DARK_MODE := 19
			if (VerCompare(A_OSVersion, "10.0.18985") >= 0)
			{
				DWMWA_USE_IMMERSIVE_DARK_MODE := 20
			}
			try DllCall("dwmapi\DwmSetWindowAttribute", "Ptr", myGUI.hwnd, "Int", DWMWA_USE_IMMERSIVE_DARK_MODE, "Int*", true, "Int", 4)
			; listView => SetExplorerTheme(LV1, "DarkMode_Explorer"), SetExplorerTheme(LV2, "DarkMode_Explorer")
			try uxtheme := DllCall("GetModuleHandle", "Str", "uxtheme", "Ptr")
			try DllCall(DllCall("GetProcAddress", "Ptr", uxtheme, "Ptr", 135, "Ptr"), "Int", 2) ; ForceDark
			try DllCall(DllCall("GetProcAddress", "Ptr", uxtheme, "Ptr", 136, "Ptr"))
		}
		for ctrlHWND, ctrl in myGUI
		{
			if ctrl.HasProp("NoBack")
				continue
			blackGuiCtrl(myGUI[ctrlHWND])
		}
	}
}
NumPad

Code: Select all

#Requires Autohotkey v2
#SingleInstance force
#Include <GuiReSizer>

N := Numpad()
N.show()

Class Numpad
{
	static hwnd := 0
	static lastWindow := 0
	__New()
	{
		local storage := Numpad.Build()
		this.controls := Map(), this.Gui := Map()
		this.controls := storage['controls']
		this.Gui := storage['gui']
		this.lastWindow := 0
		storage := ""
		this.assignEvents(this.controls)
	}
	show()
	{
		this.gui.Show('w300 h400')
		SetTimer(enum, 40)
		return this
	}
	destroy()
	{
		SetTimer(enum, 0)
		this.gui.Destroy()
	}
	static Build()
	{
		myCtrls := {}
		myGui := Gui(), myGui.Opt("+Resize +MinSize250x150 +AlwaysOnTop +E0x8000000")
		myGui.OnEvent("Size", GuiReSizer)
		controls := Map(), storage := Map()
		myGui.SetFont('s20 cWhite', "Arial")
		x := {}
		x := myGui.Add("Button", "", "1")
		controls[x.hwnd] := "1"
		pushToResizer(x, 0.045, 0.059, 0.264, 0.181)
		x := myGui.Add("Button", "", "2")
		controls[x.hwnd] := "2"
		pushToResizer(x, 0.364, 0.059, 0.264, 0.181)
		x := myGui.Add("Button", "", "3")
		controls[x.hwnd] := "3"
		pushToResizer(x, 0.659, 0.059, 0.264, 0.181)
		x := myGui.Add("Button", "", "4")
		controls[x.hwnd] := "4"
		pushToResizer(x, 0.045, 0.277, 0.264, 0.181)
		x := myGui.Add("Button", "", "5")
		controls[x.hwnd] := "5"
		pushToResizer(x, 0.364, 0.277, 0.264, 0.181)
		x := myGui.Add("Button", "", "6")
		controls[x.hwnd] := "6"
		pushToResizer(x, 0.668, 0.287, 0.264, 0.181)
		x := myGui.Add("Button", "", "7")
		controls[x.hwnd] := "7"
		pushToResizer(x, 0.045, 0.495, 0.264, 0.181)
		x := myGui.Add("Button", "", "8")
		controls[x.hwnd] := "8"
		pushToResizer(x, 0.364, 0.495, 0.264, 0.181)
		x := myGui.Add("Button", "", "9")
		controls[x.hwnd] := "9"
		pushToResizer(x, 0.665, 0.495, 0.264, 0.181)
		x := myGui.Add("Button", "", "Remove")
		controls[x.hwnd] := "Remove"
		pushToResizer(x, 0.045, 0.713, 0.264, 0.181)
		x := myGui.Add("Button", "", "0")
		controls[x.hwnd] := "0"
		pushToResizer(x, 0.364, 0.713, 0.264, 0.181)
		x := myGui.Add("Button", "", "Submit")
		controls[x.hwnd] := "Submit"
		pushToResizer(x, 0.665, 0.713, 0.264, 0.181)
		Numpad.hwnd := myGui.hwnd
		local storage := Map()
		storage['gui'] := myGui
		storage['controls'] := controls
		pushToResizer(ctrl, xp, yp, wp, hp)
		{
			ctrl.xp := xp
			ctrl.yp := yp
			ctrl.wp := wp
			ctrl.hp := hp
		}
		return storage
	}
	assignEvents(controls)
	{
		for ctrl_hwnd, key in controls
		{
			ctrl := GuiCtrlFromHwnd(ctrl_hwnd)
			ctrl.OnEvent("Click", numpad.HandleSend.Bind(ctrl_hwnd, key))
		}
	}
	static HandleSend(key, ctrl, *)
	{
		try t := WinGetTitle("ahk_id " numpad.lastWindow)
		catch
			t := "ahk_id " numpad.lastWindow
		Send(
			InStr(key, "{") ? key : numpad.lowercase(key) ;implement shift case activation here
		)
		if Trim(t) != "ahk_id 0"
			WinActivate(t)
	}
	static lowerCase(Haystack) {
		NeedleRegEx := "[A-Z]" ; Regular expression to find a single uppercase letter.
		Replacement := "$l0" ; Replacement pattern to change the matching uppercase letter to lowercase.
		; Call the RegExReplace function with the provided parameters.
		NewStr := RegExReplace(Haystack, NeedleRegEx, Replacement, &OutputVarCount, 1) ; Limit is set to 1 to replace the first occurrence only.
		; Display the original and modified strings.
		return NewStr
	}
}


enum()
{
	try active := WinExist("A")
	if !(IsSet(active))
		return
	if Numpad.hwnd != active
		Numpad.lastWindow := active
}

class _GuiResizer extends GuiReSizer
{
	static resizeCalltime := 0
	static Call(guiobj, params*)
	{
		_GuiResizer.resizeCalltime := A_TickCount
		GuiReSizer.Call(guiobj, params*)
		gu := _GuiResizer.redraw.Bind(guiobj)
		SetTimer gu, -400
	}
	static redraw(guiobj?)
	{
		if _GuiResizer.resizeCalltime + 400 < A_TickCount
			for k, ctrl in this
			{
				try ctrl.redraw()
			}
	}
}
Last edited by sashaatx on 10 May 2024, 07:19, edited 8 times in total.
https://github.com/samfisherirl
? /Easy-Auto-GUI-for-AHK-v2 ? /Useful-AHK-v2-Libraries-and-Classes : /Pulovers-Macro-Creator-for-AHKv2 :
sashaatx
Posts: 341
Joined: 27 May 2021, 08:27
Contact:

Re: OnScreen Keyboard - Dark-themed - Responsive/Resizable Design - Template

28 Apr 2024, 20:28

https://github.com/samfisherirl
? /Easy-Auto-GUI-for-AHK-v2 ? /Useful-AHK-v2-Libraries-and-Classes : /Pulovers-Macro-Creator-for-AHKv2 :
sashaatx
Posts: 341
Joined: 27 May 2021, 08:27
Contact:

Re: OnScreen Keyboard & Separate NumPad - Dark-themed - Responsive/Resizable Design - Template

10 May 2024, 07:17

Post & Titled edited, including separate NumPad

Code: Select all

#Requires Autohotkey v2
#SingleInstance force
#Include <GuiReSizer>

N := Numpad()
N.show()

Class Numpad
{
	static hwnd := 0
	static lastWindow := 0
	__New()
	{
		local storage := Numpad.Build()
		this.controls := Map(), this.Gui := Map()
		this.controls := storage['controls']
		this.Gui := storage['gui']
		this.lastWindow := 0
		storage := ""
		this.assignEvents(this.controls)
	}
	show()
	{
		this.gui.Show('w300 h400')
		SetTimer(enum, 40)
		return this
	}
	destroy()
	{
		SetTimer(enum, 0)
		this.gui.Destroy()
	}
	static Build()
	{
		myCtrls := {}
		myGui := Gui(), myGui.Opt("+Resize +MinSize250x150 +AlwaysOnTop +E0x8000000")
		myGui.OnEvent("Size", GuiReSizer)
		controls := Map(), storage := Map()
		myGui.SetFont('s20 cWhite', "Arial")
		x := {}
		x := myGui.Add("Button", "", "1")
		controls[x.hwnd] := "1"
		pushToResizer(x, 0.045, 0.059, 0.264, 0.181)
		x := myGui.Add("Button", "", "2")
		controls[x.hwnd] := "2"
		pushToResizer(x, 0.364, 0.059, 0.264, 0.181)
		x := myGui.Add("Button", "", "3")
		controls[x.hwnd] := "3"
		pushToResizer(x, 0.659, 0.059, 0.264, 0.181)
		x := myGui.Add("Button", "", "4")
		controls[x.hwnd] := "4"
		pushToResizer(x, 0.045, 0.277, 0.264, 0.181)
		x := myGui.Add("Button", "", "5")
		controls[x.hwnd] := "5"
		pushToResizer(x, 0.364, 0.277, 0.264, 0.181)
		x := myGui.Add("Button", "", "6")
		controls[x.hwnd] := "6"
		pushToResizer(x, 0.668, 0.287, 0.264, 0.181)
		x := myGui.Add("Button", "", "7")
		controls[x.hwnd] := "7"
		pushToResizer(x, 0.045, 0.495, 0.264, 0.181)
		x := myGui.Add("Button", "", "8")
		controls[x.hwnd] := "8"
		pushToResizer(x, 0.364, 0.495, 0.264, 0.181)
		x := myGui.Add("Button", "", "9")
		controls[x.hwnd] := "9"
		pushToResizer(x, 0.665, 0.495, 0.264, 0.181)
		x := myGui.Add("Button", "", "Remove")
		controls[x.hwnd] := "Remove"
		pushToResizer(x, 0.045, 0.713, 0.264, 0.181)
		x := myGui.Add("Button", "", "0")
		controls[x.hwnd] := "0"
		pushToResizer(x, 0.364, 0.713, 0.264, 0.181)
		x := myGui.Add("Button", "", "Submit")
		controls[x.hwnd] := "Submit"
		pushToResizer(x, 0.665, 0.713, 0.264, 0.181)
		Numpad.hwnd := myGui.hwnd
		local storage := Map()
		storage['gui'] := myGui
		storage['controls'] := controls
		pushToResizer(ctrl, xp, yp, wp, hp)
		{
			ctrl.xp := xp
			ctrl.yp := yp
			ctrl.wp := wp
			ctrl.hp := hp
		}
		return storage
	}
	assignEvents(controls)
	{
		for ctrl_hwnd, key in controls
		{
			ctrl := GuiCtrlFromHwnd(ctrl_hwnd)
			ctrl.OnEvent("Click", numpad.HandleSend.Bind(ctrl_hwnd, key))
		}
	}
	static HandleSend(key, ctrl, *)
	{
		try t := WinGetTitle("ahk_id " numpad.lastWindow)
		catch
			t := "ahk_id " numpad.lastWindow
		Send(
			InStr(key, "{") ? key : numpad.lowercase(key) ;implement shift case activation here
		)
		if Trim(t) != "ahk_id 0"
			WinActivate(t)
	}
	static lowerCase(Haystack) {
		NeedleRegEx := "[A-Z]" ; Regular expression to find a single uppercase letter.
		Replacement := "$l0" ; Replacement pattern to change the matching uppercase letter to lowercase.
		; Call the RegExReplace function with the provided parameters.
		NewStr := RegExReplace(Haystack, NeedleRegEx, Replacement, &OutputVarCount, 1) ; Limit is set to 1 to replace the first occurrence only.
		; Display the original and modified strings.
		return NewStr
	}
}


enum()
{
	try active := WinExist("A")
	if !(IsSet(active))
		return
	if Numpad.hwnd != active
		Numpad.lastWindow := active
}

class _GuiResizer extends GuiReSizer
{
	static resizeCalltime := 0
	static Call(guiobj, params*)
	{
		_GuiResizer.resizeCalltime := A_TickCount
		GuiReSizer.Call(guiobj, params*)
		gu := _GuiResizer.redraw.Bind(guiobj)
		SetTimer gu, -400
	}
	static redraw(guiobj?)
	{
		if _GuiResizer.resizeCalltime + 400 < A_TickCount
			for k, ctrl in this
			{
				try ctrl.redraw()
			}
	}
}
image.png
image.png (30.95 KiB) Viewed 103 times
https://github.com/samfisherirl
? /Easy-Auto-GUI-for-AHK-v2 ? /Useful-AHK-v2-Libraries-and-Classes : /Pulovers-Macro-Creator-for-AHKv2 :

Return to “Scripts and Functions (v2)”

Who is online

Users browsing this forum: No registered users and 16 guests