MBZ
MBZ

Reputation: 27592

Send a key to WebView

Short Version: Is it possible to send a key press event to an embedded WebView in Windows Phone?

Long Version: I have a simple HTML5 game hosted on a website and now I want to load it inside a WebSite in a Windows Phone app and then add control keys (e.g. left, right, shoot) to the button of the screen. And then, if the user pressed the left button, I would send a left key to the WebView. Is it possible to achieve this?

Upvotes: 0

Views: 1210

Answers (1)

peterdn
peterdn

Reputation: 2466

JavaScript can be executed in the WebView using WebView.InvokeScriptAsync. You should be able to use it to call the same JavaScript event handler methods you use for key events.

Also note that InvokeScriptAsync only invokes a single JavaScript function, however if you need to execute an arbitrary number of statements, a neat trick is to use eval. For example:

var script = "...";
await webView.InvokeScriptAsync("eval", new string[] { script });

Upvotes: 1

Related Questions