[CLASS] CtlColors - color your controls (2017-10-30)

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: [CLASS] CtlColors - color your controls

07 Sep 2016, 07:41

Also, what is the purpose of the WinSet, Redraw in the GuiSize: label?

If I use AHK_H to have a resizable editbox, and comment this code out, it does not seem to affect the coloring of the Editbox

WARNING! AHK_H not AHK_L code!!

Code: Select all

#SingleInstance force

#NoEnv
;#Include Class_CtlColors.ahk
OnExit, GuiClose
; ----------------------------------------------------------------------------------------------------------------------
SysGet, SGW, 71 ; SM_CXMENUCHECK
LB_SETCURSEL := 0x186
CB_SETCURSEL := 0x14E
Red   := "FF0000"
Green := "00C000"
Blue  := "0000FF"
Pink  := "FF20FF"
; ----------------------------------------------------------------------------------------------------------------------
Gui +Resize
Gui, Add, Text, , Integers Only
Gui, Add, Edit, x+10 yp-3 w300 aw ah vED1 hwndEDID1 gEditChanged

; ----------------------------------------------------------------------------------------------------------------------
Gui, Show, , Colored Controls
Return

EditChanged:
	Gui, Submit, NoHide
	if (ED1 == "" || round(ED1 "") == ED1 ""){
		CtlColors.Detach(EDID1)
	} else {
		CtlColors.Attach(EDID1, "Red")
	}
	return

; ----------------------------------------------------------------------------------------------------------------------
GuiClose:
GuiEscape:
   Gui, Destroy
   CtlColors.Free()
ExitApp
just me
Posts: 9442
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: [CLASS] CtlColors - color your controls

07 Sep 2016, 10:27

evilC wrote:Here is an altered version of the lib that encapsulates the OnMesssage handler into the class. It should be compatible with old code that uses this lib.
If i'd want to rewrite it, I'd put the whole thing into one (or a set of) plain function(s), now.
Also, what is the purpose of the WinSet, Redraw in the GuiSize: label?
In prior versions of AHK the colors were not shown after the first Gui, Show, in most cases, because AHK ignored the notifications for a certain amount of time while running the auto-exec section. So the Gui had to be redrawn to bring the colors up.
just me
Posts: 9442
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: [CLASS] CtlColors - color your controls

09 Sep 2016, 07:50

Fell free to use the lib or parts of it and to modify it to meet your requirements.
User avatar
SpeedMaster
Posts: 494
Joined: 12 Nov 2016, 16:09

Re: [CLASS] CtlColors - color your controls

24 Oct 2017, 08:42

Hello, thx for this nice class :bravo:
However, I have a small problem, :think:
Is it possible to recover transparency of a text control after the change of background color ?
Thanks.

Code: Select all

#NoEnv
#Include Class_CtlColors.ahk
OnExit, GuiClose

gui, font, s18
; ----------------------------------------------------------------------------------------------------------------------
gui, add, text, w700 h30 center 0x200 backgroundtrans vtxt hwndtxt, (Press space) How to recover transparency of this txt control ?
; ----------------------------------------------------------------------------------------------------------------------
gui, color, lime
Gui, Show, , Colored Controls
; ----------------------------------------------------------------------------------------------------------------------
CtlColors.Change(txt, "red", "yellow")
Return

space::
CtlColors.Change(txt, "", "")         ; how to recover transparancy ?
return

GuiClose:
GuiEscape:
   Gui, Destroy
   CtlColors.Free()
ExitApp

; ----------------------------------------------------------------------------------------------------------------------
GuiSize:
   If (A_EventInfo != 1) {
      Gui, %A_Gui%:+LastFound
      WinSet, ReDraw
   }
Return
; ----------------------------------------------------------------------------------------------------------------------

Return
just me
Posts: 9442
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: [CLASS] CtlColors - color your controls

29 Oct 2017, 02:34

AHK is using a transparent brush to paint the background of BackgroundTrans controls. So the last colour used to paint the the background e.g. by Class_CtlColors remains visible. Also,

Code: Select all

CtlColors.Change(txt, "", "")         ; how to recover transparancy ?
sets the background colour to the control's default background colour. You should use CtlColors.Detach() instead to remove the control from the array of controls handled by the class.

Repainting the whole window seems to provide the wanted behaviour for your example, but I'm not sure if it will do in every other case:

Code: Select all

