#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>

; Beispiel (Anfang)
$hMainGui = GUICreate('Testfenster (ist während PinInput gesperrt)', 600, 360)
$hButton = GUICtrlCreateButton('Test', 10, 10, 100, 25)
$hInput = GUICtrlCreateInput('', 120, 10, 200, 25)
GUISetState()
$Pin = _PinInput(6, False, $hMainGui)
If $Pin <> '241211' Then Exit MsgBox(16, 'Test', 'Falscher Pin-Code! Programm wird beendet.', 3, $hMainGui)
GUICtrlSetData($hInput, $Pin)
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
; Beispiel (Ende)

;===============================================================================
; Function Name:   _PinInput([$iCount = 4][, $bBeep = True][, $hParent = ''])
; Description::    Eingabe eines Pin-Codes (1-10stellig)
; Parameter(s):    $iCount = Wie viele Stellen muss der Benutzer eingeben (1-10)
;                  $bBeep = Sound ein-/ausschalten (True/False)
;                  $hParent = Handle des Parent-Windows (dieses wird während
;                             der Eingabe deaktiviert).
; Requirement(s):
;                  #include <GUIConstantsEx.au3>
;                  #include <StaticConstants.au3>
;                  #include <WindowsConstants.au3>
;                  #include <ButtonConstants.au3>

; Return Value(s): Wenn Eingabe mit [ENTER] abgeschlossen wurde, wird der Pin-Code
;                  zurückgegeben, ansonsten ein Leerstring und @error = 1
; Author(s):       Oscar (www.autoit.de)
;===============================================================================
Func _PinInput($iCount = 4, $bBeep = True, $hParent = '')
	If $iCount > 10 Then $iCount = 10
	If $iCount < 1 Then $iCount = 4
	Local $iOldEventMode = Opt('GUIOnEventMode', 0), $msg, $sReturn, $sOut
	Local $aButtons[12] = ['7', '8', '9', '4', '5', '6', '1', '2', '3', 'C', '0', '<-']
	Local $aKeys1[12] = [ _
			'{NUMPAD7}', '{NUMPAD8}', '{NUMPAD9}', _
			'{NUMPAD4}', '{NUMPAD5}', '{NUMPAD6}', _
			'{NUMPAD1}', '{NUMPAD2}', '{NUMPAD3}', _
			'{DELETE}', '{NUMPAD0}', '{ENTER}']
	Local $aKeys2[12] = [ _
			'7', '8', '9', _
			'4', '5', '6', _
			'1', '2', '3', _
			'{DELETE}', '0', '{ENTER}']
	Local $aAccelKeys[24][2], $ahButtons[12]
	If IsHWnd($hParent) Then GUISetState(@SW_DISABLE, $hParent)
	Local $hGui = GUICreate('Pin eingeben (' & $iCount & ' stellig)', 224, 200, Default, Default, BitOR($WS_CAPTION, $WS_POPUPWINDOW), Default, $hParent)
	GUISetFont(14, 600, 0, 'Verdana')
	GUISetIcon('shell32.dll', -48)
	Local $hOutput = GUICtrlCreateLabel('', 16, 10, 192, 34, $SS_RIGHT, $WS_EX_STATICEDGE)
	GUICtrlSetFont(-1, 22, 400)
	GUICtrlSetBkColor(-1, 0xFFDDAA)
	For $i = 0 To 11
		$ahButtons[$i] = GUICtrlCreateButton($aButtons[$i], 16 + Mod($i, 3) * 64, 54 + Int($i / 3) * 34, 64, 34, $BS_ICON)
		$aAccelKeys[$i][0] = $aKeys1[$i]
		$aAccelKeys[$i][1] = $ahButtons[$i]
		$aAccelKeys[$i+12][0] = $aKeys2[$i]
		$aAccelKeys[$i+12][1] = $ahButtons[$i]
	Next
	GUICtrlSetImage($ahButtons[9], 'shell32.dll', -132, 0)
	GUICtrlSetImage($ahButtons[11], 'shell32.dll', -145, 0)
	GUICtrlSetState($ahButtons[11], $GUI_DISABLE)
	GUISetAccelerators($aAccelKeys, $hGui)
	GUISetState()
	While True
		$msg = GUIGetMsg()
		Switch $msg
			Case $GUI_EVENT_CLOSE
				If IsHWnd($hParent) Then GUISetState(@SW_ENABLE, $hParent)
				GUIDelete($hGui)
				Opt('GUIOnEventMode', $iOldEventMode)
				If $bBeep Then Beep(200, 400)
				Return SetError(1, 0, '')
			Case $ahButtons[11]
				If StringLen($sOut) = $iCount Then
					If $hParent <> '' Then GUISetState(@SW_ENABLE, $hParent)
					GUIDelete($hGui)
					Opt('GUIOnEventMode', $iOldEventMode)
					If $bBeep Then Beep(800, 200)
					Return SetError(0, 0, $sOut)
				EndIf
			Case $ahButtons[9]
				GUICtrlSetState($hOutput, $GUI_FOCUS)
				GUICtrlSetData($hOutput, '')
				GUICtrlSetState($ahButtons[11], $GUI_DISABLE)
				$sOut = ''
				If $bBeep Then Beep(400, 200)
			Case $ahButtons[0] To $ahButtons[10]
				If StringLen($sOut) < $iCount Then
					$sOut &= $aButtons[$msg - $ahButtons[0]]
					GUICtrlSetData($hOutput, StringLeft('**********', StringLen($sOut)))
					If $bBeep Then Beep(600, 100)
				Else
					If $bBeep Then Beep(200, 200)
				EndIf
				If StringLen($sOut) = $iCount And BitAND(GUICtrlGetState($ahButtons[11]), $GUI_DISABLE) Then GUICtrlSetState($ahButtons[11], $GUI_ENABLE)
		EndSwitch
	WEnd
EndFunc

