Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF - Two Finger Horizontal scrolling on Macbook pro trackpad

The OS is Windows 8 on a Retina Macbook PRO.

i need to support both vertical scrolling using the trackpad and horizontal scrolling in my app.

I would like to move the WPF scrollviewer position based on the two finger swipes up-down (vertical scrolling) and left-right (horizontal scrolling).

As far as i see the vertical scrolling on the trackpad gets translated to a mouse wheel event in the framework. However, I found no way to recognize if a mouse wheel event is a horizontal scroll.

what events should i handle in WPF to be able to implement this?

like image 247
Faris Zacina Avatar asked Jan 15 '14 19:01

Faris Zacina


People also ask

How do I scroll sideways on Mac trackpad?

If you hold down the Shift key and scroll, you'll be scrolling horizontally instead of vertically. Now that's cool.

How do I turn on horizontal scrolling on Mac?

To reenable this behavior, go to Mos Settings > Advanced and then select "Shift" as the horizontal scrolling key. That should do it!


1 Answers

I wrote an article especially for you to know how to handle horizontal scrolling of touchpad in a WPF application. That is Support Horizontal Scrolling of TouchPad in WPF Application - walterlv


We need to fetch WM_MOUSEHWHEEL message from our WPF window. Yes! That mouse wheel message. We fetch vertical data from it before, but we now fetch horizontal data from it.

At first, we should hook the window message.

protected override void OnSourceInitialized(EventArgs e)
{
    var source = PresentationSource.FromVisual(_board);
    ((HwndSource) source)?.AddHook(Hook);
}

private IntPtr Hook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
    // Handle window message here.
    return IntPtr.Zero;
}

Next, handle WM_MOUSEHWHEEL:

const int WM_MOUSEHWHEEL = 0x020E;

private IntPtr Hook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
    switch (msg)
    {
        case WM_MOUSEHWHEEL:
            int tilt = (short) HIWORD(wParam);
            OnMouseTilt(tilt);
            return (IntPtr) 1;
    }
    return IntPtr.Zero;
}

/// <summary>
/// Gets high bits values of the pointer.
/// </summary>
private static int HIWORD(IntPtr ptr)
{
    var val32 = ptr.ToInt32();
    return ((val32 >> 16) & 0xFFFF);
}

/// <summary>
/// Gets low bits values of the pointer.
/// </summary>
private static int LOWORD(IntPtr ptr)
{
    var val32 = ptr.ToInt32();
    return (val32 & 0xFFFF);
}

private void OnMouseTilt(int tilt)
{
    // Write your horizontal handling codes here.
}

You can write horizontal scrolling code in OnMouseTilt method.

Better yet, you could pack all the codes above in a more common class and raise a MouseTilt event just like raising MouseWheel event.

like image 184
walterlv Avatar answered Oct 03 '22 08:10

walterlv