Skip to content

Commit

Permalink
change key capture method
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-panteleev committed Aug 24, 2017
1 parent 7a36efe commit f6b6f32
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 5 deletions.
57 changes: 57 additions & 0 deletions Controls/SysKeyInputBox.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input;
using WinHook.Utils;
using TextBox = System.Windows.Controls.TextBox;

namespace WinHook.Controls
{

public class SysKeyInputBox : TextBox
{
static SysKeyInputBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(SysKeyInputBox), new FrameworkPropertyMetadata(typeof(SysKeyInputBox)));
}

public SysKeyInputBox()
{
KeyHook.KeyCapture += KeyHookOnKeyCapture;
}

private void KeyHookOnKeyCapture(object sender, KeyCaptureEventArgs e)
{
if (!IsFocused) return;

Keys = e.Key;
e.Handled = true;

CommandManager.InvalidateRequerySuggested();

}

public static readonly DependencyProperty KeysProperty = DependencyProperty.Register(
"Keys", typeof(Keys?), typeof(SysKeyInputBox), new FrameworkPropertyMetadata(new Keys(), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, FillText)
);

private static void FillText(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var invoker = d as SysKeyInputBox;

var newValue = e.NewValue == null ? string.Empty : e.NewValue.ToString();
invoker.Text = newValue;
}

public Keys Keys
{
get
{
return (Keys)GetValue(KeysProperty);
}
set
{
SetValue(KeysProperty, value);
}
}
}
}
1 change: 0 additions & 1 deletion Models/ObservableObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ protected void RaisePropertyChanged([CallerMemberName] string propertyName = "")

var eventArgs = new PropertyChangedEventArgs(propertyName);
PropertyChanged(this, eventArgs);
Debug.WriteLine(propertyName);
EventProxy<PropertyChangedEventArgs>.CaptureEvent(this, eventArgs);
}
}
Expand Down
1 change: 1 addition & 0 deletions Themes/Generic.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
xmlns:Controls="clr-namespace:WinHook.Controls">

<Style TargetType="{x:Type Controls:KeyInputBox}" BasedOn="{StaticResource {x:Type TextBox}}"/>
<Style TargetType="{x:Type Controls:SysKeyInputBox}" BasedOn="{StaticResource {x:Type TextBox}}"/>
</ResourceDictionary>
1 change: 1 addition & 0 deletions Utils/KeyHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ private struct Kbdllhookstruct

private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);

public delegate void KeyCaptyreEventHandler(object sender, KeyCaptureEventArgs e);
public static event KeyCaptyreEventHandler KeyCapture;

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
Expand Down
6 changes: 3 additions & 3 deletions Views/WinHookKeyBlockerControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
<!--<ListBox Margin="5" MinWidth="200" SelectionChanged="Selector_OnSelectionChanged" ScrollViewer.ScrollChanged="ScrollViewer_OnScrollChanged"></ListBox>-->
<ListBox Name="BlockedKeysList" Margin="5" MinWidth="200" ItemsSource="{Binding Config.KeyBlockConfig.BlockedKeys}"></ListBox>
<StackPanel>
<controls:KeyInputBox x:Name="BlockedKeyTextBox" Margin="5" ShorcutMode="False" Keys="{Binding Config.KeyBlockConfig.TmpAddKeys}" />
<controls:SysKeyInputBox x:Name="BlockedKeyTextBox" Margin="5" Keys="{Binding Config.KeyBlockConfig.TmpAddKeys}" />
<!--<TextBox Margin="5" PreviewKeyDown="UIElement_OnPreviewKeyDown"></TextBox>-->
<Button Margin="5" Name="AddBtn" Command="{Binding Config.KeyBlockConfig.AddKeyCommand}" CommandParameter="{Binding ElementName=BlockedKeyTextBox, Path=Keys}">Add</Button>
<Button Margin="5" Name="RemoveBtn" Command="{Binding Config.KeyBlockConfig.RemoveKeyCommand}" CommandParameter="{Binding ElementName=BlockedKeysList, Path=SelectedItem}">Remove</Button>
<Button Margin="5" Name="AddBtn" Command="{Binding Config.KeyBlockConfig.AddKeyCommand}" CommandParameter="{Binding ElementName=BlockedKeyTextBox, Path=Keys, Mode=OneWay}">Add</Button>
<Button Margin="5" Name="RemoveBtn" Command="{Binding Config.KeyBlockConfig.RemoveKeyCommand}" CommandParameter="{Binding ElementName=BlockedKeysList, Path=SelectedItem, Mode=OneWay}">Remove</Button>
</StackPanel>
</DockPanel>
</UserControl>
3 changes: 2 additions & 1 deletion WinHook.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<WarningLevel>4</WarningLevel>
<IsWebBootstrapper>false</IsWebBootstrapper>
<PublishUrl>publish\</PublishUrl>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
Expand All @@ -25,7 +26,6 @@
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup>
Expand Down Expand Up @@ -76,6 +76,7 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="Controls\SysKeyInputBox.cs" />
<Compile Include="Utils\DropLast.cs" />
<Compile Include="Models\EventProxy.cs" />
<Compile Include="Utils\HotKeyManager.cs" />
Expand Down

0 comments on commit f6b6f32

Please sign in to comment.