Skip to content

Commit

Permalink
HotKey support
Browse files Browse the repository at this point in the history
  • Loading branch information
TransposonY committed Jan 12, 2017
1 parent 9dd21c2 commit d881fb0
Show file tree
Hide file tree
Showing 18 changed files with 710 additions and 78 deletions.
20 changes: 14 additions & 6 deletions GestureSign.Common/Applications/ApplicationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using GestureSign.Common.Input;
using GestureSign.Common.InterProcessCommunication;
using ManagedWinapi.Windows;
using Action = GestureSign.Applications.Action;

namespace GestureSign.Common.Applications
{
Expand Down Expand Up @@ -119,12 +118,14 @@ protected void TouchCapture_CaptureStarted(object sender, PointsCapturedEventArg
else
limitNumberFlag |= e.Points.Count < userApplication.LimitNumberOfFingers;
}

IgnoredApplication ignoredApplication = app as IgnoredApplication;
if (ignoredApplication != null && ignoredApplication.IsEnabled)
else
{
e.Cancel = true;
return;
IgnoredApplication ignoredApplication = app as IgnoredApplication;
if (ignoredApplication != null && ignoredApplication.IsEnabled)
{
e.Cancel = true;
return;
}
}
}
e.Cancel = limitNumberFlag ?? e.Points.Count == 1;
Expand Down Expand Up @@ -363,6 +364,13 @@ public IApplication[] FindMatchApplications<TApplication>(MatchUsing matchUsing,
excludedApplication != a.Name).ToArray();
}

public SystemWindow GetForegroundApplications()
{
CaptureWindow = SystemWindow.ForegroundWindow;
RecognizedApplication = GetApplicationFromWindow(CaptureWindow);
return CaptureWindow;
}

#endregion

#region Private Methods
Expand Down
10 changes: 10 additions & 0 deletions GestureSign.Common/Gestures/Gesture.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Runtime.Serialization;
using ManagedWinapi;

namespace GestureSign.Common.Gestures
{
Expand Down Expand Up @@ -27,6 +28,15 @@ public Gesture(string name, PointPattern[] pointPatterns)
[DataMember]
public PointPattern[] PointPatterns { get; set; }

[DataMember]
public Hotkey Hotkey { get; set; }

public bool Equals(Gesture other)
{
if (other == null) return false;
return Name != null && Name.Equals(other.Name) && Hotkey != null && Hotkey.Equals(other.Hotkey);
}

#endregion
}
}
25 changes: 15 additions & 10 deletions GestureSign.Common/Plugins/PluginManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,27 @@ protected PluginManager()
protected void TouchCapture_GestureRecognized(object sender, RecognitionEventArgs e)
{
var touchCapture = (ITouchCapture)sender;
ExecuteAction(touchCapture.Mode, e.GestureName, e.ContactIdentifiers, e.FirstCapturedPoints, e.Points);
}

#endregion

#region Public Methods

public void ExecuteAction(CaptureMode mode, string gestureName, List<int> contactIdentifiers, List<Point> firstCapturedPoints, List<List<Point>> points)
{
// Exit if we're teaching
if (touchCapture.Mode == CaptureMode.Training)
if (mode == CaptureMode.Training)
return;

// Get action to be executed
IEnumerable<IAction> executableActions = ApplicationManager.Instance.GetRecognizedDefinedAction(e.GestureName);
IEnumerable<IAction> executableActions = ApplicationManager.Instance.GetRecognizedDefinedAction(gestureName);
foreach (IAction executableAction in executableActions)
{
// Exit if there is no action configured
if (executableAction == null || !executableAction.IsEnabled ||
(touchCapture.Mode == CaptureMode.UserDisabled &&
(mode == CaptureMode.UserDisabled &&
!"GestureSign.CorePlugins.ToggleDisableGestures".Equals(executableAction.PluginClass)) ||
!Compute(executableAction.Condition, e.Points, e.ContactIdentifiers))
!Compute(executableAction.Condition, points, contactIdentifiers))
continue;

// Locate the plugin associated with this action
Expand All @@ -71,13 +79,10 @@ protected void TouchCapture_GestureRecognized(object sender, RecognitionEventArg
// Load action settings into plugin
pluginInfo.Plugin.Deserialize(executableAction.ActionSettings);
// Execute plugin process
pluginInfo.Plugin.Gestured(new PointInfo(e.FirstCapturedPoints, e.Points));
pluginInfo.Plugin.Gestured(new PointInfo(firstCapturedPoints, points));
}
}

#endregion

#region Public Methods
}

