Skip to content

Instantly share code, notes, and snippets.

@ksasao
Created December 2, 2015 11:41
Show Gist options
  • Save ksasao/defa6473cc569323b833 to your computer and use it in GitHub Desktop.
Save ksasao/defa6473cc569323b833 to your computer and use it in GitHub Desktop.
Tobii EyeXDotNet の Minimal Samples/MinimalGazeDataStream/Program.cs を改変して画面を見つめることでクリックできるようにしたコードです。
namespace MinimalGazeDataStream
{
using EyeXFramework;
using System;
using System.Runtime.InteropServices;
using Tobii.EyeX.Framework;
public class NativeMethods
{
[System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "SetCursorPos")]
[return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern bool SetCursorPos(int x, int y);
[DllImport("user32.dll")]
private static extern void SendInput(int nInputs, ref INPUT pInputs, int cbsize);
public static void RightClick()
{
const int num = 2;
INPUT[] inp = new INPUT[num];
// マウスの右ボタンを押す
inp[0].type = INPUT_MOUSE;
inp[0].mi.dwFlags = MOUSEEVENTF_RIGHTDOWN;
inp[0].mi.dx = 0;
inp[0].mi.dy = 0;
inp[0].mi.mouseData = 0;
inp[0].mi.dwExtraInfo = 0;
inp[0].mi.time = 0;
// マウスの右ボタンを離す
inp[1].type = INPUT_MOUSE;
inp[1].mi.dwFlags = MOUSEEVENTF_RIGHTUP;
inp[1].mi.dx = 0;
inp[1].mi.dy = 0;
inp[1].mi.mouseData = 0;
inp[1].mi.dwExtraInfo = 0;
inp[1].mi.time = 0;
SendInput(num, ref inp[0], Marshal.SizeOf(inp[0]));
}
public static void LeftClick()
{
const int num = 2;
INPUT[] inp = new INPUT[num];
// マウスの左ボタンを押す
inp[0].type = INPUT_MOUSE;
inp[0].mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
inp[0].mi.dx = 0;
inp[0].mi.dy = 0;
inp[0].mi.mouseData = 0;
inp[0].mi.dwExtraInfo = 0;
inp[0].mi.time = 0;
// マウスの左ボタンを離す
inp[1].type = INPUT_MOUSE;
inp[1].mi.dwFlags = MOUSEEVENTF_LEFTUP;
inp[1].mi.dx = 0;
inp[1].mi.dy = 0;
inp[1].mi.mouseData = 0;
inp[1].mi.dwExtraInfo = 0;
inp[1].mi.time = 0;
SendInput(num, ref inp[0], Marshal.SizeOf(inp[0]));
}
private const int INPUT_MOUSE = 0; // マウスイベント
private const int MOUSEEVENTF_MOVE = 0x1; // マウスを移動する
private const int MOUSEEVENTF_ABSOLUTE = 0x8000; // 絶対座標指定
private const int MOUSEEVENTF_LEFTDOWN = 0x2; // 左 ボタンを押す
private const int MOUSEEVENTF_LEFTUP = 0x4; // 左 ボタンを離す
private const int MOUSEEVENTF_RIGHTDOWN = 0x8; // 右 ボタンを押す
private const int MOUSEEVENTF_RIGHTUP = 0x10; // 右 ボタンを離す
[StructLayout(LayoutKind.Explicit)]
private struct INPUT
{
[FieldOffset(0)]
public int type;
[FieldOffset(4)]
public MOUSEINPUT mi;
};
// マウスイベント(mouse_eventの引数と同様のデータ)
[StructLayout(LayoutKind.Sequential)]
private struct MOUSEINPUT
{
public int dx;
public int dy;
public int mouseData;
public int dwFlags;
public int time;
public int dwExtraInfo;
};
}
public static class Program
{
public static void Main(string[] args)
{
int xx = 0;
int yy = 0;
int x2 = 0;
int y2 = 0;
// Initialize the EyeX host.
using (var eyeXHost = new EyeXHost())
{
eyeXHost.Init();
using (var lightlyFilteredGazeDataStream = eyeXHost.CreateDataStream(new GazePointDataStream(GazePointDataMode.LightlyFiltered)))
{
// Write the data to the console.
lightlyFilteredGazeDataStream.Next += (s, e) =>
{
Console.WriteLine("Gaze point at ({0:0.0}, {1:0.0}) @{2:0}", e.X, e.Y, e.Timestamp);
xx =(int) ((xx * 10 + e.X) / 11);
yy = (int)((yy * 10 + e.Y) / 11);
x2 = (int)((x2 * 20 + xx) / 21);
y2 = (int)((y2 * 20 + yy) / 21);
System.Windows.Forms.Cursor.Position = new System.Drawing.Point((int)xx, (int)yy);
if (Math.Abs(xx - x2) < 30 && Math.Abs(yy - y2) < 30)
{
NativeMethods.LeftClick();
x2 = 0;
y2 = 0;
}
};
// Let it run until a key is pressed.
Console.WriteLine("Listening for gaze data, press any key to exit...");
Console.In.Read();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment