Detect whether Alt, Ctrl, Shift, Tab and CapsLock is key pressed in Silverlight

Capturing keyboard shortcuts in Silverlight on Mac OSX

Some Mac keys doesn't work


The standard modifier key with Silverlight on Mac OS X

With Silverlight on Mac OS X the standard modifier keys wasn't captured. That is a big problem. The standard modifier keys are Tab, Shift, Alt, CapsLock and Ctrl.

A lot of keyboard inputs use this keys like the normal upper cases with Shift or CapsLock, copy&past with Ctrl+C,Ctrl+V or some special caracters with Alt+€, Alt+@. If this called modifier keys don't work you can't write a longer texts.

Keyboard shortcuts need additional standard modifier keys like Alt, Shift, Ctrl, Tab or CapsLock.

How to capture modifier keys with Silverlight on Mac OS X anyway

The standard way to detect if this keys are pressed like KeyUp or KeyDown events doesn't work. But you can use the TextInput event.

But attention! Both events don't work with the same parameters. Fhe first work with KeyEventArgs and the last with TextCompositionEventArgs. That's why a additional browser detection is needed, if you use both events and you programmed call back functions for KeyDown and TextInput.

Only for the case you use the Safari browser for Silverlight an on Mac OS X you could use the TextInput with e.Text witch returned a string otherwise use KeyDown e.Key wich returned only one a Char.

Capture both events, but use only on. TextInput on Mac OS X and KeyDown on Windows.


<Canvas x:Name="m_oLayoutRoot" Background="White" TextInput="LayoutRoot_TextInput" KeyDown="LayoutRoot_KeyDown" KeyUp="LayoutRoot_KeyUp" >
 
private void LayoutRoot_TextInput(object sender, TextCompositionEventArgs e)
{
   	// if Mac OS X
   	if (HtmlPage.BrowserInformation.UserAgent.ToLower().Contains("safari"))
   	{
         	Char[] keys = Convert.ToChar(e.Text);
   	}
}

private void LayoutRoot_KeyDown(object sender, KeyEventArgs e)
{
   	// if Windows
   	if (HtmlPage.BrowserInformation.UserAgent.ToLower().Contains("safari"))
   	{
   	       return;
   	}
   	if(e.Key == 'e')
   	{
   	...





© Kometschuh.de | Impressum | GitHub | Facebook |