different approach
#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <WinAPIDlg.au3>
_Main()
Func _Main()
Local $hGUI = GUICreate("Example", 250, 100)
GUICtrlCreateLabel("Type name here (max 12 chars)", 20, 20)
Local $idEditName = GUICtrlCreateEdit("", 20, 40, 160, 20, $ES_AUTOHSCROLL)
GUICtrlSetLimit($idEditName, 12)
Local $idEditResult = GUICtrlCreateEdit("", 20, 65, 160, 20, $ES_READONLY, 0)
GUISetState(@SW_SHOW)
While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
ExitLoop
EndSwitch
; Check if the active control is $idEditName
If _WinAPI_GetDlgCtrlID(ControlGetHandle($hGUI, "", ControlGetFocus($hGUI))) = $idEditName Then
Local $sValue = _InputLimiter($idEditName)
If $sValue <> "" Then GUICtrlSetData($idEditResult, $sValue)
EndIf
WEnd
EndFunc
Func _InputLimiter($iCtrlInput, $iMaxBaseLength = 12, $sSuffix = "-PC")
Local Static $sLastValue
Local $sCurrentValue = GUICtrlRead($iCtrlInput)
If $sCurrentValue <> $sLastValue Then
$sLastValue = $sCurrentValue
Return StringLeft($sCurrentValue, $iMaxBaseLength) & $sSuffix
EndIf
Return ""
EndFunc
Edit:
variant with more advanced limiter
Func _InputLimiter($iCtrlInput, $iMaxBaseLength = 12, $sSuffix = "-PC")
Local Static $sLastValue
Local $sCurrentValue = GUICtrlRead($iCtrlInput)
Local $sFilteredValue = StringRegExpReplace($sCurrentValue, "[^a-zA-Z0-9]", "")
If $sFilteredValue <> $sCurrentValue Then
GUICtrlSetData($iCtrlInput, $sFilteredValue)
Return ""
EndIf
If $sFilteredValue <> $sLastValue Then
$sLastValue = $sFilteredValue ; Update static
ConsoleWrite("$sCurrentValue=" & $sCurrentValue & @CRLF) ; For debugging
Return StringLeft($sFilteredValue, $iMaxBaseLength) & $sSuffix
EndIf
Return ""
EndFunc