#NoEnv
#Include Class_CtlColors.ahk
OnExit, GuiClose
Gui, +HwndHGUI
gui, font, s18
; ----------------------------------------------------------------------------------------------------------------------
gui, add, text, w700 h30 center 0x200 backgroundtrans vtxt hwndhtxt, (Press space) How to recover transparency of this txt control ?
; ----------------------------------------------------------------------------------------------------------------------
gui, color, lime, lime
Gui, Show, , Colored Controls
; ----------------------------------------------------------------------------------------------------------------------
CtlColors.Change(htxt, "red", "yellow")
Return

space::
CtlColors.Detach(htxt)
WinSet, Redraw, , ahk_id %HGUI%
return

GuiClose:
GuiEscape:
   Gui, Destroy
   CtlColors.Free()
ExitApp

; ----------------------------------------------------------------------------------------------------------------------
GuiSize:
   If (A_EventInfo != 1) {
      Gui, %A_Gui%:+LastFound
      WinSet, ReDraw
   }
Return
; ----------------------------------------------------------------------------------------------------------------------

Return
User avatar
SpeedMaster
Posts: 494
Joined: 12 Nov 2016, 16:09

Re: [CLASS] CtlColors - color your controls

29 Oct 2017, 13:30

Thank you for your reply.
CtlColors.detach() is a good workaround.
However i would also like to use the change() method to change only the text color.

Is there a way to handle transparency like a color ? :geek:
for ex.
CtlColors.Change(htxt, "transparent", "blue")
just me
Posts: 9442
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: [CLASS] CtlColors - color your controls

30 Oct 2017, 05:48

Well, here's a new version of the class you might want to test. You may pass "Trans" as BkColor for a (hopefully) transparent background.

Code: Select all

Removed test code.
BTW: The proper method to assign colors to a control for the first time is CtlColors.Attach().
Last edited by just me on 31 Oct 2017, 10:42, edited 1 time in total.
User avatar
SpeedMaster
Posts: 494
Joined: 12 Nov 2016, 16:09

Re: [CLASS] CtlColors - color your controls

31 Oct 2017, 09:00

just me wrote:Here's a new version of the class you might want to test. You may pass "Trans" as BkColor for a (hopefully) transparent background.
This is exactly what I was looking for. 8-) Thanks for sharing :thumbup:
just me
Posts: 9442
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: [CLASS] CtlColors - color your controls (2017-10-30)

31 Oct 2017, 10:42

Hi SpeedMaster,
thanks for testing. I updated the OP.
Portwolf
Posts: 161
Joined: 08 Oct 2018, 12:57

Re: [CLASS] CtlColors - color your controls (2017-10-30)

06 Dec 2018, 19:41

Is this still active?!
If so, please let me know, even by PM whould be awesome, i need this badly and i need to ask some questions.. :D
I have a script that would be much much more usefull if i could color the controls:

https://autohotkey.com/boards/viewtopic ... 43#p251843


I included this on my script. Now:
If anyone prefer to answer here, how do i change the color of the background of a GuiControl based on the word in it?
Ie:
"OK" would be green color with white text
"FAIL" would be red with white text
"..." whould be grey with black text

This would be awesome to put on the test results :D
just me
Posts: 9442
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: [CLASS] CtlColors - color your controls (2017-10-30)

07 Dec 2018, 05:39

For example:

Code: Select all

#NoEnv
#Include Class_CtlColors.ahk
OnExit, GuiClose
Gui, +LastFound
Gui, Add, Text, w300 Center hwndHTX1
Gui, Add, Text, w300 Center hwndHTX2
Gui, Add, Text, w300 Center hwndHTX3
Gui, Add, Button, w300 gChange, Change Text
Gui, Show, , Colored Controls
Loop, 3 {
   Txt := GetRandomTxt()
   GetTxtColors(Txt, BC, TC)
   CtlColors.Attach(HTX%A_Index%, BC, TC)
   GuiControl, , % HTX%A_Index%, %Txt%
}
; WinSet, Redraw
Return
; ----------------------------------------------------------------------------------------------------------------------
GuiClose:
GuiEscape:
   Gui, Destroy
   CtlColors.Free()
