How to Disable Multitouch in windows phone? - c#

I had used mouse events, using TouchFrameReported, I wanted it to be single touch, but it is supporting multitouch, how to disable multitouch, in touch frame reported, or is there any idea to implement so that multitouch is not supported..
void Touch_FrameReported(object sender, TouchFrameEventArgs e)
{
foreach (TouchPoint touchPoint in e.GetTouchPoints(this.mainGrid))
{
if (touchPoint.Action == TouchAction.Down)
{
currentPoint.X = touchPoint.Position.X;
currentPoint.Y = touchPoint.Position.Y;
glowDot();
}
else if (touchPoint.Action == TouchAction.Up)
{
circPathGlow.Visibility = Visibility.Collapsed;
}
else if (touchPoint.Action == TouchAction.Move)
{
}
}
}

You can find more information on:
http://social.msdn.microsoft.com/Forums/windowsapps/en-US/123e9381-fc0b-441e-a738-dcd35f526a6e/how-to-disable-multitouch
I wouldn't try to fiddle with the touch messages here. If the goal is
to limit the dragging to one control at a time then limit it to the
controls. Once one is moving, don't move the others.
At the pointer message level you can track the PointerId in
PointerPressed and ignore other PointerIds until you get a
PointerReleased or PointerCaptureLost:
Question: Do you want to disable certain multi-gestures or all?

After reading http://www.wintellect.com/blogs/jprosise/building-touch-interfaces-for-windows-phones-part-2
I came to know that I was using e.GetTouchPoints
Instead of e.GetPrimaryTouchPoint,,
Now, I use e.GetPrimaryTouchPoint, which capture only the first touch points that is being touched,,
TouchPoint touchPoint = e.GetPrimaryTouchPoint(this.mainGrid);
and rest of the code,, this solve my problem.

Related

How to route touch events to separate controls UWP C#

I am facing a major problem for custom touch event handling.
The goal is to route the touch event to different controls, depending on how many fingers are used.
For example:
We have a scrollview with many webviews arranged in it.
The zoomfactor is set to present one webview to fullscreen.
Currently I am using the pointer pressed / released functions like this, to configure the controls, so the event will be catched by the right one:
int pointerCount = 0;
private void ScrollView_PointerPressed(object sender, PointerRoutedEventArgs e)
{
try
{
pointerCount++;
if (pointerCount > 3)
pointerCount = 0;
switch (pointerCount)
{
case 1:
// I don't do anything, so it goes to the webview anyway
break;
case 2:
EnableScrolling();
break;
case 3:
ZoomInOut();
break;
default:
return;
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}
private void EnableScrolling()
{
ScrollViewer.ZoomMode = ZoomMode.Enabled;
ScrollViewer.VerticalScrollMode = ScrollMode.Enabled;
ScrollViewer.HorizontalScrollMode = ScrollMode.Enabled;
}
1-finger events should go to the webview // this works
2-finger events should go to the ScrollView // this is not working, because the webview grabs the touch event and will not release it again
3-finger events should zoom out // this works
The PointerPressed is always called, but the PointerReleased is not called when the PointerPressed was on the webview.
This also results in the effect of not decreasing the pointerCount, so it is possible to do a 1-finger tap 3 times and it results in the 3-finger event and zooms out, that should not happen.
Hopefully you can see the problem and help to resolve it.
If you think this is a way too wrong approach, feel free to show an alternative that works out better.
Well, I couldn't find a proper solution, but I was able to remove the unwanted side effect with upcounting the pointers event if they were released.
private void ScrollViewer_PointerReleased(object sender, PointerRoutedEventArgs e)
{
pointerCount = 0;
}
So the right direction handling is working fine now, but I still can't do something like:
currentWebView.CapturePointer(e.Pointer);
Because it won't root the pointerEvent into it's content, it will call the pointer events of the WebView, but it won't root it into it's html & js content.
I also tried to use
.... wb.ManipulationStarted += Wb_ManipulationStarted; ....
private void Wb_ManipulationStarted(object sender, ManipulationStartedRoutedEventArgs e)
{
// something here
}
but this also will not arrive in the content.
Any ideas for another approach?
I figured out how to handle that, and it is so simple that I could cry...
I just added this line of code to tell the webviews js that there was a event...
await webView.InvokeScriptAsync("eval", new string[] { "document.elementFromPoint(" + pointX + ", " + pointY + ").click();" });
And the webview handles the rest of it. So there is no custom invoke as I was worried about, it's just the standard function invoked, that is always invoked when a touch/click is performed.

How can I detect when scrolling has started in a ListView on Windows 10 UWP?

I'd like to subscribe to an event which tells me that scrolling has started in a ListView and get the direction of scrolling.
Is there any way to do this in Windows 10 UWP API?
Thanks
You should first obtain the ScrollViewer inside the ListView and then subscribe to its DirectManipulationStarted event.
However, to get the direction of the scrolling can be tricky. I'd suggest you to have a look at the new Windows Composition API where there's a way to use ExpressionAnimation to link the ScrollViewer's translation to a value of your choice.
A good start is to have a look at this demo from James Clarke.
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
CompositionPropertySet scrollerViewerManipulation = ElementCompositionPreview.GetScrollViewerManipulationPropertySet(myScroller);
Compositor compositor = scrollerViewerManipulation.Compositor;
ExpressionAnimation expression = compositor.CreateExpressionAnimation("ScrollManipululation.Translation.Y * ParallaxMultiplier");
expression.SetScalarParameter("ParallaxMultiplier", 0.3f);
expression.SetReferenceParameter("ScrollManipululation", scrollerViewerManipulation);
Visual textVisual = ElementCompositionPreview.GetElementVisual(background);
textVisual.StartAnimation("Offset.Y", expression);
}
Update
Actually just thought of an easier way to detect the scrolling direction. Just subscribe whenever the VerticalOffset is changed and compare it to its previous values.
private double _previousOffset;
sv.RegisterPropertyChangedCallback(ScrollViewer.VerticalOffsetProperty, (s, dp) =>
{
if (Math.Abs(sv.VerticalOffset - _previousOffset ) < 3)
{
// ignore when offset difference is too small
}
else if (sv.VerticalOffset > _previousOffset)
{
Debug.WriteLine($"up {sv.VerticalOffset - _previousOffset}");
}
else {
Debug.WriteLine($"down {sv.VerticalOffset - _previousOffset}");
}
_previousOffset = sv.VerticalOffset;
});

Making swipe work for a pivot control with embedded WebBrowser on Windows Phone 8.0

I'd like my application to have a Pivot control with a WebBrowser control in each PivotItem. The problem is that swiping to next PivotItem doesn't work since it instead results in scrolling the web page horizontally. However, the web pages are responsive and doesn't need to be scrolled horizontally so instead I want horizontal swiping to result in a pivot switch. Can this be accomplished? If I can't use a Pivot control what is the recommended way to implement page switching with WebControls on the pages? A top menu a la iOS/Android?
Yes it is possible, however needs little workaround since Webbrowser handles the event first and your Pivot probably doesn't know about it. You can use for this purpose Touch and its event FrameReported. Simple code can look like this:
public MainPage()
{
InitializeComponent();
Touch.FrameReported += Touch_FrameReported;
}
TouchPoint first;
private void Touch_FrameReported(object sender, TouchFrameEventArgs e)
{
TouchPoint mainTouch = e.GetPrimaryTouchPoint(myWebBrowser);
if (mainTouch.Action == TouchAction.Down)
first = mainTouch;
else if (mainTouch.Action == TouchAction.Up)
{
if (mainTouch.Position.X - first.Position.X < 0)
MessageBox.Show("Next item.");
//myPivot.SelectedIndex++;
if (mainTouch.Position.X - first.Position.X > 0)
MessageBox.Show("Previous item.");
//myPivot.SelectedIndex--;
}
}
As you can see - I'm remembering the where the user start his movements, then when he releases his touch - I change myPivot.SelectetIndex. Hope this helps a little. You can also try with TouchPanel, but I think that will be simpler (if it's sufficient).
If you want to disable some gestures it can look for example like in the code below. I enabled only HorizontalDrag - so it won't fire for zoom. The tolerance of course shouldn't be 0, you will have to define the most suitable for your App.
// in MainPage() constructor
TouchPanel.EnabledGestures = GestureType.HorizontalDrag;
private void Touch_FrameReported(object sender, TouchFrameEventArgs e)
{
TouchPoint mainTouch = e.GetPrimaryTouchPoint(myMap);
if (mainTouch.Action == TouchAction.Down)
first = mainTouch;
else if (mainTouch.Action == TouchAction.Up && TouchPanel.IsGestureAvailable)
{
if (mainTouch.Position.X - first.Position.X < 0)
MessageBox.Show("Next item.");
//myPivot.SelectedIndex++;
if (mainTouch.Position.X - first.Position.X > 0)
MessageBox.Show("Previous item.");
//myPivot.SelectedIndex--;
}
}
Here is the article that solves this particular issue http://blogs.msdn.com/b/madenwal/archive/2015/02/11/how-to-embed-a-webbrowser-control-inside-a-pivot-item-in-windows-phone.aspx

WPF Multitouch - How to get all touchpoints?

