using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using SYNCOMLib;
using SYNCTRLLib;
namespace ConsoleApplication11
{
class Program
{
[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);
static SynAPICtrl api = new SynAPICtrl();
static SynDeviceCtrl device = new SynDeviceCtrl();
static SynPacketCtrl packet = new SynPacketCtrl();
static int deviceHandle;
static public int x = Screen.PrimaryScreen.Bounds.Width;
static public int y = Screen.PrimaryScreen.Bounds.Height;
static public float Xmin, Xmax, Ymin, Ymax = 0;
static void Main(string[] args)
{
try
{
api.Initialize();
}
catch (Exception e)
{
Console.WriteLine("Error calling API, you didn't have Synaptics hardware or driver (if you just installed it, you need to reboot)");
loop:
Console.WriteLine("Press enter to quit OR type \"info\"");
if (Console.ReadLine().Contains("info"))
{
Console.WriteLine("{0} Exception caught.", e);
goto loop;
}
return;
}
api.Activate();
//select the first device found
deviceHandle = api.FindDevice(SynConnectionType.SE_ConnectionAny, SynDeviceType.SE_DeviceTouchPad, -1);
device.Select(deviceHandle);
device.Activate();
device.OnPacket += SynTP_Dev_OnPacket;
//For console visual
Console.Title = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
Console.SetWindowSize(58, 9);
Console.SetBufferSize(58, 9);
Console.WriteLine("Move your finger to each corner to calibrate the software");
Console.WriteLine("Raw Data from touchpad:");
Console.WriteLine("X:0 Y:0");
Console.WriteLine("Xmin:" + Xmin + " Ymin:" + Ymin);
Console.WriteLine("Xmax:" + Xmax + " Ymax:" + Ymax);
Console.WriteLine("Position on Screen (calculated):");
Console.WriteLine("X:" + 0 + " Y:" + 0);
Console.WriteLine("Press enter to quit");
Console.ReadLine();
}
//Called at each change on touchpad, constantly calibrating values & moving cursor
static void SynTP_Dev_OnPacket()
{
var result = device.LoadPacket(packet);
if (packet.X > 1)
{
Console.SetCursorPosition(0, 2);
Console.Write(new string(' ', Console.WindowWidth));
Console.SetCursorPosition(0, 2);
Console.WriteLine("X:" + packet.X + " Y:" + packet.Y);
//Calibrate
if (Xmin == 0 && Xmax == 0)
{
Xmin = packet.X;
Xmax = packet.X;
Ymin = packet.Y;
Ymax = packet.Y;
}
if (packet.X > 1)
{
if (Xmin > packet.X)
Xmin = packet.X;
if (Xmax < packet.X)
Xmax = packet.X;
}
if (packet.Y > 1)
{
if (Ymin > packet.Y)
Ymin = packet.Y;
if (Ymax < packet.Y)
Ymax = packet.Y;
}
float targetx = (packet.X - Xmin) / (Xmax - Xmin) * x;
float targety = ((Ymax - Ymin) - (packet.Y - Ymin)) / (Ymax - Ymin) * y;
//move cursor using "calibrated" values
SetCursorPos((int)targetx, (int)targety); ;
//refresh output
Console.WriteLine("Xmin:" + Xmin + " Ymin:" + Ymin);
Console.WriteLine("Xmax:" + Xmax + " Ymax:" + Ymax);
Console.SetCursorPosition(0, 6);
Console.Write(new string(' ', Console.WindowWidth));
Console.SetCursorPosition(0, 6);
Console.WriteLine("X:" + targetx + " Y:" + targety);
}
}
}
}