ExitApp
; ----------------------------------------------------------------------------------------------------------------------
Change:
Loop, 3 {
   Txt := GetRandomTxt()
   GetTxtColors(Txt, BC, TC)
   CtlColors.Change(HTX%A_Index%, BC, TC)
   GuiControl, , % HTX%A_Index%, %Txt%
}
Return
; ----------------------------------------------------------------------------------------------------------------------
GetRandomTxt() {
   Static TxtArr := ["Text containing OK", "Text containing FAIL", "Any other text"]
   Random, R, 1, 3
   Return TxtArr[R]
}
; ----------------------------------------------------------------------------------------------------------------------
GetTxtColors(Txt, ByRef BkgColor := "", ByRef TxtColor := "") {
   If RegExMatch(Txt, "\bOK\b") {
      BkgColor := "Green"
      TxtColor := "White"
   }
   Else If RegExMatch(Txt, "\bFAIL\b") {
      BkgColor := "Red"
      TxtColor := "White"
   }
   Else {
      BkgColor := "Silver"
      TxtColor := "Black"
   }
}
Portwolf
Posts: 161
Joined: 08 Oct 2018, 12:57

Re: [CLASS] CtlColors - color your controls (2017-10-30)

10 Dec 2018, 11:05

Awesome!! Going to try and implement that :)

Thanks!
Portwolf
Posts: 161
Joined: 08 Oct 2018, 12:57

Re: [CLASS] CtlColors - color your controls (2017-10-30)

10 Dec 2018, 12:03

And i failed miserably..
I cannot understand how to do it..

Can you throw a bone a little further? :D

I've got a program that changes the Gui controls based on a verification and then changes the value of the GUI Control accordingly.

Code: Select all

Gui, Add, Edit, x20 h19 y40 w40 hwndHTX1 +Center vHeaderStatus
Gui, Add, Edit, x20 h19 y60 w40 hwndHTX2 +Center vMenuStatus
Gui, Add, Edit, x20 h19 y80 w40 hwndHTX3 +Center vContentStatus
Gui, Add, Edit, x20 h19 y100 w40 hwndHTX4 +Center vFooterStatus
(...)
And i use this function to check them:

Code: Select all

getElementById(obj,ele,ByRef tracker, ByRef failelement){
	if obj.document.getElementById(ele){
		GuiControl,,tracker, OK
		return True
	}
else {
	GuiControl,,tracker, FAIL
	SoundBeep, 500, 100
	return False
}
}
How do i set that up? i already added the hwndHTX but, cannot get it working.
I'm seeing a lot of code there that i almost dont understand..
Like many here, im pretty new to this.
Hatsuko
Posts: 15
Joined: 09 May 2018, 16:56

Re: [CLASS] CtlColors - color your controls (2017-10-30)

14 Dec 2018, 20:38

Hello just me,

First of all, thank you for this awesome class :D

I have made a little input viewer with the help of Class_CtlColors. It looks like this:
Image
I use Class_CtlColors to color my GUI controls. Whenever a key is pressed down or released, the corresponding control's color should change.
However I'm facing a problem. The program works fine at the beginning, but it will stop working after some time.

By after some time I mean:
For example, it may be working for 47 minutes and then it stops working.

By stop working I mean:
  1. It stops showing my key press. The view does not update anymore. Instead, it fixes on the last key(s) I pressed before it stopped working.
  2. Right click on the tray brings nothing. Default behavior: AHK default tray menu should show up.
  3. Somehow the program is still running. For example, it can still print logs. And it doesn't show "Not Responding" in task manager.

I use it to display my key press in gaming, so the colors will change very often and rather fast. Can this be the cause of the problem? I know nothing about Windows API, I am just guessing that maybe it's kind of heavy operation...

Here's my code:

IWannaInputViewer.ahk

Code: Select all

#SingleInstance Force
#NoEnv
; #Warn

#Include <Class_CtlColors>

class IWannaInputViewer {
	static config := { Left: { options: "x129 y97 w30 h30", text: "" }  ; Designed in AutoGUI 2.5.4
		, Right: { options: "x193 y97 w30 h30", text: "" }
		, Up: { options: "x161 y65 w30 h30", text: "" }
		, Down: { options: "x161 y97 w30 h30", text: "" }
		, LShift: { options: "x1 y65 w62 h30", text: "Shift" }
		, RShift: { options: "x97 y65 w62 h30", text: "Shift" }
		, Z: { options: "x65 y65 w30 h30", text: "Z" }
		, R: { options: "x65 y1 w30 h30", text: "R" }
		, S: { options: "x65 y33 w30 h30", text: "S" }
		, Q: { options: "x33 y1 w30 h30", text: "Q" }
		, P: { options: "x97 y1 w30 h30", text: "P" }
		, Enter: { options: "x129 y33 w62 h30", text: "Enter" }
		, Backspace: { options: "x129 y1 w62 h30", text: "Back" }
		, F2: { options: "x1 y1 w30 h30", text: "F2" }
		, CapsLock: { options: "x1 y33 w62 h30", text: "Caps" }
		, RControl: { options: "x97 y97 w30 h30", text: "Ctrl" } }