We are evaluating touchscreen keyboards and for one we need to track 10 fingers at the same time. The problem is that the touchscreen driver is very wonky (and there is no fixed version). It sends out 2500+ events for the FrameReported event each second for so many fingers. There is just no way to handle all those, even if we discard 90% at the beginning. It's simply impossible to keep up and do anything meaningful with the data.
Instead of System.Windows.Input.Touch.FrameReported, I also tried to use the (Preview) TouchMove events of the window; Same problem here.
So now I wanted, instead of using events, to poll in a separate Thread, but I cannot find information on how to get all the current touchpoints.
The only thing I found is a WinForms hack, but that isn't an option, since then I will be unable to render any WPF controls in my window.
Any solutions?
Edit 1:
This is the code, that handles all the move events:
private void UserControlTouchMove(object sender, TouchEventArgs e)
{
//Update Position of the corresponding point
var touch = e.GetTouchPoint(this);
var id = touch.TouchDevice.Id;
e.Handled = true;
var position = touch.Position;
//update finger on display, quick and dirty
if (m_ShowFingers)
{
foreach (var finger in m_Fingers)
{
if (id == (int)finger.DataContext)
{
finger.RenderTransform = new TranslateTransform(position.X - HalfFingerSize, position.Y - HalfFingerSize);
break;
}
}
}
}

C# - Capturing CTRL-Mouse wheel in WebBrowser control

