Numeric Password…

Share your advanced PureBasic knowledge/code with the community.
User avatar
Piero
Addict
Addict
Posts: 894
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Numeric Password…

Post by Piero »

Code: Select all

Global StringGadgetFlags = #PB_String_Numeric ; set to 0 for normal password
#pwtooltip = "Password Input Field" ; use after creating StringGadget
Procedure ShowHidePass(StringGadget, activate = #True)
   Protected x,y,w,h,s.s
   x=GadgetX(StringGadget)
   y=GadgetY(StringGadget)
   w=GadgetWidth(StringGadget)
   h=GadgetHeight(StringGadget)
   s=GetGadgetText(StringGadget)
   FreeGadget(StringGadget)
   StringGadget(StringGadget,x,y,w,h,s,StringGadgetFlags)
   GadgetToolTip(StringGadget,#pwtooltip)
   If activate: SetActiveGadget(StringGadget) :EndIf
   StringGadgetFlags ! #PB_String_Password
EndProcedure

OpenWindow(0, 0, 0, 250, 90, "Numeric Password")
StringGadget(0, 40, 20, 180, 20, "1234", StringGadgetFlags | #PB_String_Password)
GadgetToolTip(0, #pwtooltip )
CheckBoxGadget(1, 40, 55, 180, 25, "Show Password")
SetActiveGadget(0)
Repeat
   Select WaitWindowEvent()
      Case #PB_Event_CloseWindow : Break
      Case #PB_Event_Gadget
         If EventGadget() = 1
            ShowHidePass(0)
         EndIf
         CompilerIf #PB_Compiler_OS = #PB_OS_Windows
            If EventType()=#PB_EventType_Focus And GadgetType(EventGadget())=#PB_GadgetType_String
               SendMessage_(GadgetID(EventGadget()),#EM_SETSEL,0,-1)
            EndIf
         CompilerEndIf      
   EndSelect
ForEver
Last edited by Piero on Fri Aug 22, 2025 7:03 am, edited 1 time in total.
User avatar
idle
Always Here
Always Here
Posts: 5874
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Numeric Password…

Post by idle »

Thanks that's handy.
User avatar
Piero
Addict
Addict
Posts: 894
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Numeric Password…

Post by Piero »

idle wrote: Fri Aug 22, 2025 6:40 am Thanks that's handy.
YW :)

PS: I just added a "fix" for windoze ;)
Axolotl
Addict
Addict
Posts: 824
Joined: Wed Dec 31, 2008 3:36 pm

Re: Numeric Password…

Post by Axolotl »

Thanks for sharing.

BTW: On Window I prefer another 'fix' called API constant #EM_SETPASSWORDCHAR :oops:

Code: Select all

Global StringGadgetFlags = #PB_String_Numeric ; set to 0 for normal password
#pwtooltip = "Password Input Field" ; use after creating StringGadget

CompilerIf #PB_Compiler_OS = #PB_OS_Windows

Procedure DisableStringGadgetPassword(Gadget, State) 
  If State 
    SendMessage_(GadgetID(Gadget), #EM_SETPASSWORDCHAR, 0, 0)
  Else 
    SendMessage_(GadgetID(Gadget), #EM_SETPASSWORDCHAR, '●', 0) 
  EndIf 
EndProcedure 

CompilerElse ; #PB_Compiler_OS = #ALL_THE_OTHERS 

Procedure ShowHidePass(StringGadget, activate = #True)
   Protected x,y,w,h,s.s
   x=GadgetX(StringGadget)
   y=GadgetY(StringGadget)
   w=GadgetWidth(StringGadget)
   h=GadgetHeight(StringGadget)
   s=GetGadgetText(StringGadget)
   FreeGadget(StringGadget)
   StringGadget(StringGadget,x,y,w,h,s,StringGadgetFlags)
   GadgetToolTip(StringGadget,#pwtooltip)
   If activate: SetActiveGadget(StringGadget) :EndIf
   StringGadgetFlags ! #PB_String_Password
EndProcedure

CompilerEndIf      

OpenWindow(0, 0, 0, 250, 90, "Numeric Password") 
StringGadget(0, 40, 20, 180, 20, "1234", StringGadgetFlags | #PB_String_Password) ; #PB_String_Numeric  
GadgetToolTip(0, #pwtooltip )
CheckBoxGadget(1, 40, 55, 180, 25, "Show Password")
SetActiveGadget(0)
Repeat
   Select WaitWindowEvent()
      Case #PB_Event_CloseWindow : Break

      Case #PB_Event_Gadget
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
        If EventGadget() = 1
          DisableStringGadgetPassword(0, GetGadgetState(1)) 
          SetActiveGadget(0) 
        EndIf 
CompilerElse 
       If EventGadget() = 1
        ShowHidePass(0)
       EndIf
       CompilerIf #PB_Compiler_OS = #PB_OS_Windows
          If EventType()=#PB_EventType_Focus And GadgetType(EventGadget())=#PB_GadgetType_String
            SendMessage_(GadgetID(EventGadget()),#EM_SETSEL,0,-1)
          EndIf
       CompilerEndIf      
CompilerEndIf      
   EndSelect
ForEver
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
ebs
Enthusiast
Enthusiast
Posts: 560
Joined: Fri Apr 25, 2003 11:08 pm

Re: Numeric Password…

Post by ebs »

@Axolotl,

Thank you! Your tip about about #EM_SETPASSWORDCHAR is exactly what I need.

One question - do you know how I can do this in an ancient ASCII (not UNICODE) executable?
I would have thought that passing the correct value as wParam should be all that's required, but I can't get it to work,.

I read that the black dot UNICODE character is entered by typing ALT+25CF, so I tried $25CF and 9679, but neither one worked.

Is there a way to create the proper value in an ASCII executable? Maybe just encode the password string as UNICODE?
Axolotl
Addict
Addict
Posts: 824
Joined: Wed Dec 31, 2008 3:36 pm

Re: Numeric Password…

Post by Axolotl »

Well, my first (lucky) guess is to use '*' instead. Because the asterisk is a valid ascii char.
Second idea: try to use the #EM_GETPASSWORDCHAR to find out the using char. (should be the result or zero if in not password mode)
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
ebs
Enthusiast
Enthusiast
Posts: 560
Joined: Fri Apr 25, 2003 11:08 pm

Re: Numeric Password…

Post by ebs »

Thanks for your fast reply.

I did try #EM_GETPASSWORDCHAR and guess what it returns? '*' :D

I did find a way to cheat by changing the font to Wingdings and using one of the black dot characters in that font, then switching back to the default font when showing the actual password characters. It (the code) isn't pretty, but it works!
threedslider
Enthusiast
Enthusiast
Posts: 395
Joined: Sat Feb 12, 2022 7:15 pm

Re: Numeric Password…

Post by threedslider »

Thanks @Piero for sharing your code :D

Mine is instead of numeric i put character :), here the code :

Code: Select all

Global StringGadgetFlags = #PB_String ; set to 0 for normal password
#pwtooltip = "Password Input Field" ; use after creating StringGadget
Procedure ShowHidePass(StringGadget, activate = #True)
   Protected x,y,w,h,s.s
   x=GadgetX(StringGadget)
   y=GadgetY(StringGadget)
   w=GadgetWidth(StringGadget)
   h=GadgetHeight(StringGadget)
   s=GetGadgetText(StringGadget)
   FreeGadget(StringGadget)
   StringGadget(StringGadget,x,y,w,h,s,StringGadgetFlags)
   GadgetToolTip(StringGadget,#pwtooltip)
   If activate: SetActiveGadget(StringGadget) :EndIf
   StringGadgetFlags ! #PB_String_Password
EndProcedure

OpenWindow(0, 0, 0, 250, 90, "Character Password")
StringGadget(0, 40, 20, 180, 20, "hello", StringGadgetFlags | #PB_String_Password)
GadgetToolTip(0, #pwtooltip )
CheckBoxGadget(1, 40, 55, 180, 25, "Show Password")
SetActiveGadget(0)
Repeat
   Select WaitWindowEvent()
      Case #PB_Event_CloseWindow : Break
      Case #PB_Event_Gadget
         If EventGadget() = 1
            ShowHidePass(0)
         EndIf
         CompilerIf #PB_Compiler_OS = #PB_OS_Windows
            If EventType()=#PB_EventType_Focus And GadgetType(EventGadget())=#PB_GadgetType_String
               SendMessage_(GadgetID(EventGadget()),#EM_SETSEL,0,-1)
            EndIf
         CompilerEndIf      
   EndSelect
ForEver
Enjoy and happy coding !
User avatar
Piero
Addict
Addict
Posts: 894
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Numeric Password…

Post by Piero »

1) You windoze ppl always complicate simple PB multiplatform solutions with your api stuff :x
2) I must admit that """#EM_SETPASSWORDCHAR""" is the way it should work on all platforms
3) You must admit that your StringGadget text doesn't get selected on focus
:wink: :P

Edit: replaced the colloquial "sh*t" with the synonymous "stuff" to avoid """misinterpretations"""
Last edited by Piero on Sat Aug 23, 2025 12:56 am, edited 1 time in total.
User avatar
Kiffi
Addict
Addict
Posts: 1493
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: Numeric Password…

Post by Kiffi »

Piero wrote: Fri Aug 22, 2025 8:54 pm1) You windoze ppl always complicate simple PB multiplatform solutions with your api sh* :x
Please avoid disrespectful wording like that. Let’s keep the discussion polite and technical.
Hygge
User avatar
Piero
Addict
Addict
Posts: 894
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Numeric Password…

Post by Piero »

Kiffi wrote: Fri Aug 22, 2025 11:32 pm
Piero wrote: Fri Aug 22, 2025 8:54 pm1) You windoze ppl always complicate simple PB multiplatform solutions with your api sh* :x
Please avoid disrespectful wording like that. Let’s keep the discussion polite and technical.
Hey Kiffi, I'm very surprised to hear you "took my words as disrespectful"
I also use win, but that's not the point: I'm not the only one that thinks that ( viewtopic.php?p=644313#p644313 )
Anyway I have no problems with it; I just think the overwhelming majority here (win users) should LEARN not to "take as granted" that we all use windows (I dunno how to explain myself better: it's also for their own benefit…)
Anyway, to be sincere, I feel very uncomfortable when using win (even more when using a cell phone…)
Seems I used Mac too much ;)
Post Reply