Jump to content

How can I create an input format to be followed while typing


Recommended Posts

Hello all,

I am currently trying to create a script for setup automation, one of the things I want it to do is allow the user to type in the activation key for windows which I will then use to activate windows.

My question is, how do I create an input format that is enforced while the user is typing? Example:

The user will type their activation key in as such: hgjfkhgjfkhgjfkhgjfkhgjfkhgjfk

While the user is typing, dashes will automatically be entered if the user doesn't include them so that when the user is done typing it will actually be: hgjfk-hgjfk-hgjfk-hgjfk-hgjfk

I thought to monitor input and every 5 keys to input a dash if the 6th key isn't a dash but I was hoping there would be an easier way?

Edited by adamb53
Link to comment
Share on other sites

I found this code in my collections (might be "old"):

;by UEZ
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Misc.au3>
Opt("GUIOnEventMode", 1)

$dll = DllOpen("user32.dll")
$hGUI = GUICreate("Seriennummer", 232, 90)
$input = GUICtrlCreateInput("", 42, 30, 140)
$Label = GUICtrlCreateLabel("Format: XXXX-XXXX-XXXX-XXXX", 32, 8, 163, 17)
GUICtrlSetLimit($input, 19)
$Button = GUICtrlCreateButton("Weiter", 144, 56, 75, 25, $WS_GROUP)
GUICtrlSetState(-1, $GUI_DISABLE)
GUISetState()
$iLen_prev = 0
$iCurPos = 0
GUIRegisterMsg($WM_COMMAND, '_WM_COMMAND')
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

While Sleep(10)
    $aCaret_pos = WinGetCaretPos()
    If _IsPressed("25", $dll) Or _IsPressed("26", $dll) Then ;left or up
        Send("{RIGHT}")
    ElseIf _IsPressed("27", $dll) Or _IsPressed("28", $dll) Then ;right or down
        DllCall("User32.dll", 'int', 'SetCaretPos', 'int', $aCaret_pos[0], 'int', $aCaret_pos[1])
    EndIf
    $aPos = GUIGetCursorInfo($hGUI)
    If $aPos[2] And $aPos[4] = $input Then
        ControlFocus("", "", $input)
        Send("{Right 50}")
    EndIf

WEnd

Func _Exit()
    DllClose($dll)
    Exit
EndFunc

Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    If BitAND($wParam, 0x0000FFFF) = $input Then
        GUICtrlSetData($input, StringUpper(GUICtrlRead($input))) ;upper letters
        $i = GUICtrlRead($input)
        $iLen = StringLen($i) + 1
        If Mod($iLen, 5) = 0 And $iLen > $iLen_prev And $iLen < 20 Then GUICtrlSetData($input, $i & "-")
        If Mod($iLen, 5) = 0 And $iLen < $iLen_prev Then GUICtrlSetData($input, StringLeft($i, $iLen - 2))
        $iLen_prev = $iLen
        If $iLen = 20 Then
            GUICtrlSetState($Button, $GUI_ENABLE)
        Else
            GUICtrlSetState($Button, $GUI_DISABLE)
        EndIf
    EndIf
    Return $GUI_RUNDEFMSG
EndFunc

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Subclassing might be faster

#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <WinAPI.au3>

GUICreate("Test", 500, 200)
Global Const $iInput = GUICtrlCreateInput("", 10, 10, 480)

GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
GUISetState()


Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)

    $iNotification = _WinAPI_HiWord($wParam)
    $iControlID = _WinAPI_LoWord($wParam)

    Switch $iControlID
        Case $iInput

            Switch $iNotification
                Case $EN_CHANGE

                    $s_Text = GUICtrlRead($iControlID)

                    $s_Text = StringRegExpReplace($s_Text, "(-|^)([^\-]{5})([^\-])", "\1\2-\3")
                    $s_Text = StringRegExpReplace($s_Text, "-$", "")

                    GUICtrlSetData($iControlID, $s_Text)


            EndSwitch

    EndSwitch

EndFunc   ;==>WM_COMMAND

Do
    Sleep(10)
Until GUIGetMsg() = $GUI_EVENT_CLOSE

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

  • Moderators

PhoenixXL,

 

That is not "subclassing" - you do not change the WndProc for the control. ;)

 

is subclassing. :)

 

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Both do exactly what I need :) Thanks to both of you!

I like the second suggestion as it is a lot cleaner but the first has the character limit, I may combine the two so I can implement a character limit as well.

Thanks heaps again!

Link to comment
Share on other sites

Subclassing might be faster

I'm saying the same,

Subclassing would be faster than the method I have used.

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

  • Moderators

PheonixXL,

My apologies, I misunderstood your comment as referring to the code you posted. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

I like the second suggestion as it is a lot cleaner but the first has the character limit, I may combine the two so I can implement a character limit as well.

I have added a character limit. check the following

#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <WinAPI.au3>

