views:

802

answers:

3

In a Silverlight application's log in screen, i need to determine if Caps Lock is toggled. This is easy enough by handling the KeyUp or KeyDown event, however how does one determine if it is toggled on or off even if a key hasn't been pressed?

The reason I want todo this is what if the user doesn't press Caps Lock while the Silverlight application is running, but rather they toggled it before they even got to the login screen? I need to still warn them that caps Lock is on.

Apparently handling unmanaged code is not possible in Silverlight so the following doesn't work.

[DllImport("user32.dll")] internal static extern short GetKeyState(int keyCode);

A: 

As far as I know you can't. You might be able to pickup on the key going up and down from your app, but you won't know what state it was in if the users leave your app.

I've not noticed any additional supprt for it in SL3, but I can't say I've been particularly looking for it.

Steven Robbins
A: 

Silverlight does not support invoking Win32 DLLs on the client side. So you can't use the above technique to test whether the Capslock key is pressed. Have you tried using the KeyDown event and testing for System keys? I haven't done that, but it's a hunch.

Cyril Gupta
Yes, KeyDown and KeyUp events will work fine, however I'm trying to determine if Caps Lock is toggled on, not if it is pressed. If someone toggles Caps Lock before they go to my Silverlight application i need to warn them that it is toggled on.
Rogan
+3  A: 

The way to detect CAPS LOCKS is to check the KeyValue and the Shift of KeyEventArgs.
if the KeyValue is (for example) 'A' but the Shift is false then the CAPS LOCKS is on.
Not the best way but its how ajax applications has been doing it.

Shay Erlichmen
Thank-you for this. Is there a way to determine it on the KeyUp/KeyDown event in the code behind of Silverlight XAML?
Rogan
yes, the JavaScript event and the SilverLight event are quite slimier. Just take the code and translate it to C#.
Shay Erlichmen
Good solution Shay +1
Cyril Gupta