Skip to content

Instantly share code, notes, and snippets.

@JayToltTech
Last active June 30, 2022 12:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JayToltTech/05d7f466c442e875874ae09d359823fa to your computer and use it in GitHub Desktop.
Save JayToltTech/05d7f466c442e875874ae09d359823fa to your computer and use it in GitHub Desktop.
Eye Gaze Aware Discussion
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using ToltTech.Integration.Win32;
namespace ToltTech.Integration
{
public static class GazeAware
{
private const long WS_TOBIIGAZEAWARE = 0x2L;
private const string EyeGazeAware = "eyegaze:aware";
private static readonly IntPtr pStr = Marshal.StringToHGlobalUni(EyeGazeAware);
public static void SetGazeAware(Window window)
{
var hwnd = new WindowInteropHelper(window).Handle;
// Tobii Computer Control gaze aware indicator
var windowStyle = User32.GetWindowLong(hwnd, User32.GWL_EXSTYLE) | WS_TOBIIGAZEAWARE;
if (User32.SetWindowLong(hwnd, User32.GWL_EXSTYLE, windowStyle) == IntPtr.Zero)
{
Marshal.ThrowExceptionForHR(Marshal.GetLastWin32Error());
}
// MSFT Windows Input team proposal
if (!User32.SetProp(hwnd, EyeGazeAware, pStr))
{
Marshal.ThrowExceptionForHR(Marshal.GetLastWin32Error());
}
window.Unloaded += (object sender, RoutedEventArgs e) =>
{
User32.RemoveProp(hwnd, EyeGazeAware);
};
}
public static bool IsGazeAware()
{
var foregroundWindowHwnd = User32.GetForegroundWindow();
if (foregroundWindowHwnd == IntPtr.Zero)
{
// The foreground window can be NULL in certain circumstances, such as when a window is losing activation.
return false;
}
var tobiiGazeAware = (User32.GetWindowLong(foregroundWindowHwnd, User32.GWL_EXSTYLE) & WS_TOBIIGAZEAWARE) == WS_TOBIIGAZEAWARE;
var eyeGazeAware = User32.GetProp(foregroundWindowHwnd, EyeGazeAware) != IntPtr.Zero;
return tobiiGazeAware || eyeGazeAware;
}
}
}
using System;
using System.Text;
using System.Threading;
using ToltTech.Integration.Win32;
namespace FindGazeAware
{
internal class Program
{
static void Main()
{
while (true)
{
var foregroundWindowHwnd = User32.GetForegroundWindow();
int size = User32.GetWindowTextLength(foregroundWindowHwnd);
if (size++ > 0 && User32.IsWindowVisible(foregroundWindowHwnd))
{
var windowTitle = new StringBuilder(size);
User32.GetWindowText(foregroundWindowHwnd, windowTitle, size);
Console.WriteLine($"{windowTitle} - IsGazeAware: {ToltTech.Integration.GazeAware.IsGazeAware()}");
}
Thread.Sleep(5000);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment