#include "IsPressedCallback.au3"
#include <GDIPlus.au3>

Local $aKeycodes[6] = ["51", "57", "45", "52", "54", "5A"] ;QWERTZ
Local $aKeynames[6] = ["Q", "W", "E", "R", "T", "Z"] ;QWERTZ
Local $iPressColor = 0xff0000ff
Local $iReleaseColor = 0xffff0000

_GDIPlus_Startup()
Opt("GUIOnEventMode", True)
Opt("TrayIconDebug", True)

$hGUI = GUICreate("QWERTZ-Keyboard", 50 * 6, 50)
GUISetOnEvent(-3, "_Exit")
GUISetState()

;GDI+ Init
$hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGUI)
$hBufferBMP = _GDIPlus_BitmapCreateFromGraphics(50 * 6, 50, $hGraphics)
$hBuffer = _GDIPlus_ImageGetGraphicsContext($hBufferBMP)

Local $aBrushes[UBound($aKeycodes)]

For $i = 0 To UBound($aKeycodes) - 1
	$aBrushes[$i] = _GDIPlus_BrushCreateSolid($iReleaseColor)
	_IsPressed_CallbackRegister($aKeycodes[$i], "_ChangeColor", $IPC_DOWN + $IPC_UP)
Next

While True
	_Draw()
WEnd

Func _ChangeColor($sKey, $iEvent)
	Switch $iEvent
		Case $IPC_DOWN
			$iNewColor = $iPressColor
		Case $IPC_UP
			$iNewColor = $iReleaseColor
	EndSwitch
	For $i = 0 To UBound($aKeycodes) - 1
		If $sKey = $aKeycodes[$i] Then
			_GDIPlus_BrushSetSolidColor($aBrushes[$i], $iNewColor)
		EndIf
	Next
	_Draw()
EndFunc   ;==>_ChangeColor

Func _Draw()
	_GDIPlus_GraphicsClear($hBuffer, 0xffffffff)
	For $i = 0 To UBound($aKeycodes) - 1
		_GDIPlus_GraphicsFillRect($hBuffer, 50 * $i, 0, 50, 50, $aBrushes[$i])
		_GDIPlus_GraphicsDrawString($hBuffer, $aKeynames[$i], 50 * $i, 0, "Arial", 34)
	Next
	_GDIPlus_GraphicsDrawImageRect($hGraphics, $hBufferBMP, 0, 0, 50 * 6, 50)
EndFunc   ;==>_Draw

Func _Exit()
	_GDIPlus_DisposeAll()
	_GDIPlus_Shutdown()
	Exit
EndFunc   ;==>_Exit

Func _GDIPlus_DisposeAll()
	;Common GDI+ Dispose Function created by CleanGDI+.au3
	_GDIPlus_GraphicsDispose($hGraphics)
	_GDIPlus_BitmapDispose($hBufferBMP)
	_GDIPlus_GraphicsDispose($hBuffer)
	For $i = 0 To UBound($aBrushes) - 1
		_GDIPlus_BrushDispose($aBrushes[$i])
	Next
EndFunc   ;==>_GDIPlus_DisposeAll