I'm developing a Windows Forms application in C# with an embedded WebBrowser control to "dummy-proof" (i.e. disable context menus, back button, free navigation, etc.) access to a third-party web application.
Right now I'm trying to add the Zoom feature to my custom browser. I have the keyboard combos working for it (CTRL + and CTRL - make the correct OLE calls to the underlying ActiveX WebBrowser object), but among the other frustrating things about WebBrowser I've had to deal with, I can't seem to figure out how to capture CTRL-Mouse wheel to simulate the Zoom function like IE does. I've looked everywhere to find a solution to this but to no avail.
To try to figure it out, I created an empty form with just the WebBrowser control in it, and found the following:
CTRL-MouseWheel always fires the MouseWheel event when the parent form has focus and the mouse cursor is hovering over the top of the window (e.g. over the title of the application), or when the mouse cursor is hovering over the WebBrowser control when it does not appear to have focus even though the parent form has focus.
CTRL-MouseWheel never fires the MouseWheel event when the mouse cursor is hovering over the WebBrowser control and WebBrowser has focus, and there seems to be no effect.
Using the mouse wheel without CTRL scrolls the window contents of WebBrowser but does not fire the MouseWheel event until the vertical scroll bar has fully reached either the top or the bottom.
Intercepting the Message for WM_MOUSEWHEEL by overriding WndProc and DefWndProc both for a sample class inherited from WebBrowser and for the parent form applies only for the above conditions (with wParam properly denoting MK_CONTROL).
The PreviewKeyDown event fires when CTRL is pressed, as expected, but still does nothing in unison with the mouse wheel.
So I guess the Message is being processed below the parent form and the managed control level and does not bubble up to where I can intercept or even handle it. Is there a way to do this, or some other way to simulate zooming in and out using CTRL-MouseWheel?
Thanks for reading!
First cast the WebBrowser.Document.DomDocument to the right interface in the mshtml namespace, like mshtml.HTMLDocumentEvents2_Event, then you can handle (and cancel) mousewheel events. I'm not sure, but I think you need to wire up the event handler anytime the document is changed, so I do it on the WebBrowser.DocumentCompleted event. I'm also not sure if you need to release any COM objects.
This was frustrating enough that I got it to work and stopped caring...
Here is at least one document explaining the basics: How to handle document events in a Visual C# .NET application
For your specific case, just conditionally squash the onmousewheel event, based on whether or not the CTRL key is pressed.
private void webBrowser_DocumentCompleted(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
if (webBrowser.Url.ToString() == "about:blank")
return;
var docEvents = (mshtml.HTMLDocumentEvents2_Event)webBrowser.Document.DomDocument;
docEvents.onmousewheel -= docEvents_onmousewheel; //may not be necessary?
docEvents.onmousewheel += docEvents_onmousewheel;
}
bool docEvents_onmousewheel(mshtml.IHTMLEventObj pEvtObj)
{
if (pEvtObj.ctrlKey)
{
pEvtObj.cancelBubble = true; //not sure what this does really
pEvtObj.returnValue = false; //this cancels the event
return false; //not sure what this does really
}
else
return true; //again not sure what this does
}
Now if you need to know the Wheel Delta (scroll amount), you'll want to cast the events object to yet another interface.
bool docEvents_onmousewheel(mshtml.IHTMLEventObj pEvtObj)
{
var wheelEventObj = (mshtml.IHTMLEventObj4)pEvtObj;
var delta = wheelEventObj.wheelDelta;
[...]
}
Perhaps using SetWindowsHookEx to look for these events may work for you. This is what I've used to get scroll wheel events on top of the ActiveX MapPoint control.
Be aware there are some quirks with this on Windows 7 that may require some tinkering. For more details do a search for SetWindowsHookEx on Windows 7 on the MSDN forums.
To solve this problem you have to listen for and handle these messages:
OLECMDID_GETZOOMRANGE
OLECMDID_OPTICAL_GETZOOMRANGE
OLECMDID_OPTICAL_ZOOM
OLECMDID_ZOOM
They're dispatched by Internet Explorer. See the remarks on MSDN.
This is the code I used to disable ctrl+shift: You need to change the behavior of WndProc in the deepest control "Internet Explorer_Server",
Do this after your web browser is ready:
IntPtr wbHandle = Win32.FindWindowEx(this.wbMain.Handle, IntPtr.Zero, "Shell Embedding", String.Empty);
wbHandle = Win32.FindWindowEx(wbHandle, IntPtr.Zero, "Shell DocObject View", String.Empty);
wbHandle = Win32.FindWindowEx(wbHandle, IntPtr.Zero, "Internet Explorer_Server", String.Empty);
WbInternal wb = new WbInternal(wbHandle);
class WbInternal : NativeWindow
{
public WbInternal(IntPtr handle)
{
this.AssignHandle(handle);
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_MOUSEWHEEL)
{
if (((int)m.WParam & 0x00FF) == MK_SHIFT)
{
return;
}
}
base.WndProc(ref m);
}
}
You can find more message about WM_MOUSEWHEEL from MSDN.
I find this from MSDN. But I forgot the link, Once find, will append it here.
I couldn't get any of these to work reliably so after some (frustrating) experimentation, I came up with a derivative of the answer posted by TCC. My webbrowser control is hosted in a usercontrol. The main differences are I use a class-level variable for the HTMLDocumentEvents2_Event so I can unsubscribe successfully, and I set the mshtml.IHTMLEventObj pEvtObj.Returnvalue to true.. seems to work well now..
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
_wbData = (WebBrowser)FindElement("DataBrowser");
_horzScroll = (ScrollBar)FindElement("HorizontalScroll");
_vertScroll = (ScrollBar)FindElement("VerticalScroll");
_wbData.LoadCompleted += new System.Windows.Navigation.LoadCompletedEventHandler(OnLoadCompleted);
_horzScroll.Scroll += new ScrollEventHandler(OnHorizontalScroll);
_vertScroll.Scroll += new ScrollEventHandler(OnVerticalScroll);
LoadDefault();
EnableSoundEffects(SoundEffects);
}
private void OnHorizontalScroll(object sender, ScrollEventArgs e)
{
// _wbData.Handle
mshtml.HTMLDocument htmlDoc = _wbData.Document as mshtml.HTMLDocument;
_horzPosition = (int)e.NewValue;
if (htmlDoc != null && htmlDoc.body != null)
htmlDoc.parentWindow.scroll(_horzPosition, _vertPosition);
}
private void OnVerticalScroll(object sender, ScrollEventArgs e)
{
mshtml.HTMLDocument htmlDoc = _wbData.Document as mshtml.HTMLDocument;
_vertPosition = (int)e.NewValue;
if (htmlDoc != null && htmlDoc.body != null)
htmlDoc.parentWindow.scroll(_horzPosition, _vertPosition);
}
private void OnLoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
mshtml.HTMLDocument htmlDoc = _wbData.Document as mshtml.HTMLDocument;
if (htmlDoc != null && htmlDoc.body != null)
{
mshtml.IHTMLElement2 body = (mshtml.IHTMLElement2)htmlDoc.body;
_horzScroll.Visibility = Visibility.Collapsed;
if (body.scrollHeight > _wbData.ActualHeight)
_vertScroll.Visibility = Visibility.Visible;
else
_vertScroll.Visibility = Visibility.Collapsed;
_vertScroll.ViewportSize = _wbData.ActualHeight;
_vertScroll.Maximum = body.scrollHeight - (_wbData.ActualHeight - 8);
_eventHelper = (HTMLDocumentEvents2_Event)_wbData.Document;
_eventHelper.onmousewheel -= OnMouseWheel;
_eventHelper.onmousewheel += new HTMLDocumentEvents2_onmousewheelEventHandler(OnMouseWheel);
}
}
private bool OnMouseWheel(mshtml.IHTMLEventObj pEvtObj)
{
mshtml.HTMLDocument htmlDoc = _wbData.Document as mshtml.HTMLDocument;
var wheelEventObj = (mshtml.IHTMLEventObj4)pEvtObj;
var delta = wheelEventObj.wheelDelta;
if (htmlDoc != null && htmlDoc.body != null && wheelEventObj != null)
{
_vertPosition += (int)wheelEventObj.wheelDelta;
htmlDoc.parentWindow.scroll(_horzPosition, _vertPosition);
}
pEvtObj.returnValue = true;
return true;
}

Categories

Resources