views:

860

answers:

2

When I test my Silverlight 2 app in Firefox, when Silverlight has focus, I have no problem receiving every key press via the Page.KeyDown event.

When I test it in Internet Explorer 7, I can only get keyboard events that the browser doesn't already handle. HOWEVER, I can override those same keyboard events in javascript.

For instance, Ctrl + D. I can override this in javascript by doing the following, but I can't do the same thing in Silverlight!!

function initKeyHandling(){ document.attachEvent("onkeydown", keydown); }

function keydown(e) { printToTextbox('Keydown ', e); return false; }

...

initKeyHandling();

Furthermore, my attempts to forward the events from Javascript to Silverlight have failed. My javascript keyboard event handlers never even get fired when Silverlight has focus.

A: 

We've run into similar issues using Flash, and we had to resolve them by having a separate DIV tag on the same page which we'd have to programmatically set the focus to whenever we needed to invoke our keyboard shortcuts.

Raul Agrait
If my user can press a keyboard shortcut at anytime, I'd always have to have focus on the DIV, right? That sounds crazy!
wizlb
+1  A: 

From the docs it seems that it is not possible (at least without a javascript work around like you are mentioning):

Keyboard Events and the Browser Different browsers might handle keyboard events differently. When you create an application that uses keyboard input, make sure to test the application in your target browsers.

The browser determines which keystrokes it interprets as commands and which keystrokes it passes on to hosted content. This means that certain keystrokes cannot be retrieved from KeyDown and KeyUp event-handler functions. Most keystrokes that a browser interprets as commands are shortcut or accelerator keystrokes, and are not passed as key events to Silverlight. For example, CTRL+D is a shortcut keystroke combination for adding a favorite URL to the Firefox and Internet Explorer browsers, and neither CTRL nor D is reported as a key event.

Have a look at: http://msdn.microsoft.com/en-us/library/cc189015.aspx

I think I read about a work around with an underlying HTML textbox the other day. But I'm not sure if it still supported, or what the details were. I will return if I find it again.

/Asger

asgerhallas