Sunday, 16 December 2012

Tracking MouseMove in Silverlight

namespace MouseTracking
{
public partial class MainPage
{
readonly MouseTracker tracker = new MouseTracker();

public MainPage()
{
InitializeComponent();
tracker.StartTracking(this);
}
}
}
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Input;

namespace MouseTracking
{
public class MouseTracker
{
public MouseTracker()
{
TraceMouse = true;
}

public bool TraceMouse { get; set; }

public void StartTracking(UserControl sender)
{
sender.Loaded += sender_Loaded;
//TODO: Hook into specific FrameworkElements events? MouseOver, select, click, etc
}

void sender_Loaded(object sender, RoutedEventArgs e)
{
var page = (UserControl) sender;
page.MouseLeftButtonDown += page_MouseLeftButtonDown;
page.SizeChanged += page_SizeChanged;
page.MouseMove += page_MouseMove;
page.KeyDown += page_KeyDown;
page.Unloaded += page_Unloaded;

var data = string.Format("resolution={0}x{1}",
HtmlPage.Window.Eval("screen.width"),
HtmlPage.Window.Eval("screen.height"));

Write(DateTime.Now, page.ToString(), "Loaded", data);
}

void page_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var data = string.Format("ClickCount={0}", e.ClickCount);

Write(DateTime.Now, sender.ToString(), "page_MouseLeftButtonDown", data);
}

void page_SizeChanged(object sender, SizeChangedEventArgs e)
{
var data = string.Format("screen={0}x{1} page={2}x{3}",
(Application.Current.RootVisual as FrameworkElement).ActualWidth,
(Application.Current.RootVisual as FrameworkElement).ActualHeight,
(sender as FrameworkElement).ActualWidth,
(sender as FrameworkElement).ActualHeight);

Write(DateTime.Now, sender.ToString(), "SizeChanged", data);

}

private void page_KeyDown(object sender, KeyEventArgs e)
{
Write(DateTime.Now, sender.ToString(), "KeyDown", string.Format("key={0}", e.Key));
}

void page_Unloaded(object sender, RoutedEventArgs e)
{
Debug.WriteLine("Removing event handlers");

var page = (UserControl)sender;
page.Loaded -= sender_Loaded;
page.MouseLeftButtonDown -= page_MouseLeftButtonDown;
page.SizeChanged -= page_SizeChanged;
page.MouseMove -= page_MouseMove;
page.KeyDown -= page_KeyDown;
page.Unloaded -= page_Unloaded;
}

void page_MouseMove(object sender, MouseEventArgs e)
{
Write(DateTime.Now, sender.ToString(), "MouseMove", string.Format("coord={0},{1}", e.GetPosition(null).X, e.GetPosition(null).Y));
}

private void Write(DateTime time, string page, string eventName, string data)
{
string ipAddress = Application.Current.Resources["ipAddress"].ToString();

// call service here (ipaddress can be retrieved serverside using HttpContext object)
// for now we'll just output to debug window

if (TraceMouse)
Debug.WriteLine("{0} {1} {2} {3} {4}", ipAddress, time.ToString("MM/dd/yyyy HH:mm:ss.fff"), page, eventName, data);
}
}
}
<body>
<form id="form1" runat="server" style="height:100%">
<div id="silverlightControlHost">
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
<param name="source" value="ClientBin/MouseTracking.xap"/>
<param name="onError" value="onSilverlightError" />
<param name="background" value="white" />
<param name="minRuntimeVersion" value="5.0.61118.0" />
<param name="initParams" value="ipAddress=<%= Request.UserHostAddress %>"/>
<param name="autoUpgrade" value="true" />
<a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=5.0.61118.0" style="text-decoration:none">
<img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="Get Microsoft Silverlight" style="border-style:none"/>
</a>
</object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe></div>
</form>
</body>
</html>
private void Application_Startup(object sender, StartupEventArgs e)
{
if (e.InitParams != null)
{
foreach (var item in e.InitParams)
{
this.Resources.Add(item.Key, item.Value);
}
}

this.RootVisual = new MainPage();
}

Source:  https://github.com/stevenh77/MouseTracking

1 comment:

  1. Wow, what a great experience we had here ! I just love it so much that the greedy-visual data gnome in me wants more ! Great work !

    ReplyDelete