23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Edge Gestures<br />

As we saw in Chapter 7, “Commanding UI,” you don’t need to do anything special for commands on the<br />

app bar or navigation bar to appear: Windows automatically handles the edge swipe from the top and<br />

bottom of your app, along with right-click, Win+Z, and the context menu key on the keyboard. That<br />

said, you can detect when these events happen directly by listening for the starting, completed, and<br />

canceled events on the Windows.UI.Input.EdgeGesture object: 48<br />

var edgeGesture = Windows.UI.Input.EdgeGesture.getForCurrentView();<br />

edgeGesture.addEventListener("starting", onStarting);<br />

edgeGesture.addEventListener("completed", onCompleted);<br />

edgeGesture.addEventListener("canceled", onCanceled);<br />

With these, completed fires for all input types; the starting and canceled events occur only for<br />

touch. Within these events, the eventArgs.kind property contains a value from the EdgeGesture-Kind<br />

enumeration that indicates the kind of input that invoked the event. The starting and canceled events<br />

will always have the kind of touch, obviously, whereas completed can be any touch, keyboard, or mouse:<br />

function onCompleted(e) {<br />

// Determine whether it was touch or keyboard invocation<br />

if (e.kind === Windows.UI.Input.EdgeGestureKind.touch) {<br />

id("ScenarioOutput").innerText = "Invoked with touch.";<br />

}<br />

else if (e.kind === Windows.UI.Input.EdgeGestureKind.mouse) {<br />

id("ScenarioOutput").innerText = "Invoked with right-click.";<br />

}<br />

else if (e.kind === Windows.UI.Input.EdgeGestureKind.keyboard) {<br />

id("ScenarioOutput").innerText = "Invoked with keyboard.";<br />

}<br />

}<br />

The code above is taken from Scenario 1 of the Edge gesture invocation sample. In Scenario 2, the<br />

sample also shows that you can prevent the edge gesture event from occurring for a particular element<br />

if you handle the contextmenu event for that element and call eventArgs.preventDefault in your<br />

handler. It does this for one element on the screen, such that right-clicking that element with the mouse<br />

or pressing the context menu key when that element has the focus will prevent the edge gesture events:<br />

document.getElementById("handleContextMenuDiv").addEventListener("contextmenu", onContextMenu);<br />

function onContextMenu(e) {<br />

e.preventDefault();<br />

id("ScenarioOutput").innerText =<br />

"The ContextMenu event was handled. The EdgeGesture event will not fire.";<br />

}<br />

48 As WinRT object events, these are subject to the considerations in “WinRT Events and removeEventListener” in Chapter 3.<br />

370

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!