public bool LoadPlugins(IHostControl host)
{
Expand Down
47 changes: 33 additions & 14 deletions GestureSign.ControlPanel/Dialogs/GestureDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using ManagedWinapi;

namespace GestureSign.ControlPanel.Dialogs
{
Expand All @@ -26,11 +28,12 @@ public GestureDefinition()
public GestureDefinition(IGesture gesture)
: this()
{
_oldGesture = (Gesture)gesture;
_currentPointPatterns = gesture.PointPatterns;
GestureManager.Instance.GestureName = _oldGestureName = gesture.Name;
GestureManager.Instance.GestureName = gesture.Name;
}

private string _oldGestureName;
private Gesture _oldGesture;
private string _similarGestureName;
private PointPattern[] _currentPointPatterns;
private Color _color;
Expand Down Expand Up @@ -70,16 +73,18 @@ private void Window_Loaded(object sender, RoutedEventArgs e)
if (_currentPointPatterns != null)
imgGestureThumbnail.Source = GestureImage.CreateImage(_currentPointPatterns, new Size(65, 65), _color);

if (_oldGestureName == null)
if (_oldGesture == null)
{
NamedPipe.SendMessageAsync("StartTeaching", "GestureSignDaemon");
Title = LocalizationProvider.Instance.GetTextValue("GestureDefinition.Title");
}
else
{
Title = LocalizationProvider.Instance.GetTextValue("GestureDefinition.Rename");
txtGestureName.Text = _oldGestureName; //this.txtGestureName.Text

txtGestureName.Text = _oldGesture.Name; //this.txtGestureName.Text
var hotkey = ((Gesture)_oldGesture).Hotkey;
if (hotkey != null)
HotKeyTextBox.HotKey = new HotKey(KeyInterop.KeyFromVirtualKey(hotkey.KeyCode), (ModifierKeys)hotkey.ModifierKeys);
DrawGestureTextBlock.Visibility = ResetButton.Visibility = StackUpGestureButton.Visibility = Visibility.Collapsed;

txtGestureName.Focus();
Expand All @@ -91,7 +96,7 @@ private void Window_Loaded(object sender, RoutedEventArgs e)
private async void Window_Closing(object sender, CancelEventArgs e)
{
//Non-renaming mode
if (_oldGestureName == null)
if (_oldGesture == null)
await NamedPipe.SendMessageAsync("StopTraining", "GestureSignDaemon");
}

Expand All @@ -114,7 +119,7 @@ private void cmdCancel_Click(object sender, RoutedEventArgs e)
private void txtGestureName_TextChanged(object sender, TextChangedEventArgs e)
{
string newGestureName = txtGestureName.Text.Trim();
if (_oldGestureName == newGestureName || SimilarGestureName == newGestureName) return;
if (_oldGesture?.Name == newGestureName || SimilarGestureName == newGestureName) return;

txtGestureName.GetBindingExpression(TextBox.TextProperty).UpdateSource();
}
Expand Down Expand Up @@ -178,16 +183,30 @@ private bool SaveGesture()
if (_currentPointPatterns == null || _currentPointPatterns.Length == 0)
return false;

//Rename mode
if (_oldGestureName != null)
var newGesture = new Gesture()
{
Name = newGestureName,
PointPatterns = _currentPointPatterns,
Hotkey = HotKeyTextBox.HotKey != null ?
new Hotkey()
{
KeyCode = KeyInterop.VirtualKeyFromKey(HotKeyTextBox.HotKey.Key),
ModifierKeys = (int)HotKeyTextBox.HotKey.ModifierKeys
} : null
};

if (_oldGesture != null)
{
if (_oldGestureName.Equals(newGestureName)) return true;
if (GestureManager.Instance.GestureExists(newGestureName))
//Edit gesture
if (_oldGesture.Equals(newGesture)) return true;
if (!_oldGesture.Name.Equals(newGestureName) && GestureManager.Instance.GestureExists(newGestureName))
{
txtGestureName.GetBindingExpression(TextBox.TextProperty).UpdateSource();
return false;
}
GestureManager.Instance.RenameGesture(_oldGestureName, newGestureName);
GestureManager.Instance.RenameGesture(_oldGesture.Name, newGestureName);
GestureManager.Instance.DeleteGesture(newGestureName);
GestureManager.Instance.AddGesture(newGesture);
}
else
{
Expand All @@ -199,7 +218,7 @@ private bool SaveGesture()
return false;
}
// Add new gesture to gesture manager
GestureManager.Instance.AddGesture(new Gesture(newGestureName, _currentPointPatterns));
GestureManager.Instance.AddGesture(newGesture);
}
else
{
Expand All @@ -214,7 +233,7 @@ private bool SaveGesture()

GestureManager.Instance.DeleteGesture(newGestureName);
// Add new gesture to gesture manager
GestureManager.Instance.AddGesture(new Gesture(newGestureName, _currentPointPatterns));
GestureManager.Instance.AddGesture(newGesture);
}
}
GestureManager.Instance.GestureName = newGestureName;
Expand Down
110 changes: 65 additions & 45 deletions GestureSign.ControlPanel/Dialogs/GestureDefinition.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,51 +21,71 @@
<controls:MetroWindow.Resources>
</controls:MetroWindow.Resources>
<StackPanel>
<TextBlock x:Name="DrawGestureTextBlock"
FontSize="14"
FontWeight="Bold"
Margin="12,10,12,0"
Text="{localization:LocalisedText GestureDefinition.DrawGesture}"
HorizontalAlignment="Center" />
<Image x:Name="imgGestureThumbnail"
Height="65"
Margin="0,10,0,0"
StretchDirection="DownOnly" />
<Button x:Name="ResetButton"
Content="{localization:LocalisedText GestureDefinition.Reset}"
Style="{DynamicResource SquareButtonStyle}"
Width="100"
HorizontalAlignment="Left"
BorderThickness="1"
BorderBrush="{StaticResource HighlightBrush}"
Margin="20,5,0,0"
Click="ResetButton_Click"
controls:ControlsHelper.ContentCharacterCasing="Normal"
IsEnabled="False"
Focusable="False" />
<Button x:Name="StackUpGestureButton"
Content="{localization:LocalisedText GestureDefinition.StackUpGesture}"
Style="{DynamicResource SquareButtonStyle}"
Width="100"
HorizontalAlignment="Right"
BorderThickness="1"
BorderBrush="{StaticResource HighlightBrush}"
Margin="0,-28,20,0"
Click="StackUpGestureButton_Click"
controls:ControlsHelper.ContentCharacterCasing="Normal"
IsEnabled="False"
Focusable="False" />
<TextBlock x:Name="ExistingTextBlock"
TextWrapping="Wrap"
FontSize="14"
Margin="12,0"
Text="{localization:LocalisedText GestureDefinition.ExistingGesture}"
Visibility="Collapsed" />
<Image x:Name="ExistingGestureImage"
Height="65"
StretchDirection="DownOnly"
Margin="5"
Visibility="Collapsed" />
<TabControl>
<TabItem controls:ControlsHelper.HeaderFontSize="18"
Header="{localization:LocalisedText Gesture.Header}">
<StackPanel>
<TextBlock x:Name="DrawGestureTextBlock"
FontSize="14"
FontWeight="Bold"
Margin="12,10,12,0"
Text="{localization:LocalisedText GestureDefinition.DrawGesture}"
HorizontalAlignment="Center" />
<Image x:Name="imgGestureThumbnail"
Height="65"
Margin="0,10,0,0"
StretchDirection="DownOnly" />
<Button x:Name="ResetButton"
Content="{localization:LocalisedText GestureDefinition.Reset}"
Style="{DynamicResource SquareButtonStyle}"
Width="100"
HorizontalAlignment="Left"
BorderThickness="1"
BorderBrush="{StaticResource HighlightBrush}"
Margin="20,5,0,0"
Click="ResetButton_Click"
controls:ControlsHelper.ContentCharacterCasing="Normal"
IsEnabled="False"
Focusable="False" />
<Button x:Name="StackUpGestureButton"
Content="{localization:LocalisedText GestureDefinition.StackUpGesture}"
Style="{DynamicResource SquareButtonStyle}"
Width="100"
HorizontalAlignment="Right"
BorderThickness="1"
BorderBrush="{StaticResource HighlightBrush}"
Margin="0,-28,20,0"
Click="StackUpGestureButton_Click"
controls:ControlsHelper.ContentCharacterCasing="Normal"
IsEnabled="False"
Focusable="False" />
<TextBlock x:Name="ExistingTextBlock"
TextWrapping="Wrap"
FontSize="14"
Margin="12,0"
Text="{localization:LocalisedText GestureDefinition.ExistingGesture}"
Visibility="Collapsed" />
<Image x:Name="ExistingGestureImage"
Height="65"
StretchDirection="DownOnly"
Margin="5"
Visibility="Collapsed" />
</StackPanel>
</TabItem>
<TabItem Header="{localization:LocalisedText GestureDefinition.HotKey}"
controls:ControlsHelper.HeaderFontSize="18">
<Canvas Height="50">
<controls:HotKeyBox x:Name="HotKeyTextBox"
Width="313"
AreModifierKeysRequired="True"
Watermark="{localization:LocalisedText GestureDefinition.HotKeyWatermark}"
Canvas.Left="18"
Canvas.Top="15"
FontSize="14">
</controls:HotKeyBox>
</Canvas>
</TabItem>
</TabControl>
<TextBox x:Name="txtGestureName"
controls:TextBoxHelper.Watermark="{localization:LocalisedText GestureDefinition.GestureNameWatermark}"
TextWrapping="Wrap"
Expand Down
2 changes: 2 additions & 0 deletions GestureSign.ControlPanel/Languages/ControlPanel/en.xml
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ Or click the button below to select the running applications.</GetMatchStringTip
<StackUpGesture>Stack Up</StackUpGesture>
<DrawGesture>Please draw a gesture</DrawGesture>
<Reset>Reset</Reset>
<HotKey>HotKey</HotKey>
<HotKeyWatermark>Enter hot key</HotKeyWatermark>
<Messages>
<GestureExists>The Gesture Name "{0}" already exists, please provide a different Gesture Name</GestureExists>
</Messages>
Expand Down
2 changes: 2 additions & 0 deletions GestureSign.ControlPanel/Languages/ControlPanel/zh.xml
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ http://transposony.coding.me/GestureSign/
<StackUpGesture>叠加手势</StackUpGesture>
<DrawGesture>请画出手势</DrawGesture>
<Reset>重置</Reset>
<HotKey>快捷键</HotKey>
<HotKeyWatermark>可为该手势指定一个快捷键</HotKeyWatermark>
<Messages>
<GestureExists>输入的手势名称{0}已存在,请重新输入一个手势名称</GestureExists>
</Messages>
Expand Down
5 changes: 5 additions & 0 deletions GestureSign.Daemon/GestureSign.Daemon.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@
<SubType>Form</SubType>
</Compile>
<Compile Include="TrayManager.cs" />
<Compile Include="Triggers\GestureNameEventArgs.cs" />
<Compile Include="Triggers\HotKeyManager.cs" />
<Compile Include="Triggers\Trigger.cs" />
<Compile Include="Triggers\TriggerManager.cs" />
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<SubType>Designer</SubType>
Expand Down Expand Up @@ -162,6 +166,7 @@
<None Include="Resources\add.ico" />
<None Include="Resources\stop.ico" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>
Expand Down
Loading

0 comments on commit d881fb0

Please sign in to comment.