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

Mouse click highlight v2 with mouse actions as keys in addition to visual circles #162

Open
wants to merge 5 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
4 changes: 4 additions & 0 deletions src/Carnac.Logic/Carnac.Logic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="Gma.System.MouseKeyHook, Version=5.4.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MouseKeyHook.5.4.0\lib\net40\Gma.System.MouseKeyHook.dll</HintPath>
</Reference>
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="PropertyChanged, Version=1.52.1.0, Culture=neutral, PublicKeyToken=ee3ee20bcf148ddd, processorArchitecture=MSIL">
Expand Down Expand Up @@ -92,6 +95,7 @@
<Compile Include="KeyMonitor\KeyDirection.cs" />
<Compile Include="KeysController.cs" />
<Compile Include="Models\Processinfo.cs" />
<Compile Include="MouseMonitor\InterceptMouse.cs" />
<Compile Include="ShortcutAccumulator.cs" />
<Compile Include="KeyPressDefinition.cs" />
<Compile Include="KeyShortcut.cs" />
Expand Down
10 changes: 8 additions & 2 deletions src/Carnac.Logic/KeyProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using Carnac.Logic.Models;
using Microsoft.Win32;
using System.Windows.Media;
using Carnac.Logic.MouseMonitor;


namespace Carnac.Logic
{
Expand Down Expand Up @@ -66,7 +68,10 @@ public IObservable<KeyPress> GetKeyStream()
winKeyPressed = false;
}, observer.OnError);

var keyStreamSubsription = interceptKeysSource.GetKeyStream()
var keyStreamSubsription = Observable.Merge(
new IObservable<InterceptKeyEventArgs>[2] {
interceptKeysSource.GetKeyStream(),
InterceptMouse.Current.GetKeyStream() })
.Select(DetectWindowsKey)
.Where(k => !IsModifierKeyPress(k) && k.KeyDirection == KeyDirection.Down)
.Select(ToCarnacKeyPress)
Expand Down Expand Up @@ -123,14 +128,15 @@ static IEnumerable<string> ToInputs(bool isLetter, bool isWinKeyPressed, Interce
var controlPressed = interceptKeyEventArgs.ControlPressed;
var altPressed = interceptKeyEventArgs.AltPressed;
var shiftPressed = interceptKeyEventArgs.ShiftPressed;
var mouseAction = InterceptMouse.MouseKeys.Contains(interceptKeyEventArgs.Key);
if (controlPressed)
yield return "Ctrl";
if (altPressed)
yield return "Alt";
if (isWinKeyPressed)
yield return "Win";

if (controlPressed || altPressed)
if (controlPressed || altPressed || mouseAction)
{
//Treat as a shortcut, don't be too smart
if (shiftPressed)
Expand Down
30 changes: 28 additions & 2 deletions src/Carnac.Logic/Models/Message.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Carnac.Logic.MouseMonitor;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
Expand Down Expand Up @@ -29,7 +30,8 @@ public Message(KeyPress key)
{
processName = key.Process.ProcessName;
processIcon = key.Process.ProcessIcon;
canBeMerged = !key.HasModifierPressed;
// allow to aggregate all key combination as to not spam if ctrl + mousewheel is used.
canBeMerged = true; // !key.HasModifierPressed;

keys = new ReadOnlyCollection<KeyPress>(new[] { key });
textCollection = new ReadOnlyCollection<string>(CreateTextSequence(key).ToArray());
Expand Down Expand Up @@ -104,14 +106,38 @@ public static Message MergeIfNeeded(Message previousMessage, Message newMessage)
return ShouldCreateNewMessage(previousMessage, newMessage)
? newMessage
: previousMessage.Merge(newMessage);
/*
* Code used when mouse and show modifiers standalone are mixed together
// replace key was after standalone modifier keypress, replace by new Message
+ if (previousMessage.keys != null && KeyProvider.IsModifierKeyPress(previousMessage.keys[0].InterceptKeyEventArgs))
+ {
+ return previousMessage.Replace(newMessage);
+ }
+ // if current is modifier and previous is a mouse action ignore modifierkeypress
+ if (previousMessage.keys != null && KeyProvider.IsModifierKeyPress(newMessage.keys[0].InterceptKeyEventArgs)
+ && InterceptMouse.MouseKeys.Contains(previousMessage.keys[0].Key))
+ {
+ return previousMessage.Replace(previousMessage);
+ }
+
+ if (ShouldCreateNewMessage(previousMessage, newMessage))
+ {
+ return newMessage;
+ }
+ return previousMessage.Merge(newMessage);
*/
}

static bool ShouldCreateNewMessage(Message previous, Message current)
{
return previous.ProcessName != current.ProcessName ||
current.LastMessage.Subtract(previous.LastMessage) > OneSecond ||
!previous.CanBeMerged ||
!current.CanBeMerged;
!current.CanBeMerged ||
// new message for different mouse keys;
((InterceptMouse.MouseKeys.Contains(current.keys[0].Key) ||
(previous.keys != null && InterceptMouse.MouseKeys.Contains(previous.keys[0].Key)))
&& !previous.keys[0].Input.SequenceEqual(current.keys[0].Input));;
}

public Message FadeOut()
Expand Down
31 changes: 31 additions & 0 deletions src/Carnac.Logic/Models/PopupSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,36 @@ public string SortDescription
public bool DetectShortcutsOnly { get; set; }
public bool ShowApplicationIcon { get; set; }
public bool SettingsConfigured { get; set; }

[DefaultValue(true)]
public bool ShowMouseClicks { get; set; }

[DefaultValue("OrangeRed")]
public string LeftClickColor { get; set; }

[DefaultValue("RoyalBlue")]
public string RightClickColor { get; set; }

[DefaultValue(1)]
public double ClickStartScale { get; set; }

[DefaultValue(4)]
public double ClickStopScale { get; set; }

[DefaultValue(3700)]
public int ClickFadeDelay { get; set; }

[DefaultValue(1)]
public double ClickStartBorder { get; set; }

[DefaultValue(0.8)]
public double ClickStartOpacity { get; set; }

[DefaultValue(2)]
public double ClickStopBorder { get; set; }

[DefaultValue(0)]
public double ClickStopOpacity { get; set; }
public string ClickColor { get; set; }
}
}
112 changes: 112 additions & 0 deletions src/Carnac.Logic/MouseMonitor/InterceptMouse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using Carnac.Logic.KeyMonitor;
using System;
using System.Diagnostics;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Windows.Forms;
using Gma.System.MouseKeyHook;
using System.Collections.Generic;

