WPF PasswordBox: How do I warn a user that Caps Lock is enabled?
Asked Answered
S

3

8

Does anyone know how to implement the standard bubble message that warns users whenever Caps Lock is enabled and a password control has focus? Is this built into the .NET framework, or do I need to write my own class to do this?

Schatz answered 26/11, 2008 at 15:59 Comment(3)
Did you solve this? I was looking for the same?Undesirable
Follow: [How to give warning to user with balloon in wpf][1] [1]: https://mcmap.net/q/585826/-warn-about-capslock/…Arbour
Use Keyboard.IsKeyToggled for CapsLock key and manually show tooltip or hint.Resolution
K
3

You could add a handler function to the PasswordChanged event handler and test for the value of the CapsLock key in that function. If found to be on, you could pop-up a message from there.

Keelykeen answered 26/11, 2008 at 16:11 Comment(2)
This will do the trick! To detect CapsLock in the event handler, just check the boolean value of: Keyboard.IsKeyToggled(Key.CapsLock)Schatz
This answer is useless: 1. PasswordChanged happen TOO LATE. Much better to react on 'GotFocus'. 2. HOW EXACTLY we can check status of CapsLock key? That was the question.Resolution
M
12

This is an old question, and already answered, but I came across this same problem and I first started with Keyboard.IsKeyToggled(Key.CapsLock) but that returned false if Caps Lock was set prior to the application running. So I found another solution that works perfectly.

Console.CapsLock //is boolean and returns true if CapsLock is on

Absolutely brilliant and simple (it's in the mscorlib dll so you don't have to worry about unneeded dependencies either)

Marnamarne answered 18/5, 2011 at 19:8 Comment(1)
It's wrong solution. You had to use Keyboard.IsKeyToggledResolution
K
3

You could add a handler function to the PasswordChanged event handler and test for the value of the CapsLock key in that function. If found to be on, you could pop-up a message from there.

Keelykeen answered 26/11, 2008 at 16:11 Comment(2)
This will do the trick! To detect CapsLock in the event handler, just check the boolean value of: Keyboard.IsKeyToggled(Key.CapsLock)Schatz
This answer is useless: 1. PasswordChanged happen TOO LATE. Much better to react on 'GotFocus'. 2. HOW EXACTLY we can check status of CapsLock key? That was the question.Resolution
B
2

If you use a MaskedTextBox and specify a passwordChar the .NET framework will automatically do this for you

Bracing answered 26/11, 2008 at 16:9 Comment(1)
I believe MaskedTextBox is a WinForms control - I'm using WPF. I'd like to be able to continue using the PasswordBox because of its built-in security.Schatz

© 2022 - 2024 — McMap. All rights reserved.