Finding Key State using PInvoke in SL5 RC

If we have to find the Key State of  CAPS lock,  NUM lock etc in silverlight 5 RC, we could do like this using PInvoke

Add the Declaration

[DllImport(“user32.dll”, CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Winapi)]
public static extern short GetKeyState(int keyCode);

And to get the different Key states

bool CapsLock = (((ushort)GetKeyState(0x14)) & 0xffff) != 0;

bool NumLock = (((ushort)GetKeyState(0x90)) & 0xffff) != 0;

bool ScrollLock = (((ushort)GetKeyState(0x91)) & 0xffff) != 0;




Using PInvoke in SL5 to show MessageBox

One of the new features in SL5 is the ability to use PInvoke. This post shows how to use PInvoke to show MesageBox with lot more options.

Start by creating a SL5 OOB application with elevated trust and add the following code

[DllImport(“user32.dll”, CharSet = CharSet.Auto)]
public static extern MessageBoxResult MessageBox(IntPtr hWnd, String text, String caption, int options);
public enum MessageBoxOptions
Ok = 0x000000,
OkCancel = 0x000001,
AbortRetryIgnore = 0x000002,
YesNoCancel = 0x000003,
YesNo = 0x000004,
RetryCancel = 0x000005,
CancelTryContinue = 0x000006,

IconHand = 0x000010,
IconQuestion = 0x000020,
IconExclamation = 0x000030,
IconAsterisk = 0x000040,
UserIcon = 0x000080,

IconWarning = IconExclamation,
IconError = IconHand,
IconInformation = IconAsterisk,
IconStop = IconHand,

DefButton1 = 0x000000,
DefButton2 = 0x000100,
DefButton3 = 0x000200,
DefButton4 = 0x000300,

ApplicationModal = 0x000000,
SystemModal = 0x001000,
TaskModal = 0x002000,

Help = 0x004000, //Help Button
NoFocus = 0x008000,

SetForeground = 0x010000,
DefaultDesktopOnly = 0x020000,
Topmost = 0x040000,
Right = 0x080000,
RTLReading = 0x100000,

public enum MessageBoxResult : uint
Ok = 1,
Timeout = 32000

To actually see the messagebox, we could do something like

MessageBoxOptions options = MessageBoxOptions.CancelTryContinue | MessageBoxOptions.DefButton1 | MessageBoxOptions.IconQuestion;
MessageBox(IntPtr.Zero, “This is a messagebox with 3 buttons, one of them being default and a question icon”, “Caption”, (int)options);

Here is the result