namespace Carnac.Logic.MouseMonitor
{
public class InterceptMouse : IInterceptKeys
{

public static readonly InterceptMouse Current = new InterceptMouse();
readonly IKeyboardMouseEvents m_GlobalHook = Hook.GlobalEvents();
readonly IObservable<InterceptKeyEventArgs> keyStream;
private IObserver<InterceptKeyEventArgs> observer;
private readonly KeysConverter kc = new KeysConverter();

public static readonly List<Keys> MouseKeys = new List<Keys>()
{
Keys.LButton,
Keys.MButton,
Keys.RButton,
Keys.XButton1,
Keys.XButton2,
Keys.VolumeUp,
Keys.VolumeDown
};

InterceptMouse()
{
keyStream = Observable.Create<InterceptKeyEventArgs>(observer =>
{
this.observer = observer;
m_GlobalHook.MouseClick += OnMouseClick;
m_GlobalHook.MouseDoubleClick += OnMouseDoubleClick;
m_GlobalHook.MouseWheel += HookManager_MouseWheel;
Debug.Write("Subscribed to mouse");

return Disposable.Create(() =>
{
m_GlobalHook.MouseClick -= OnMouseClick;
m_GlobalHook.MouseDoubleClick -= OnMouseDoubleClick;
m_GlobalHook.MouseWheel -= HookManager_MouseWheel;
m_GlobalHook.Dispose();
Debug.Write("Unsubscribed from mouse");
});
})
.Publish().RefCount();

}

private Keys MouseButtonsToKeys(MouseButtons button)
{
switch(button)
{
case MouseButtons.Left:
return Keys.LButton;
case MouseButtons.Middle:
return Keys.MButton;
case MouseButtons.Right:
return Keys.RButton;
case MouseButtons.XButton1:
return Keys.XButton1;
case MouseButtons.XButton2:
return Keys.XButton2;
default:
return Keys.None;
}
}

private void OnMouseClick(object sender, MouseEventArgs e)
{
observer.OnNext(new InterceptKeyEventArgs(
MouseButtonsToKeys(e.Button),
KeyDirection.Down,
Control.ModifierKeys == Keys.Alt,
Control.ModifierKeys == Keys.Control,
Control.ModifierKeys == Keys.Shift));
}

private void OnMouseDoubleClick(object sender, MouseEventArgs e)
{
observer.OnNext(new InterceptKeyEventArgs(
MouseButtonsToKeys(e.Button),
KeyDirection.Down,
Control.ModifierKeys == Keys.Alt,
Control.ModifierKeys == Keys.Control,
Control.ModifierKeys == Keys.Shift));
}

private void HookManager_MouseWheel(object sender, MouseEventArgs e)
{
// for now using VolumeDown and Up as proxy could be refactored
observer.OnNext(new InterceptKeyEventArgs(
e.Delta > 0 ? Keys.VolumeUp : Keys.VolumeDown,
KeyDirection.Down,
Control.ModifierKeys == Keys.Alt,
Control.ModifierKeys == Keys.Control,
Control.ModifierKeys == Keys.Shift));
}

public IObservable<InterceptKeyEventArgs> GetKeyStream()
{
return keyStream;
}

}
}

1 change: 1 addition & 0 deletions src/Carnac.Logic/packages.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Fody" version="1.29.4" targetFramework="net40" developmentDependency="true" />
<package id="MouseKeyHook" version="5.4.0" targetFramework="net45" />
<package id="PropertyChanged.Fody" version="1.52.1" targetFramework="net40" developmentDependency="true" />
<package id="SettingsProviderNet" version="2.1.1" targetFramework="net40" />
<package id="System.Interactive" version="3.1.1" targetFramework="net45" />
Expand Down
1 change: 1 addition & 0 deletions src/Carnac/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ protected override void OnExit(ExitEventArgs e)
{
trayIcon.Dispose();
carnac.Dispose();
keyShowView.Dispose();
ProcessUtilities.DestroyMutex();

base.OnExit(e);
Expand Down
3 changes: 3 additions & 0 deletions src/Carnac/Carnac.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@
<Reference Include="DeltaCompressionDotNet.PatchApi, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3e8888ee913ed789, processorArchitecture=MSIL">
<HintPath>..\packages\DeltaCompressionDotNet.1.0.0\lib\net45\DeltaCompressionDotNet.PatchApi.dll</HintPath>
</Reference>
<Reference Include="Gma.System.MouseKeyHook, Version=5.4.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MouseKeyHook.5.4.0\lib\net40\Gma.System.MouseKeyHook.dll</HintPath>
</Reference>
<Reference Include="ICSharpCode.SharpZipLib, Version=0.86.0.518, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\squirrel.windows.1.5.2\lib\Net45\ICSharpCode.SharpZipLib.dll</HintPath>
</Reference>
Expand Down
Loading