3

You may find if the CapsLock key has been pressed subscribing to the KeyDown/KeyUp event. And then toggle the state of the CapsLock based on that input. The problem with this approach is that you need the initial state of the CapsLock key to start toggling that.

One application of this could be giving the user a notification on a Login Page (this is what i need).

By the way i'm using Silverlight 5.

EDIT

The solution posted here says:

You can however find out if Capslock is on by making use of KeyEventArgs.PlatformKeyCode that's actually send at onKeyDown.You can look up the Virtual Key-code for capslock in here: http://msdn.microsoft.com/en-us/library/ms927178.aspx

With this solution you can't determine the CapsLock state, because KeyEventArgs.PlatformKeyCode returns "an integer value that represents the key that is pressed or released (depending on which event is raised)". So if CapsLock is On and Key A is pressed then KeyEventArgs.PlatformKeyCode = 65, and on the other hand if CapsLock is off and Key A is pressed then KeyEventArgs.PlatformKeyCode = 65.

In other words you can't determine if the CapsLock is enabled or not based on the KeyEventArgs.PlatformKeyCode property.


The answer to this question also seems to have a solution, it checks two things:

  1. the letter typed is Upper Case and Shift isn't pressed
  2. the letter typed is Lower Case and Sift is pressed

Both of this cases implies that the CapsLock is On, but there is also a problem with this solution, given a KeyEventArgs you can know the pressed key in the keyboard but can't know the Char outputted by that key.

Community
  • 1
  • 1
Ariel
  • 1,641
  • 1
  • 18
  • 27
  • Perhaps see this post: http://forums.silverlight.net/post/185708.aspx – Brad Christie Sep 19 '11 at 17:24
  • Related post: [How to determine if the Caps Lock is toggled in a Silverlight Application?](http://stackoverflow.com/q/733102/590956). Doesn't look like it's possible to detect caps lock without using a key event in silverlight from what I've read so far. :( – Sam Sep 19 '11 at 17:31
  • But you really don't want to alert the user that the caps lock is on until they start typing in a password box anyways. Maybe my username is in all caps so I want the caps lock on and don't bother me about it. However, I can't see it is on when I type my password so a nice alert (like in Windows) would be helpful. – Bryant Sep 19 '11 at 17:48
  • @Bryant you're rigth, there is no need to show this before pressing any key, so i changed the title of the question. – Ariel Sep 19 '11 at 19:11

2 Answers2

7

I'd suggest using a Behavior for this detection since you can hook into the PasswordChanged and KeyDown events to determine if the Caps Lock is on. Here is a quick behavior I wrote to detect if the Caps Lock is on. You can bind to the CapsLockOn behavior and use something like a data state behavior to hide/show your warning message.

public class DetectCapsLockBehavior : Behavior<PasswordBox>
{
    private int _lastKey;
    private ModifierKeys _modifiers;

    [Category("Settings")]
    public bool CapsLockOn
    {
        get { return (bool)GetValue(CapsLockOnProperty); }
        set { SetValue(CapsLockOnProperty, value); }
    }

    public static readonly DependencyProperty CapsLockOnProperty = DependencyProperty.Register("CapsLockOn", typeof(bool), typeof(DetectCapsLockBehavior), new PropertyMetadata(null));

    protected override void OnAttached()
    {
        AssociatedObject.PasswordChanged += new RoutedEventHandler(AssociatedObject_PasswordChanged);
        AssociatedObject.KeyDown += new KeyEventHandler(AssociatedObject_KeyDown);
    }

    void AssociatedObject_KeyDown(object sender, KeyEventArgs e)
    {
        _lastKey = e.PlatformKeyCode;
        _modifiers = Keyboard.Modifiers;
    }

    void AssociatedObject_PasswordChanged(object sender, RoutedEventArgs e)
    {
        if (_lastKey >= 0x41 && _lastKey <= 0x5a)
        {
            var lastChar = AssociatedObject.Password.Last();
            if (_modifiers != ModifierKeys.Shift)
            {
                CapsLockOn = char.ToLower(lastChar) != lastChar;
            }
            else
            {
                CapsLockOn = char.ToUpper(lastChar) != lastChar;
            }
        }
    }
}

NOTE: This is sample code, so there could be bugs. Just trying to demonstrate how it could be done.

Bryant
  • 8,660
  • 1
  • 33
  • 53
1

region KeysDetection

    bool bCaps = false; 
    bool bIns = false; 
    bool bNum = false; 

    public void FloatableWindow_KeyDown(object sender, KeyEventArgs e) 
    { 



        switch (e.Key) 
        { 
            case Key.CapsLock: 
                bCaps = !bCaps; 
                lbl_caps.Opacity = (bCaps) ? 1 : 0.5; 
                break; 

            case Key.Insert: 
                bIns = !bIns; 
                lbl_ins.Opacity = (bIns) ? 1 : 0.5; 
                break; 

            case Key.Unknown: 
                { 
                    if (e.PlatformKeyCode == 144) 
                    { 
                        { 
                            bNum = !bNum; 
                            lbl_num.Opacity = (bNum) ? 1 : 0.5; 
                        } 
                    } 
                    break; 
                } 
        } 


    } 

    #endregion 
Community
  • 1
  • 1
shirivo
  • 19
  • 2