Override (IE7) browser keyboard handling from Silverlight

家住魔仙堡 提交于 2019-12-25 00:46:49

问题


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.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/769668/override-ie7-browser-keyboard-handling-from-silverlight

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!