GUICreate("Test", 500, 200)
Global Const $iInput = GUICtrlCreateInput("", 10, 10, 480)
Global Const $iMinChars = 6 ;the minimum no. of chars after which a dash has to be inserted.
Global Const $iMaxDashes = 5 ;this will block the user from entering more chars in the input
;for example if no. of dashes are two there can be maximum only 15 non dash characters.

GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
GUISetState()


Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)

    Static $s_InputData_Previous
    Local $s_Temp
    $iNotification = _WinAPI_HiWord($wParam)
    $iControlID = _WinAPI_LoWord($wParam)

    Switch $iControlID
        Case $iInput

            Switch $iNotification
                Case $EN_CHANGE

                    $s_Text = GUICtrlRead($iControlID)
                    $s_Temp = $s_Text ;store the real data in this var
                    $s_Text = StringReplace($s_Text, "-", "")

                    If StringLen($s_Text) > (($iMaxDashes + 1) * $iMinChars) Then Return GUICtrlSetData($iControlID, $s_InputData_Previous)
                    $s_InputData_Previous = $s_Temp

                    Do
                        $s_Text = StringRegExpReplace($s_Text, "(-|^)([^\-]{" & $iMinChars & "})([^\-])", "\1\2-\3")
                    Until @extended = 0

                    GUICtrlSetData($iControlID, $s_Text)


            EndSwitch

    EndSwitch

EndFunc   ;==>WM_COMMAND

Do
    Sleep(10)
Until GUIGetMsg() = $GUI_EVENT_CLOSE 

The caret will go to the last char. Let me know if that is a problem.

Regards :)

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

Subclassed version. - faster and without any flickers.

#include <GUIConstants.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <WinAPI.au3>

; ==== User Variables ========
Global Const $iMinChars = 6 ;the minimum no. of chars after which a dash has to be inserted.
Global Const $iMaxDashes = 5 ;this will block the user from entering more chars in the input.
;for example if no. of dashes are 2 and minimum no. of chars are 5 there can be maximum only 15 non dash characters.
; ==== User Variables End ====

GUICreate("Test", 500, 200)
Global Const $iInput = GUICtrlCreateInput("", 10, 10, 400)

Global Const $iChrCode_Dash = 45 ;character code of Dash.
Global Const $iChrCode_BackSpace = 08 ;character code of Backspace.

; Unregister, Unsubclass upon Exitting.
OnAutoItExitRegister("_AutoExit")

; Register callback function and obtain handle to _New_WndProc.
Global $hNew_WndProc = DllCallbackRegister("_New_WndProc", "int", "hwnd;uint;wparam;lparam")

; Get pointer to _New_WndProc
Global $pNew_WndProc = DllCallbackGetPtr($hNew_WndProc), $pOld_WndProc = _Edit_SubClass(GUICtrlGetHandle($iInput), $pNew_WndProc)

GUISetState()


Func _New_WndProc($hWnd, $iMsg, $wParam, $lParam)

    Switch $iMsg
        Case $WM_CHAR

            $iControlID = _WinAPI_GetDlgCtrlID($hWnd)

            $s_Text = GUICtrlRead($iControlID) ;the characters before the msg was sent

            Switch $wParam ;character code of the key
                Case $iChrCode_Dash
                    Return 1 ;block the processing of the Dash Key

                Case $iChrCode_BackSpace
                    $s_Text = StringTrimRight($s_Text, 1) ;delete the last char.
                    If StringRight($s_Text, 1) = "-" Then $s_Text = StringTrimRight($s_Text, 1) ;if the last char is dash delete it.

                Case Else

                    ;process the key
                    $s_Text &= ChrW($wParam)

            EndSwitch

            $s_Text = StringRegExpReplace($s_Text, "(-|^)([^\-]{" & $iMinChars & "})([^\-])", "\1\2-\3")

            ;block the processing if limit full
            If StringLen($s_Text) > (($iMaxDashes + 1) * $iMinChars) + $iMaxDashes Then Return 1

            GUICtrlSetData($iControlID, $s_Text)

            Return 1 ;block the default processing.

    EndSwitch

    Return _WinAPI_CallWindowProc($pOld_WndProc, $hWnd, $iMsg, $wParam, $lParam)

EndFunc   ;==>_New_WndProc

Do
    Sleep(10)
Until GUIGetMsg() = $GUI_EVENT_CLOSE

Func _AutoExit()
    ;unsubclass
    _Edit_SubClass(GUICtrlGetHandle($iInput), $pOld_WndProc)
    ; Now free created WndProc
    DllCallbackFree($hNew_WndProc)
EndFunc   ;==>_AutoExit

Func _Edit_SubClass($hWnd, $pNew_WindowProc)
    Local $iRes = _WinAPI_SetWindowLong($hWnd, -4, $pNew_WindowProc)
    If @error Then Return SetError(1, 0, 0)
    If $iRes = 0 Then Return SetError(1, 0, 0)
    Return SetError(0, 0, $iRes)
EndFunc   ;==>_Edit_SubClass
Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...