	static commonOptions := "+Center +0x200"  ; +Border


	static fontName := "Envy Code R"
	static fontOptions := "s9 q3"

	static windowTitle := "I wanna be the Input Viewer"
	static windowW := 224, windowH := 128

	static windowBgColor := "000000"
	static idleKeyBgColor := "333333"
	static idleKeyTextColor := "666666"
	static activeKeyBgColor := "666666"
	static activeKeyTextColor := "CCCCCC"

	static classInitialized := IWannaInputViewer.ClassInit()
	static debugMode := False

	ClassInit() {
		#Include %A_LineFile%/../IWannaInputViewer_Unicode.ahk
		this.config.Left.text := STRINGS.LEFT_ARROW
		this.config.Right.text := STRINGS.RIGHT_ARROW
		this.config.Up.text := STRINGS.UP_ARROW
		this.config.Down.text := STRINGS.DOWN_ARROW
		Return True
	}

	class Key {
		isDown := False
		hwnd :=
		__New(hwnd := "") {
			If (hwnd != "") {
				this.hwnd := hwnd
			}
		}
	}

	static keys := {}

	; https://autohotkey.com/docs/misc/SendMessageList.htm
	static WINDOWS_MESSAGES := { WM_LBUTTONDOWN: 0x201, WM_NCLBUTTONDOWN: 0xA1 }

	NewGui() {
		Gui, IWannaInputViewer:New, +AlwaysOnTop, % this.windowTitle

		Gui, Font, % this.fontOptions, % this.fontName, 
		Gui, Color, % this.windowBgColor

		For k, v in this.config {
			keyName := k
			options := v.options . " " . this.commonOptions . " Hwndh"
			text := v.text

			Gui, Add, Text, %options%, %text%
			this.keys[keyName] := new IWannaInputViewer.Key(h)

			; Color it
			CtlColors.Attach(h, this.idleKeyBgColor, this.idleKeyTextColor)

			; Create hotkeys
			funcObj := ObjBindMethod(this, "OnKeyDown", keyName)
			Hotkey, ~*%keyName%, %funcObj%
			funcObj := ObjBindMethod(this, "OnKeyUp", keyName)
			Hotkey, ~*%keyName% up, %funcObj%
		}

		options := "w" . this.windowW . " h" . this.windowH
		Gui, Show, %options%, 

		funcObj := ObjBindMethod(this, "GuiMove")
		OnMessage(this.WINDOWS_MESSAGES.WM_LBUTTONDOWN, funcObj)

		funcObj := ObjBindMethod(this, "ToggleDebugMode")
		Menu, IWannaInputViewerContextMenu, Add, % "Debug Mode", %funcObj%
	}

	OnKeyDown(keyName) {
		If (this.keys[keyName].isDown == False) {
			this.keys[keyName].isDown := True
			this.Mark(keyName, True)

			If (this.debugMode) {
				this.DebugLog("OnKeyDown(" . keyName . ")")
			}
		}
	}

	OnKeyUp(keyName) {
		If (this.keys[keyName].isDown == True) {
			this.keys[keyName].isDown := False
			this.Mark(keyName, False)

			If (this.debugMode) {
				this.DebugLog("OnKeyUp(" . keyName . ")")
			}
		}
	}

	Mark(keyName, isDown) {
		If (isDown) {
			; GuiControl, Show, % this.keys[keyName].hwnd, 
			CtlColors.Change(this.keys[keyName].hwnd, this.activeKeyBgColor, this.activeKeyTextColor)
		}
		Else {
			; GuiControl, Hide, % this.keys[keyName].hwnd, 
			CtlColors.Change(this.keys[keyName].hwnd, this.idleKeyBgColor, this.idleKeyTextColor)
		}
	}

	GuiMove() {
		PostMessage, % this.WINDOWS_MESSAGES.WM_NCLBUTTONDOWN, 2, , , A
	}

