In a WPF application, I want a user tracking system to keep statistics on how users use the application. In other words, I'm looking for a way to track which commands are executed and how they were called by the user (by clicking a button on the toolbar, using keyboard shortcuts, etc.) So far, I have not found a good way to do this using the WPF command template ...
Do you have any ideas / suggestions on how to achieve / create something like this without overriding each control used in the application?
For discussion purposes, I created a very basic WPF application containing a toolbar with a single Save button, TextBox, and ListBox. I also added KeyBinding to run the Save command when pressing CTRL + S.
The first task is to determine which device (mouse or keyboard) was used to run the command.
The second task is to determine which control is used to run the command (command source). I am not interested in knowing which control had keyboard focus when running the command, I would like to know which control was used to run the command (usually this is a button, hyperlink, MenuItem from ContextMenu, etc.)
MainWindow.xaml
<Window x:Class="TrackingCommands.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" x:Name="Me" Height="480" Width="600">
<Window.CommandBindings>
<CommandBinding Command="Save" Executed="OnSaveCommandExecuted" CanExecute="OnSaveCommandCanExecute" />
</Window.CommandBindings>
<Window.InputBindings>
<KeyBinding Command="Save" Gesture="CTRL+S"/>
</Window.InputBindings>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<ToolBarTray Grid.Row="0">
<ToolBar>
<Button Command="Save" Content="Save"/>
</ToolBar>
</ToolBarTray>
<TextBox Grid.Row="1" TextWrapping="Wrap" AcceptsReturn="True"/>
</Grid>
</Window>
MainWindow.xaml.cs
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
}
private void OnSaveCommandExecuted(object sender, ExecutedRoutedEventArgs e)
{
e.Handled = true;
}
private void OnSaveCommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
e.Handled = true;
}
}
EDIT
I realized that my initial question was a little vague, I apologize. I will try to give more information and ask a more accurate question.
I know that just saving a list of commands that have been executed is enough. The challenge here is to extract which device was used to initiate the command initially: mouse or keyboard?
"", , , , "Enter" . , CTRL + S . , ?
ViewModel? , : . , , . ? - Button Click KeyDown, ?