Secure textbox

Is there any secure textbox out there? Or should I try(again) and edit the DTextEntry?

I’ve tried it multiple times but I keep failing, does anyone know a good way to do this>

“Secure” textbox? Like, showing characters as * instead - password-esque?

Yes, sorry for the vague description.

Hook into the text box’s OnTextChanged function. When it changes, compare string length. If it’s longer, then store the latest character, then set the text to *** (The number of characters present). If it’s shorter, they’ve backspaced. Pop the last value off the password stack, and then you’re good to go.

Thats one way I tried it, though for example:
Someone has a upper case letter at the start of his password, when done typing he realises he might have used a lower case letter, he clicks at the front and deletes the letter, now replacing it. This letter will join at the end of the hidden variable.

But I guess I’ll have to live with that then.

I’ll do some snooping of the DTextEntry code, see if I can come up with a solution for you.

http://www.facepunch.com/showpost.php?p=20452025&postcount=2

  • snip -

Wow, thanks Kogitsune, exactly what I needed!

self:GetCaretPos()

Use that to get the position of the cursor in the text. Enjoy.

It’s isn’t working correctly. It keep adding to the real value even if I use backspace

I’m trying to use this, but it’s kind of late and I can’t really see how I should do it. This is what I have:
[lua]
local PANEL = {};

PANEL.m_sPasswordChar = “*”
PANEL.m_sRealText = “”

AccessorFunc( PANEL, “m_sPasswordChar”, “PasswordCharacter” )

function PANEL:GetRealText( )
return self.m_sRealText
end

function PANEL:SetRealText( str )
self.m_sRealText = str

self:SetValue( string.rep( self:GetPasswordCharacter( ), #str ) )

end

function PANEL:OnTextChanged()
if( #self:GetRealText() < #self:GetValue() )then
self:SetRealText(string.sub(self:GetRealText( ), 0, self:GetCaretPos() - 1) … string.sub(self:GetValue(), self:GetCaretPos(), self:GetCaretPos()) … string.sub(self:GetRealText( ), self:GetCaretPos() + 2));
else
self:SetRealText(string.sub(self:GetRealText( ), 0, self:GetCaretPos() - 2) … string.sub(self:GetRealText( ), self:GetCaretPos()));
end;
end

function PANEL:UpdateConvarValue( )
self:ConVarChanged( self:GetRealText( ) )
end

derma.DefineControl( “DTextEntrySec”, “A textbox with password characters”, PANEL, “DTextEntry” )
[/lua]

I know it doesn’t work at all:

  • The wrong characters are getting removed
  • It sometimes freaks out when using delete and backspace and it adds a bunch of *'s instead or removing something…

I had a quick mess around with it, gave up after around 5 minutes.

[lua]local panel = {};

panel.RealText = “”;

function panel:OnTextChanged()
self.RealText = string.sub(self.RealText , 0 , self:GetCaretPos() -1)… string.sub(self:GetValue() , self:GetCaretPos() , self:GetCaretPos() + 1)… string.sub(self.RealText , self:GetCaretPos() + 1 , string.len(self.RealText))
self:SetValue(string.rep("*" , string.len(self.RealText)))
end

derma.DefineControl( “DPasswordEntry”, “A textbox with password characters”, panel, “DTextEntry” )[/lua]

It’s nowhere near perfect (As you’ll see if you try it out)

EDIT: It works now:
[lua]function PANEL:OnTextChanged()
if( #self:GetRealText() < #self:GetValue() )then
self:SetRealText(string.sub(self:GetRealText( ), 0, self:GetCaretPos() - 1) … string.sub(self:GetValue(), self:GetCaretPos(), self:GetCaretPos()) … string.sub(self:GetRealText( ), self:GetCaretPos()));
else
self:SetRealText(string.sub(self:GetRealText( ), 0, self:GetCaretPos()) … string.sub(self:GetRealText( ), self:GetCaretPos() + 2));
end;
end[/lua]

1 problem: selecting the text and trying to “overwrite” it wont work.

Thanks all.