Monday, October 20, 2014

[C#]Synaptics custom touchpad gesture code.

Nah, my laptop cannot use updated version of the synaptics touchpad driver. The driver that comes with it does lack of using some windows 8 touchpad gesture. I'm start to think, maybe i can access the Synaptics API and yes, that's possible. After a lot search in google, i have a code that allows to switch to Windows apps and show Charm bar with touchpad gesture.

If someone need it, feel free to check my source code(well, it's dirty written)(Requires Input Simulator)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Runtime.InteropServices;
using System.Diagnostics;
using SYNCTRLLib;
using WindowsInput;
 
namespace mytest2 {
    class MyPoint {
        public int x;
        public int y;
        public MyPoint(int a,int b) {
            x=a;
            y=b;
        }
        public MyPoint() {
            x=0;
            y=0;
        }
        public void Reset() {
            x=0;
            y=0;
        }
    }
    public struct Point {
        public int x,y;
    }
    class Program {
        // FingerState==589824=tap
        static MyPoint WinAppsPoint=new MyPoint(0,2000);
        static MyPoint CharmBarPoint=new MyPoint(5000,6143);
        static MyPoint FirstLast=new MyPoint();
        static SynAPICtrl apictrl=new SynAPICtrl();
        static SynDeviceCtrl devctrl=new SynDeviceCtrl();
        static SynPacketCtrl packetctrl=new SynPacketCtrl();
        static bool is_press=false;
        static int devh;
        [DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
        public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
        [DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
        public static extern int GetCursorPos(ref Point _);
        [DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
        public static extern int SetCursorPos(int x,int y);
        [DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
        public static extern int SendInput(uint n,Input[] list,int size);
        static double tempy1=0;
        static double tempy2=0;
        static void mycallback() {
            if(devctrl.LoadPacket(packetctrl)==1) {
                if(is_press) {
                    if(packetctrl.FingerState==589824) {    // Tap
                        FirstLast.Reset();
                        is_press=false;
                        return;
                    }
                    else if(packetctrl.FingerState<=512) {  // OK
                        is_press=false;
                        int myx=Math.Min(FirstLast.x,FirstLast.y),myy=Math.Max(FirstLast.x,FirstLast.y);
                        int r=(int)Math.Atan2(tempy2-tempy1,FirstLast.y-FirstLast.x);
                        Debug.WriteLine("Rotation: {0}",r);
                        if(myx>=WinAppsPoint.x && myy<=WinAppsPoint.y && FirstLast.x<FirstLast.y) {
                            Debug.WriteLine("Windows Apps Scroll");
                            Point temp=new Point();
                            GetCursorPos(ref temp);
                            SetCursorPos(0,0);
                            mouse_event(2|4,0,0,0,0);
                            SetCursorPos(temp.x,temp.y);
                        } else if(myx>=CharmBarPoint.x && myy<=CharmBarPoint.y && FirstLast.x>FirstLast.y && (r==3||r==(-3)||r==2||r==(-2))) {
                            Debug.WriteLine("Charm Bar");
                            InputSimulator myinpt=new InputSimulator();
                            myinpt.Keyboard.KeyDown(WindowsInput.Native.VirtualKeyCode.LWIN);
                            myinpt.Keyboard.KeyDown(WindowsInput.Native.VirtualKeyCode.VK_C);
                            myinpt.Keyboard.KeyUp(WindowsInput.Native.VirtualKeyCode.LWIN);
                            myinpt.Keyboard.KeyUp(WindowsInput.Native.VirtualKeyCode.VK_C);
                        }
                        return;
                    }
                    FirstLast.y=packetctrl.X;
                    tempy2=packetctrl.Y;
                }
                else if(packetctrl.FingerState>512 && packetctrl.FingerState!=589824) {
                    Debug.WriteLine("Press");
                    is_press=true;
                    FirstLast.x=packetctrl.X;
                    tempy1=packetctrl.Y;
                }
                //Console.WriteLine(packetctrl.FingerState);
                //Console.WriteLine("{0} {1}",packetctrl.X,packetctrl.Y);
            }
        }
        static void Main(string[] args) {
            apictrl.Initialize();
            apictrl.Activate();
            devh=apictrl.FindDevice(SynConnectionType.SE_ConnectionAny,SynDeviceType.SE_DeviceTouchPad,0);
            devctrl.Select(devh);
            devctrl.Activate();
            devctrl.OnPacket+=mycallback;
            while(true) {
                Thread.Sleep(16);
            }
        }
    }
}

No comments:

Post a Comment