Last active
May 1, 2024 16:59
-
-
Save sdragou/5094010 to your computer and use it in GitHub Desktop.
Gesture mouse in C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Leap; | |
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Data; | |
using System.Drawing; | |
using System.Linq; | |
using System.Text; | |
using System.Windows.Forms; | |
using System.Runtime.InteropServices; | |
using System.Threading; | |
namespace gesture_mouse | |
{ | |
public partial class Form1 : Form | |
{ | |
SampleListener listener = new SampleListener(); | |
Controller controller = new Controller(); | |
public Form1() | |
{ | |
InitializeComponent(); | |
listener.FrameReady += listener_FrameReady; | |
controller.EnableGesture(Gesture.GestureType.TYPESCREENTAP); | |
controller.EnableGesture(Gesture.GestureType.TYPESWIPE); | |
controller.EnableGesture(Gesture.GestureType.TYPECIRCLE); | |
} | |
protected override void OnFormClosed(FormClosedEventArgs e) | |
{ | |
base.OnFormClosed(e); | |
controller.RemoveListener(listener); | |
controller.Dispose(); | |
} | |
float x = 0; | |
float y = 0; | |
float k = 0.125f; | |
float tracking = 0; | |
float trackSpeed = 4; | |
bool right_down = false; | |
bool left_down = false; | |
void listener_FrameReady(Controller controller) | |
{ | |
Frame frame = controller.Frame(); | |
if (frame.Pointables.Count > 0) | |
{ | |
Pointable p = frame.Pointables[0]; | |
Leap.Screen s = controller.CalibratedScreens.ClosestScreenHit(p); | |
Hand hand = frame.Hands[0]; | |
FingerList fingers = hand.Fingers; | |
// Check if the hand has any fingers | |
Vector v = Vector.Zero; | |
if (frame.Pointables.Count == 0) | |
{ | |
if (right_down == true) | |
{ | |
right_down = false; | |
tracking = 0; | |
MouseHelper.MouseEvent(MouseHelper.MouseEventFlags.RightUp); | |
} | |
if (left_down == true) | |
{ | |
left_down = false; | |
tracking = 0; | |
MouseHelper.MouseEvent(MouseHelper.MouseEventFlags.LeftUp); | |
} | |
} | |
if (frame.Pointables.Count == 1) | |
{ | |
int numGestures = frame.Gestures().Count(); | |
if (numGestures > 0) | |
{ | |
for (int i = 0; i < numGestures; i++) | |
{ | |
if (frame.Gestures()[i].Type == Leap.Gesture.GestureType.TYPESCREENTAP) | |
{ | |
ScreenTapGesture tap = new ScreenTapGesture(frame.Gestures()[i]); | |
} | |
else if (frame.Gestures()[i].Type == Leap.Gesture.GestureType.TYPESWIPE) | |
{ | |
SwipeGesture swipe = new SwipeGesture(frame.Gestures()[i]); | |
MouseHelper.MouseEvent(MouseHelper.MouseEventFlags.Wheel, (long)(swipe.Direction.y * swipe.Speed)); | |
} | |
else if (frame.Gestures()[i].Type == Leap.Gesture.GestureType.TYPECIRCLE) | |
{ | |
CircleGesture circle = new CircleGesture(frame.Gestures()[i]); | |
float progress = circle.Progress; | |
} | |
} | |
} | |
else | |
{ | |
x += (((float)0.5 + frame.Pointables[0].TipPosition.Normalized.x) * s.WidthPixels / (float)1.0 - x) * k; | |
y += (((300 - frame.Pointables[0].TipPosition.y) * s.HeightPixels / 150) - y) * k; | |
debugbox1.Text = x.ToString() + " --- " + frame.Pointables[0].TipPosition.y.ToString(); | |
MouseHelper.SetCursorPosition((int)x, (int)y); | |
} | |
} | |
if (frame.Pointables.Count == 2) | |
{ | |
if (frame.Gestures().Count() == 0) | |
{ | |
v = fingers[0].TipPosition; | |
double dist = Math.Sqrt(Math.Pow(v.x - fingers[1].TipPosition.x, 2) + Math.Pow(v.y - fingers[1].TipPosition.y, 2)); | |
if (dist > 31 || dist < 7) | |
{ | |
if (left_down == true) | |
{ | |
MouseHelper.MouseEvent(MouseHelper.MouseEventFlags.LeftUp); | |
left_down = false; | |
} | |
tracking = 0; | |
} | |
else | |
{ | |
tracking += trackSpeed; | |
if (left_down == false) | |
{ | |
MouseHelper.MouseEvent(MouseHelper.MouseEventFlags.LeftDown); | |
left_down = true; | |
} | |
} | |
float px = (v.Normalized.x + fingers[1].TipPosition.Normalized.x) / 2; | |
float py = (v.y + fingers[1].TipPosition.y) / 2; | |
x = ((float)0.5 + px) * s.WidthPixels / (float)1.0; | |
y = ((300 - py) * s.HeightPixels / 150); | |
if (tracking > 100 || tracking <= 0) MouseHelper.SetCursorPosition((int)x, (int)y); | |
} | |
else | |
{ | |
for (int i = 0; i < frame.Gestures().Count(); i++) | |
{ | |
if (frame.Gestures()[i].Type == Leap.Gesture.GestureType.TYPESCREENTAP) | |
{ | |
ScreenTapGesture tap = new ScreenTapGesture(frame.Gestures()[i]); | |
} | |
else if (frame.Gestures()[i].Type == Leap.Gesture.GestureType.TYPESWIPE) | |
{ | |
SwipeGesture swipe = new SwipeGesture(frame.Gestures()[i]); | |
MouseHelper.MouseEvent(MouseHelper.MouseEventFlags.Wheel, (long)(swipe.Direction.y * swipe.Speed)); | |
} | |
else if (frame.Gestures()[i].Type == Leap.Gesture.GestureType.TYPECIRCLE) | |
{ | |
CircleGesture circle = new CircleGesture(frame.Gestures()[i]); | |
float progress = circle.Progress; | |
} | |
} | |
} | |
} | |
// ------------------------------------- >2 pointables ---------------------- | |
if (frame.Pointables.Count > 2) | |
{ | |
if (frame.Gestures().Count() > 0) | |
{ | |
//debugbox1.Text = "Gesture" + frame.Gestures()[0].ToString(); | |
int numGestures = frame.Gestures().Count(); | |
if (numGestures > 0) | |
{ | |
for (int i = 0; i < numGestures; i++) | |
{ | |
if (frame.Gestures()[i].Type == Leap.Gesture.GestureType.TYPESCREENTAP) | |
{ | |
ScreenTapGesture tap = new ScreenTapGesture(frame.Gestures()[i]); | |
} | |
else if (frame.Gestures()[i].Type == Leap.Gesture.GestureType.TYPESWIPE) | |
{ | |
SwipeGesture swipe = new SwipeGesture(frame.Gestures()[i]); | |
MouseHelper.MouseEvent(MouseHelper.MouseEventFlags.Wheel, (long)(swipe.Direction.y * swipe.Speed)); | |
} | |
else if (frame.Gestures()[i].Type == Leap.Gesture.GestureType.TYPECIRCLE) | |
{ | |
CircleGesture circle = new CircleGesture(frame.Gestures()[i]); | |
float progress = circle.Progress; | |
} | |
} | |
} | |
} | |
} | |
} | |
checkBox1.Checked = left_down; | |
checkBox2.Checked = right_down; | |
} | |
private void button1_Click(object sender, EventArgs e) | |
{ | |
controller.AddListener(listener); | |
} | |
private void button2_Click(object sender, EventArgs e) | |
{ | |
controller.RemoveListener(listener); | |
} | |
private void toolStripMenuItem1_Click(object sender, EventArgs e) | |
{ | |
Application.Exit(); | |
} | |
} | |
class SampleListener : Listener | |
{ | |
public override void OnInit(Controller controller) | |
{ } | |
public override void OnConnect(Controller controller) | |
{ } | |
public override void OnDisconnect(Controller controller) | |
{ } | |
public override void OnExit(Controller controller) | |
{ } | |
public delegate void FrameHandler(Controller controller); | |
public event FrameHandler FrameReady; | |
public override void OnFrame(Controller controller) | |
{ | |
if (FrameReady != null) | |
{ | |
FrameReady(controller); | |
} | |
} | |
} | |
class MouseHelper | |
{ | |
[StructLayout(LayoutKind.Sequential)] | |
public struct MousePoint | |
{ | |
public int X; | |
public int Y; | |
public MousePoint(int x, int y) | |
{ | |
X = x; | |
Y = y; | |
} | |
} | |
[Flags] | |
public enum MouseEventFlags | |
{ | |
LeftDown = 0x00000002, | |
LeftUp = 0x00000004, | |
MiddleDown = 0x00000020, | |
MiddleUp = 0x00000040, | |
Move = 0x00000001, | |
Absolute = 0x00008000, | |
RightDown = 0x00000008, | |
RightUp = 0x00000010, | |
Wheel = 0x00000800, | |
XDown = 0x00000080, | |
XUp = 0x00000100 | |
} | |
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] | |
public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo); | |
[DllImport("user32.dll", EntryPoint = "SetCursorPos")] | |
[return: MarshalAs(UnmanagedType.Bool)] | |
private static extern bool SetCursorPos(int X, int Y); | |
[DllImport("user32.dll")] | |
[return: MarshalAs(UnmanagedType.Bool)] | |
private static extern bool GetCursorPos(out MousePoint lpMousePoint); | |
public static void SetCursorPosition(int X, int Y) | |
{ | |
SetCursorPos(X, Y); | |
} | |
public static void SetCursorPosition(MousePoint point) | |
{ | |
SetCursorPos(point.X, point.Y); | |
} | |
public static MousePoint GetCursorPosition() | |
{ | |
MousePoint currentMousePoint; | |
var gotPoint = GetCursorPos(out currentMousePoint); | |
if (!gotPoint) { currentMousePoint = new MousePoint(0, 0); } | |
return currentMousePoint; | |
} | |
public static void MouseEvent(MouseEventFlags value) | |
{ | |
MouseEvent(value, 0); | |
} | |
public static void MouseEvent(MouseEventFlags value, long data) | |
{ | |
MousePoint position = GetCursorPosition(); | |
mouse_event((int)value, position.X, position.Y, data, 0); | |
} | |
public static void MouseEvent(int x, int y, MouseEventFlags value, long data) | |
{ | |
MousePoint position = GetCursorPosition(); | |
mouse_event((int)value, x, y, data, 0); | |
} | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace gesture_mouse | |
{ | |
partial class Form1 | |
{ | |
/// <summary> | |
/// Required designer variable. | |
/// </summary> | |
private System.ComponentModel.IContainer components = null; | |
/// <summary> | |
/// Clean up any resources being used. | |
/// </summary> | |
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> | |
protected override void Dispose(bool disposing) | |
{ | |
if (disposing && (components != null)) | |
{ | |
components.Dispose(); | |
} | |
base.Dispose(disposing); | |
} | |
#region Windows Form Designer generated code | |
/// <summary> | |
/// Required method for Designer support - do not modify | |
/// the contents of this method with the code editor. | |
/// </summary> | |
private void InitializeComponent() | |
{ | |
this.components = new System.ComponentModel.Container(); | |
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1)); | |
this.checkBox1 = new System.Windows.Forms.CheckBox(); | |
this.checkBox2 = new System.Windows.Forms.CheckBox(); | |
this.gesture_mouse = new System.Windows.Forms.NotifyIcon(this.components); | |
this.debugbox1 = new System.Windows.Forms.TextBox(); | |
this.speed = new System.Windows.Forms.TextBox(); | |
this.button1 = new System.Windows.Forms.Button(); | |
this.button2 = new System.Windows.Forms.Button(); | |
this.SuspendLayout(); | |
// | |
// checkBox1 | |
// | |
this.checkBox1.AutoSize = true; | |
this.checkBox1.Location = new System.Drawing.Point(71, 13); | |
this.checkBox1.Name = "checkBox1"; | |
this.checkBox1.Size = new System.Drawing.Size(98, 21); | |
this.checkBox1.TabIndex = 0; | |
this.checkBox1.Text = "checkBox1"; | |
this.checkBox1.UseVisualStyleBackColor = true; | |
// | |
// checkBox2 | |
// | |
this.checkBox2.AutoSize = true; | |
this.checkBox2.Location = new System.Drawing.Point(213, 12); | |
this.checkBox2.Name = "checkBox2"; | |
this.checkBox2.Size = new System.Drawing.Size(98, 21); | |
this.checkBox2.TabIndex = 1; | |
this.checkBox2.Text = "checkBox2"; | |
this.checkBox2.UseVisualStyleBackColor = true; | |
// | |
// gesture_mouse | |
// | |
this.gesture_mouse.BalloonTipIcon = System.Windows.Forms.ToolTipIcon.Info; | |
this.gesture_mouse.BalloonTipText = "Gesture_mouse"; | |
this.gesture_mouse.Icon = ((System.Drawing.Icon)(resources.GetObject("gesture_mouse.Icon"))); | |
this.gesture_mouse.Text = "Gesture_mouse"; | |
this.gesture_mouse.Visible = true; | |
this.gesture_mouse.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.gesture_mouse_MouseDoubleClick); | |
// | |
// debugbox1 | |
// | |
this.debugbox1.ImeMode = System.Windows.Forms.ImeMode.Disable; | |
this.debugbox1.Location = new System.Drawing.Point(71, 41); | |
this.debugbox1.Name = "debugbox1"; | |
this.debugbox1.Size = new System.Drawing.Size(240, 22); | |
this.debugbox1.TabIndex = 2; | |
// | |
// speed | |
// | |
this.speed.Location = new System.Drawing.Point(71, 70); | |
this.speed.Name = "speed"; | |
this.speed.Size = new System.Drawing.Size(240, 22); | |
this.speed.TabIndex = 3; | |
this.speed.TextChanged += new System.EventHandler(this.speed_TextChanged); | |
// | |
// button1 | |
// | |
this.button1.Location = new System.Drawing.Point(383, 35); | |
this.button1.Name = "button1"; | |
this.button1.Size = new System.Drawing.Size(75, 23); | |
this.button1.TabIndex = 4; | |
this.button1.Text = "Start"; | |
this.button1.UseVisualStyleBackColor = true; | |
this.button1.Click += new System.EventHandler(this.button1_Click); | |
// | |
// button2 | |
// | |
this.button2.Location = new System.Drawing.Point(383, 69); | |
this.button2.Name = "button2"; | |
this.button2.Size = new System.Drawing.Size(75, 23); | |
this.button2.TabIndex = 5; | |
this.button2.Text = "Stop"; | |
this.button2.UseVisualStyleBackColor = true; | |
this.button2.Click += new System.EventHandler(this.button2_Click); | |
// | |
// Form1 | |
// | |
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F); | |
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; | |
this.ClientSize = new System.Drawing.Size(489, 154); | |
this.Controls.Add(this.button2); | |
this.Controls.Add(this.button1); | |
this.Controls.Add(this.speed); | |
this.Controls.Add(this.debugbox1); | |
this.Controls.Add(this.checkBox2); | |
this.Controls.Add(this.checkBox1); | |
this.Name = "Form1"; | |
this.Text = "Gesture Mouse"; | |
this.ResumeLayout(false); | |
this.PerformLayout(); | |
} | |
#endregion | |
private System.Windows.Forms.CheckBox checkBox1; | |
private System.Windows.Forms.CheckBox checkBox2; | |
private System.Windows.Forms.NotifyIcon gesture_mouse; | |
private System.Windows.Forms.TextBox debugbox1; | |
private System.Windows.Forms.TextBox speed; | |
private System.Windows.Forms.Button button1; | |
private System.Windows.Forms.Button button2; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment