Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to make Keystroke history follow mouse position #78

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions KeyNStroke/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public static void Main()
SettingsStore mySettings;
Window welcomeWindow;
Settings1 settingsWindow;
KeystrokeDisplay KeystrokeHistoryWindow = null;
bool KeystrokeHistoryVisible = false;
bool? attached = null;

protected override void OnStartup(StartupEventArgs e)
{
Expand Down Expand Up @@ -231,6 +234,17 @@ private void OnSettingChanged(object sender, PropertyChangedEventArgs e)
case "EnableKeystrokeHistory":
OnKeystrokeHistorySettingChanged();
break;
case "EnableCursorFollow":
if (attached==null)
{
attached = mySettings.EnableCursorFollow;
}else if (attached != mySettings.EnableCursorFollow)
{
mySettings.SaveAll();
System.Windows.Forms.Application.Restart(); //We do a restart to avoid some bugs
Environment.Exit(0);
}
break;
case "EnableAnnotateLine":
OnAnnotateLineSettingChanged();
break;
Expand Down Expand Up @@ -260,9 +274,6 @@ private void OnSettingChanged(object sender, PropertyChangedEventArgs e)

#region Keystroke History

KeystrokeDisplay KeystrokeHistoryWindow;
bool KeystrokeHistoryVisible;

private void OnKeystrokeHistorySettingChanged()
{
if (mySettings.EnableKeystrokeHistory && !mySettings.Standby)
Expand All @@ -280,7 +291,13 @@ private void EnableKeystrokeHistory()
if (KeystrokeHistoryVisible || KeystrokeHistoryWindow != null)
return;
KeystrokeHistoryVisible = true; // Prevent Recursive call
KeystrokeHistoryWindow = new KeystrokeDisplay(myKeystrokeConverter, mySettings);
if (mySettings.EnableCursorFollow)
{
EnableMouseHook();
KeystrokeHistoryWindow = new KeystrokeDisplay(myMouseHook, myKeystrokeConverter, mySettings);
}else{
KeystrokeHistoryWindow = new KeystrokeDisplay(myKeystrokeConverter, mySettings);
}
KeystrokeHistoryWindow.Show();
}

Expand All @@ -289,6 +306,7 @@ private void DisableKeystrokeHistory()
KeystrokeHistoryVisible = false;
if (KeystrokeHistoryWindow == null)
return;
DisableMouseHookIfNotNeeded();
KeystrokeHistoryWindow.Close();
KeystrokeHistoryWindow = null;
}
Expand Down Expand Up @@ -431,7 +449,7 @@ private void EnableMouseHook()

private void DisableMouseHookIfNotNeeded()
{
if (CursorIndicatorWindow == null && ButtonIndicatorWindow == null && AnnotateLineWindow == null)
if (CursorIndicatorWindow == null && ButtonIndicatorWindow == null && AnnotateLineWindow == null && (!mySettings.EnableCursorFollow || KeystrokeHistoryWindow == null))
DisableMouseHook();
}

Expand Down
56 changes: 49 additions & 7 deletions KeyNStroke/KeystrokeDisplay.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
Expand Down Expand Up @@ -29,24 +29,37 @@ public partial class KeystrokeDisplay : Window
{
readonly SettingsStore settings;
readonly IKeystrokeEventProvider k;
IMouseRawEventProvider m = null;
IntPtr windowHandle;
Brush OrigInnerPanelBackgroundColor;

public KeystrokeDisplay(IKeystrokeEventProvider k, SettingsStore s)
public KeystrokeDisplay(IMouseRawEventProvider m, IKeystrokeEventProvider k, SettingsStore s)
{
InitializeComponent();
InitializeAnimations();

this.m = m;
this.k = k;
this.settings = s;
initializeDisplay();
}

public KeystrokeDisplay(IKeystrokeEventProvider k, SettingsStore s)
{
this.k = k;
this.settings = s;
initializeDisplay();
}

private void initializeDisplay()
{
InitializeComponent();
InitializeAnimations();

this.settings.EnableSettingsMode = false;
this.settings.EnablePasswordMode = false;
this.settings.PropertyChanged += SettingChanged;
this.settings.CallPropertyChangedForAllProperties();

this.buttonResizeWindow.Settings = s;
this.buttonResizeInnerPanel.Settings = s;
this.buttonResizeWindow.Settings = this.settings;
this.buttonResizeInnerPanel.Settings = this.settings;

//addWelcomeInfo();
}
Expand All @@ -57,6 +70,12 @@ private void Window_Loaded(object sender, RoutedEventArgs e)
InitPeriodicTopmostTimer();
windowHandle = new WindowInteropHelper(this).Handle;

if (this.m != null)
{
this.m.MouseEvent += m_MouseEvent;
SetFormStyles();
}

this.k.KeystrokeEvent += KeystrokeEvent;

OrigInnerPanelBackgroundColor = innerPanel.Background;
Expand All @@ -69,6 +88,29 @@ private void Window_Loaded(object sender, RoutedEventArgs e)
}
}

void m_MouseEvent(MouseRawEventArgs raw_e)
{
if (raw_e.Action == MouseAction.Move)
{
UpdatePosition(raw_e.Position);
}
}

void UpdatePosition(NativeMethodsMouse.POINT cursorPosition)
{
NativeMethodsWindow.SetWindowPosition(windowHandle,
(int)(cursorPosition.X + settings.CursorFollowDistance),
(int)(cursorPosition.Y + settings.CursorFollowDistance));
}

void SetFormStyles()
{
Log.e("CI", $"WindowHandle={windowHandle}");
NativeMethodsGWL.ClickThrough(windowHandle);
NativeMethodsGWL.HideFromAltTab(windowHandle);
UpdatePosition(NativeMethodsMouse.CursorPosition);
}

#region periodically make TopMost
readonly DispatcherTimer makeTopMostTimer = new DispatcherTimer();
void InitPeriodicTopmostTimer()
Expand Down
7 changes: 7 additions & 0 deletions KeyNStroke/Settings1.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
Margin="5">
<CheckBox x:Name="cb_enableKeystrokeDisplay"
IsChecked="{Binding EnableKeystrokeHistory}">Show Keystroke History</CheckBox>
<CheckBox x:Name="cb_cursorfollow"
IsChecked="{Binding EnableCursorFollow}">Label follows cursor position</CheckBox>
<StackPanel Margin="0,0,0,5">
<ComboBox x:Name="cb_KeystrokeMethod"
Margin="10"
Expand Down Expand Up @@ -98,6 +100,11 @@
Minimum="10.0"
Maximum="100.0"
Value="{Binding LineDistance, Mode=TwoWay}" />
<uc:LabeledSlider x:Name="slider_cf_distance"
Title="Follow Distance"
Minimum="0"
Maximum="200"
Value="{Binding CursorFollowDistance, Mode=TwoWay}" />
<StackPanel Orientation="Horizontal">
<Label>No of lines in history</Label>
<xctk:IntegerUpDown x:Name="slider_historycount"
Expand Down
22 changes: 21 additions & 1 deletion KeyNStroke/SettingsStore.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.Win32;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
Expand Down Expand Up @@ -203,6 +203,8 @@ class Settings
[DataMember] public Nullable<bool> enableHistoryTimeout = null;
[DataMember] public Nullable<bool> enableWindowFade = null;
[DataMember] public Nullable<bool> enableCursorIndicator = null;
[DataMember] public Nullable<bool> enableCursorFollow = null;
[DataMember] public Nullable<double> cursorFollowDistance = null;
[DataMember] public Nullable<double> cursorIndicatorOpacity = null;
[DataMember] public Nullable<double> cursorIndicatorSize = null;
[DataMember] public SerializableColor2 cursorIndicatorColor = null;
Expand Down Expand Up @@ -416,6 +418,20 @@ public bool EnableCursorIndicator
set { i.enableCursorIndicator = value; OnSettingChanged("EnableCursorIndicator"); }
}

public bool EnableCursorFollowDefault = false;
public bool EnableCursorFollow
{
get { return Or(i.enableCursorFollow, EnableCursorFollowDefault); }
set { i.enableCursorFollow = value; OnSettingChanged("EnableCursorFollow"); }
}

public double CursorFollowDistanceDefault = 56;
public double CursorFollowDistance
{
get { return Or(i.cursorFollowDistance, CursorFollowDistanceDefault); }
set { i.cursorFollowDistance = value; OnSettingChanged("CursorFollowDistance"); }
}

public double CursorIndicatorOpacityDefault = 0.3;
public double CursorIndicatorOpacity
{
Expand Down Expand Up @@ -694,6 +710,8 @@ public void CallPropertyChangedForAllProperties()
PropertyChanged(this, new PropertyChangedEventArgs("EnableHistoryTimeout"));
PropertyChanged(this, new PropertyChangedEventArgs("EnableWindowFade"));
PropertyChanged(this, new PropertyChangedEventArgs("EnableCursorIndicator"));
PropertyChanged(this, new PropertyChangedEventArgs("EnableCursorFollow"));
PropertyChanged(this, new PropertyChangedEventArgs("CursorFollowDistance"));
PropertyChanged(this, new PropertyChangedEventArgs("CursorIndicatorOpacity"));
PropertyChanged(this, new PropertyChangedEventArgs("CursorIndicatorSize"));
PropertyChanged(this, new PropertyChangedEventArgs("CursorIndicatorColor"));
Expand Down Expand Up @@ -827,6 +845,8 @@ public override string ToString()
EnableHistoryTimeout: {EnableHistoryTimeout}
EnableWindowFade: {EnableWindowFade}
EnableCursorIndicator: {EnableCursorIndicator}
EnableCursorFollow: {EnableCursorFollow}
CursorFollowDistance: {CursorFollowDistance}
CursorIndicatorOpacity: {CursorIndicatorOpacity}
CursorIndicatorSize: {CursorIndicatorSize}
CursorIndicatorColor: {CursorIndicatorColor}
Expand Down