	DebugLog(msg) {
		FormatTime, timeStr, %A_Now%, Time
		text = [%timeStr%] %msg%`n
		FileAppend, %text%, iwbtiv.log
	}

	ToggleDebugMode() {
		this.debugMode := !this.debugMode
		Menu, IWannaInputViewerContextMenu, % this.debugMode ? "Check" : "Uncheck", % "Debug Mode"
		Return this.debugMode
	}
}

IWannaInputViewerGuiEscape() {
	IWannaInputViewerGuiClose()
}
IWannaInputViewerGuiClose() {
	ExitApp
}
IWannaInputViewerGuiContextMenu() {
	Menu, IWannaInputViewerContextMenu, Show
}

IWannaInputViewer.NewGui()
IWannaInputViewer_Unicode.ahk

Code: Select all

STRINGS := { UP_ARROW: "↑"
	, DOWN_ARROW: "↓"
	, LEFT_ARROW: "←"
	, RIGHT_ARROW: "→" }

Actually I have fixed the problem using a temporary way. If I create two Text control for each key, one for the idle status, one for the pressed down status, and use GuiControl, Show and GuiControl, Hide to show or hide the pressed down status one, then the program will work all the way. I'm just kind of not satisfied with this solution :/
Hatsuko
Posts: 15
Joined: 09 May 2018, 16:56

Re: [CLASS] CtlColors - color your controls (2017-10-30)

14 Dec 2018, 21:43

Portwolf wrote:
10 Dec 2018, 12:03
And i failed miserably..
I cannot understand how to do it..

Can you throw a bone a little further? :D

I've got a program that changes the Gui controls based on a verification and then changes the value of the GUI Control accordingly.

Code: Select all

Gui, Add, Edit, x20 h19 y40 w40 hwndHTX1 +Center vHeaderStatus
Gui, Add, Edit, x20 h19 y60 w40 hwndHTX2 +Center vMenuStatus
Gui, Add, Edit, x20 h19 y80 w40 hwndHTX3 +Center vContentStatus
Gui, Add, Edit, x20 h19 y100 w40 hwndHTX4 +Center vFooterStatus
(...)
And i use this function to check them:

Code: Select all

getElementById(obj,ele,ByRef tracker, ByRef failelement){
	if obj.document.getElementById(ele){
		GuiControl,,tracker, OK
		return True
	}
else {
	GuiControl,,tracker, FAIL
	SoundBeep, 500, 100
	return False
}
}
How do i set that up? i already added the hwndHTX but, cannot get it working.
I'm seeing a lot of code there that i almost dont understand..
Like many here, im pretty new to this.
Basically you need to use hwnd to access the control you want to change color. For example:

Code: Select all

CtlColors.Change(HTX1, "00FF00", "FFFFFF")
would change the control associated with HTX1 to green background and white text.

If you want to change "OK" and "FAIL"'s color, you need their hwnd as well.
just me
Posts: 9442
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: [CLASS] CtlColors - color your controls (2017-10-30)

15 Dec 2018, 05:46

Hello Hatsuko,
3. Somehow the program is still running. For example, it can still print logs. ...
Does it actually log calls of OnKeyDown/OnKeyUp() after it stopped working?
Hatsuko
Posts: 15
Joined: 09 May 2018, 16:56

Re: [CLASS] CtlColors - color your controls (2017-10-30)

15 Dec 2018, 20:50

just me wrote:
15 Dec 2018, 05:46
Does it actually log calls of OnKeyDown/OnKeyUp() after it stopped working?
Yes, it actually logs calls of OnKeyDown/OnKeyUp() after it stops working.

I just tried it again and I found something even stranger. I added a ToolTip, % "OnKeyDown(" . keyName . ")" inside OnKeyDown/OnKeyUp() and I kept using it until it stopped working. After it stopped working, it continued logging the correct messages, and it continued showing tooltips, but the content of tooltips became empty. I did not know what happened... I might try it again.

Is a screen recording helpful? If so I can post it. It'll be a long video though.
Portwolf
Posts: 161
Joined: 08 Oct 2018, 12:57

Re: [CLASS] CtlColors - color your controls (2017-10-30)

20 Dec 2018, 08:20

Hatsuko wrote:
14 Dec 2018, 21:43
Portwolf wrote:
10 Dec 2018, 12:03
(...)
Basically you need to use hwnd to access the control you want to change color. For example:

Code: Select all

CtlColors.Change(HTX1, "00FF00", "FFFFFF")
would change the control associated with HTX1 to green background and white text.

If you want to change "OK" and "FAIL"'s color, you need their hwnd as well.
Hi and im sorry for late reply, lots of work over here... :)
I already tried that.
I tried using an "If" on the function, trying to change the fields as the checking goes along:

Code: Select all

getElementById(obj,ele,ByRef tracker, ByRef failelement, colorizer){
	if obj.document.getElementById(ele){
		GuiControl,,tracker, OK
		CtlColors.Change(colorizer, "green", "FFFFFF")
		return True
	}
else {
	GuiControl,,tracker, FAIL
	SoundBeep, 500, 100
	CtlColors.Change(colorizer, "red", "FFFFFF")
	return False
}
}
The colorizer was then calling the hwnd on the function with:

Code: Select all

   getElementById(WB,"header",HeaderStatus,"Header on login page",Color1)

Code: Select all

Gui, Add, Edit, x20 h19 y40 w40 hwndColor1 +Center vHeaderStatus
Problem is that as the checks went on, fields with OK and FAIL were getting the same color.
Sometimes, they were getting colored randomly even before getting the result in them.
Hatsuko
Posts: 15
Joined: 09 May 2018, 16:56

Re: [CLASS] CtlColors - color your controls (2017-10-30)

23 Dec 2018, 07:08

Hatsuko wrote:
15 Dec 2018, 20:50
just me wrote:
15 Dec 2018, 05:46
Does it actually log calls of OnKeyDown/OnKeyUp() after it stopped working?
Yes, it actually logs calls of OnKeyDown/OnKeyUp() after it stops working.

I just tried it again and I found something even stranger. I added a ToolTip, % "OnKeyDown(" . keyName . ")" inside OnKeyDown/OnKeyUp() and I kept using it until it stopped working. After it stopped working, it continued logging the correct messages, and it continued showing tooltips, but the content of tooltips became empty. I did not know what happened... I might try it again.

Is a screen recording helpful? If so I can post it. It'll be a long video though.
I just tried it again and got the same result. I have recorded a video.
Spoiler


Below are the code I used in the video (the same as my previous post, except that I added Tooltip)

IWannaInputViewer.ahk

Code: Select all

#SingleInstance Force
#NoEnv
; #Warn

#Include <Class_CtlColors>

class IWannaInputViewer {
	static config := { Left: { options: "x129 y97 w30 h30", text: "" }  ; Designed in AutoGUI 2.5.4
		, Right: { options: "x193 y97 w30 h30", text: "" }
		, Up: { options: "x161 y65 w30 h30", text: "" }
		, Down: { options: "x161 y97 w30 h30", text: "" }
		, LShift: { options: "x1 y65 w62 h30", text: "Shift" }
		, RShift: { options: "x97 y65 w62 h30", text: "Shift" }
		, Z: { options: "x65 y65 w30 h30", text: "Z" }
		, R: { options: "x65 y1 w30 h30", text: "R" }
		, S: { options: "x65 y33 w30 h30", text: "S" }
		, Q: { options: "x33 y1 w30 h30", text: "Q" }
		, P: { options: "x97 y1 w30 h30", text: "P" }
		, Enter: { options: "x129 y33 w62 h30", text: "Enter" }
		, Backspace: { options: "x129 y1 w62 h30", text: "Back" }
		, F2: { options: "x1 y1 w30 h30", text: "F2" }
		, CapsLock: { options: "x1 y33 w62 h30", text: "Caps" }
		, RControl: { options: "x97 y97 w30 h30", text: "Ctrl" } }

	static commonOptions := "+Center +0x200"  ; +Border


	static fontName := "Envy Code R"
	static fontOptions := "s9 q3"

	static windowTitle := "I wanna be the Input Viewer"
	static windowW := 224, windowH := 128

	static windowBgColor := "000000"
	static idleKeyBgColor := "333333"
	static idleKeyTextColor := "666666"
	static activeKeyBgColor := "666666"
	static activeKeyTextColor := "CCCCCC"

	static classInitialized := IWannaInputViewer.ClassInit()
	static debugMode := False

	ClassInit() {
		#Include %A_LineFile%/../IWannaInputViewer_Unicode.ahk
		this.config.Left.text := STRINGS.LEFT_ARROW
		this.config.Right.text := STRINGS.RIGHT_ARROW
		this.config.Up.text := STRINGS.UP_ARROW
		this.config.Down.text := STRINGS.DOWN_ARROW
		Return True
	}

	class Key {
		isDown := False
		hwnd :=
		__New(hwnd := "") {
			If (hwnd != "") {
				this.hwnd := hwnd
			}
		}
	}

	static keys := {}

	; https://autohotkey.com/docs/misc/SendMessageList.htm
	static WINDOWS_MESSAGES := { WM_LBUTTONDOWN: 0x201, WM_NCLBUTTONDOWN: 0xA1 }

	NewGui() {
		Gui, IWannaInputViewer:New, +AlwaysOnTop, % this.windowTitle

		Gui, Font, % this.fontOptions, % this.fontName, 
		Gui, Color, % this.windowBgColor

		For k, v in this.config {
			keyName := k
			options := v.options . " " . this.commonOptions . " Hwndh"
			text := v.text

			Gui, Add, Text, %options%, %text%
			this.keys[keyName] := new IWannaInputViewer.Key(h)

			; Color it
			CtlColors.Attach(h, this.idleKeyBgColor, this.idleKeyTextColor)

			; Create hotkeys
			funcObj := ObjBindMethod(this, "OnKeyDown", keyName)
			Hotkey, ~*%keyName%, %funcObj%
			funcObj := ObjBindMethod(this, "OnKeyUp", keyName)
			Hotkey, ~*%keyName% up, %funcObj%
		}

		options := "w" . this.windowW . " h" . this.windowH
		Gui, Show, %options%, 

		funcObj := ObjBindMethod(this, "GuiMove")
		OnMessage(this.WINDOWS_MESSAGES.WM_LBUTTONDOWN, funcObj)

		funcObj := ObjBindMethod(this, "ToggleDebugMode")
		Menu, IWannaInputViewerContextMenu, Add, % "Debug Mode", %funcObj%
	}

	OnKeyDown(keyName) {
		If (this.keys[keyName].isDown == False) {
			this.keys[keyName].isDown := True
			this.Mark(keyName, True)

			If (this.debugMode) {
				this.DebugLog("OnKeyDown(" . keyName . ")")
			}
			ToolTip, % "OnKeyDown(" . keyName . ")"
		}
	}

	OnKeyUp(keyName) {
		If (this.keys[keyName].isDown == True) {
			this.keys[keyName].isDown := False
			this.Mark(keyName, False)

			If (this.debugMode) {
				this.DebugLog("OnKeyUp(" . keyName . ")")
			}
			ToolTip, % "OnKeyUp(" . keyName . ")"
		}
	}

	Mark(keyName, isDown) {
		If (isDown) {
			; GuiControl, Show, % this.keys[keyName].hwnd, 
			CtlColors.Change(this.keys[keyName].hwnd, this.activeKeyBgColor, this.activeKeyTextColor)
		}
		Else {
			; GuiControl, Hide, % this.keys[keyName].hwnd, 
			CtlColors.Change(this.keys[keyName].hwnd, this.idleKeyBgColor, this.idleKeyTextColor)
		}
	}

	GuiMove() {
		PostMessage, % this.WINDOWS_MESSAGES.WM_NCLBUTTONDOWN, 2, , , A
	}

	DebugLog(msg) {
		FormatTime, timeStr, %A_Now%, Time
		text = [%timeStr%] %msg%`n
		FileAppend, %text%, iwbtiv.log
	}

	ToggleDebugMode() {
		this.debugMode := !this.debugMode
		Menu, IWannaInputViewerContextMenu, % this.debugMode ? "Check" : "Uncheck", % "Debug Mode"
		Return this.debugMode
	}
}

IWannaInputViewerGuiEscape() {
	IWannaInputViewerGuiClose()
}
IWannaInputViewerGuiClose() {
	ExitApp
}
IWannaInputViewerGuiContextMenu() {
	Menu, IWannaInputViewerContextMenu, Show
}

IWannaInputViewer.NewGui()
IWannaInputViewer_Unicode.ahk

Code: Select all

STRINGS := { UP_ARROW: "↑"
	, DOWN_ARROW: "↓"
	, LEFT_ARROW: "←"
	, RIGHT_ARROW: "→" }


Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: NPerovic and 51 guests