/trunk/misu/BindKeyForm.cs |
@@ -1,10 +1,11 @@ |
using System; |
using System.Collections.Generic; |
using System.Collections.Specialized; |
using System.Drawing; |
using System.Linq; |
using System.Windows.Forms; |
using Configuration; |
using Gma.System.MouseKeyHook; |
using misu.Properties; |
|
namespace misu |
{ |
@@ -16,12 +17,6 @@ |
|
#endregion |
|
#region Public Enums, Properties and Fields |
|
public Settings Settings { get; set; } |
|
#endregion |
|
#region Private Delegates, Events, Enums, Properties, Indexers and Fields |
|
private IKeyboardMouseEvents GlobalHook { get; } |
@@ -42,11 +37,6 @@ |
GlobalHook = globalHook; |
} |
|
public BindKeyForm(IKeyboardMouseEvents globalHook, Settings settings) : this(globalHook) |
{ |
Settings = settings; |
} |
|
#endregion |
|
#region Event Handlers |
@@ -61,14 +51,16 @@ |
e.Handled = true; |
e.SuppressKeyPress = true; |
|
var distinct = new List<Keys>(BoundKeys.Distinct()); |
Settings.Default.Keys = new StringCollection(); |
foreach (var key in BoundKeys.Distinct()) |
{ |
Settings.Default.Keys.Add(key.ToString()); |
} |
|
Settings.Binding.Keys = distinct; |
this.Execute(() => { textBox1.Text = string.Join(" ", Settings.Default.Keys.OfType<string>()); }); |
|
this.Execute(() => { textBox1.Text = Settings.Binding.ToString(); }); |
KeyBound?.Invoke(this, new KeyBoundEventArgs()); |
|
KeyBound?.Invoke(this, new KeyBoundEventArgs(Settings.Binding)); |
|
BoundKeys.Clear(); |
} |
|
@@ -95,7 +87,10 @@ |
|
private void OnFormLoad(object sender, EventArgs e) |
{ |
textBox1.Text = Settings.Binding.ToString(); |
if (Settings.Default.Keys != null) |
{ |
textBox1.Text = string.Join(" ", Settings.Default.Keys.OfType<string>()); |
} |
} |
|
#endregion |
/trunk/misu/MisuMainForm.cs |
@@ -1,23 +1,18 @@ |
using System; |
using System.ComponentModel; |
using System.Configuration; |
using System.Drawing; |
using System.Linq; |
using System.Threading.Tasks.Dataflow; |
using System.Windows.Forms; |
using AutoUpdaterDotNET; |
using Configuration; |
using Gma.System.MouseKeyHook; |
using misu.Properties; |
|
namespace misu |
{ |
public partial class MisuMainForm : Form |
{ |
#region Public Enums, Properties and Fields |
|
public Settings Settings { get; set; } |
|
#endregion |
|
#region Private Delegates, Events, Enums, Properties, Indexers and Fields |
|
private BufferBlock<KeyEventArgs> PressedKeysBufferBlock { get; } |
@@ -39,23 +34,19 @@ |
InitializeComponent(); |
AutoUpdater.Start("http://misu.grimore.org/update/update.xml"); |
|
PressedKeysBufferBlock = new BufferBlock<KeyEventArgs>(new DataflowBlockOptions {EnsureOrdered = true}); |
|
// Load the settings. |
var serialization = Serialization.Deserialize(@"Settings.xml"); |
switch (serialization) |
// Upgrade settings if required. |
if (!ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).HasFile) |
{ |
case SerializationSuccess<Settings> serializationSuccess: |
Settings = serializationSuccess.Result; |
break; |
case SerializationFailure _: |
Settings = new Settings(); |
break; |
Settings.Default.Upgrade(); |
} |
|
Settings.PropertyChanged += Settings_PropertyChanged; |
Settings.Binding.PropertyChanged += Binding_PropertyChanged; |
// Bind to settings changed event. |
Settings.Default.SettingsLoaded += Default_SettingsLoaded; |
Settings.Default.SettingsSaving += Default_SettingsSaving; |
Settings.Default.PropertyChanged += Default_PropertyChanged; |
|
PressedKeysBufferBlock = new BufferBlock<KeyEventArgs>(new DataflowBlockOptions {EnsureOrdered = true}); |
|
// Hook to global events. |
GlobalHook = Hook.GlobalEvents(); |
|
@@ -70,10 +61,10 @@ |
GlobalHook.MouseMoveExt += GlobalHook_BlockMouse; |
GlobalHook.MouseDownExt += GlobalHook_BlockMouse; |
|
if (Settings.Binding.Keys.Any()) |
if (Settings.Default.Keys != null) |
{ |
Notify("Shortcut is bound.", |
$"Press {Settings.Binding} to toggle locking."); |
$"Press {string.Join(" ", Settings.Default.Keys.OfType<string>())} to toggle locking."); |
} |
} |
|
@@ -81,11 +72,19 @@ |
|
#region Event Handlers |
|
private async void Binding_PropertyChanged(object sender, PropertyChangedEventArgs e) |
private static void Default_SettingsSaving(object sender, CancelEventArgs e) |
{ |
await Settings.Serialize(@"Settings.xml"); |
} |
|
private static void Default_PropertyChanged(object sender, PropertyChangedEventArgs e) |
{ |
Settings.Default.Save(); |
} |
|
private static void Default_SettingsLoaded(object sender, SettingsLoadedEventArgs e) |
{ |
} |
|
private void GlobalHook_BlockMouse(object sender, MouseEventExtArgs e) |
{ |
// Do not perform any operations while the binding form is visible. |
@@ -94,18 +93,12 @@ |
return; |
} |
|
if (_lock && Settings.Mouse) |
if (_lock && Settings.Default.Mouse) |
{ |
e.Handled = true; |
} |
} |
|
private async void Settings_PropertyChanged(object sender, PropertyChangedEventArgs e) |
{ |
// Serialize the settings to the configuration file on form closing. |
await Settings.Serialize(@"Settings.xml"); |
} |
|
private void GlobalHook_KeyUp(object sender, KeyEventArgs e) |
{ |
// Do not perform any operations while the binding form is visible. |
@@ -119,33 +112,35 @@ |
return; |
} |
|
if (Settings.Binding == null) |
if (Settings.Default.Keys == null) |
{ |
return; |
} |
|
if (Settings.Binding.Keys.SequenceContains(keys.Select(key => key.KeyCode))) |
if (!Settings.Default.Keys.OfType<string>().SequenceEqual(keys.Select(key => key.KeyCode.ToString()))) |
{ |
_lock = !_lock; |
return; |
} |
|
// Lock the cursor in position. |
switch (_lock) |
{ |
case true: |
Cursor.Clip = new Rectangle(Cursor.Position, new Size(1, 1)); |
break; |
default: |
Cursor.Clip = Screen.PrimaryScreen.Bounds; |
break; |
} |
_lock = !_lock; |
|
// Dismiss the current keypress to avoid typing the key. |
e.Handled = true; |
e.SuppressKeyPress = true; |
// Lock the cursor in position. |
switch (_lock) |
{ |
case true: |
Cursor.Clip = new Rectangle(Cursor.Position, new Size(1, 1)); |
break; |
default: |
Cursor.Clip = Screen.PrimaryScreen.Bounds; |
break; |
} |
|
Notify($"Mouse and Keyboard are {(_lock ? "locked" : "unlocked")}.", |
$"Press {Settings.Binding} to toggle mouse and keyboard locking."); |
} |
// Dismiss the current keypress to avoid typing the key. |
e.Handled = true; |
e.SuppressKeyPress = true; |
|
Notify($"Mouse and Keyboard are {(_lock ? "locked" : "unlocked")}.", |
$"Press {string.Join(" ", Settings.Default.Keys.OfType<string>())} to toggle mouse and keyboard locking."); |
} |
|
private async void GlobalHook_KeyDown(object sender, KeyEventArgs e) |
@@ -156,7 +151,7 @@ |
return; |
} |
|
if (_lock && Settings.Keyboard) |
if (_lock && Settings.Default.Keyboard) |
{ |
e.SuppressKeyPress = true; |
e.Handled = true; |
@@ -179,14 +174,14 @@ |
|
private void OnContextMenuLaunchOnBootChanged(object sender, EventArgs e) |
{ |
Settings.LaunchOnBoot = ((ToolStripMenuItem) sender).Checked; |
Settings.Default.LaunchOnBoot = ((ToolStripMenuItem) sender).Checked; |
|
Utilities.LaunchOnBootSet(Settings.LaunchOnBoot); |
Utilities.LaunchOnBootSet(Settings.Default.LaunchOnBoot); |
} |
|
private void OnContextMenuBindKeyClick(object sender, EventArgs e) |
{ |
BindKeyForm = new BindKeyForm(GlobalHook, Settings); |
BindKeyForm = new BindKeyForm(GlobalHook); |
BindKeyForm.KeyBound += BindKeyForm_KeyBound; |
BindKeyForm.Closing += BindKeyForm_Closing; |
BindKeyForm.Show(); |
@@ -201,11 +196,7 @@ |
private async void BindKeyForm_KeyBound(object sender, KeyBoundEventArgs e) |
{ |
Notify("Shortcut key is bound.", |
$"Press {e.Binding} to toggle between locked and unlocked keyboard and mouse."); |
|
Settings.Binding.Keys.AddRange(e.Binding.Keys); |
|
await Settings.Serialize(@"Settings.xml"); |
$"Press {string.Join(" ", Settings.Default.Keys.OfType<string>())} to toggle between locked and unlocked keyboard and mouse."); |
} |
|
private void KeyboardToolStripMenuItem_CheckedChanged(object sender, EventArgs e) |
@@ -212,7 +203,7 @@ |
{ |
var item = (ToolStripMenuItem) sender; |
|
Settings.Keyboard = item.Checked; |
Settings.Default.Keyboard = item.Checked; |
} |
|
private void MouseToolStripMenuItem_CheckedChanged(object sender, EventArgs e) |
@@ -219,17 +210,17 @@ |
{ |
var item = (ToolStripMenuItem) sender; |
|
Settings.Mouse = item.Checked; |
Settings.Default.Mouse = item.Checked; |
} |
|
private void ContextMenuStrip_Opening(object sender, CancelEventArgs e) |
{ |
// Apply the settings to the menu items. |
launchOnBootToolStripMenuItem.Checked = Settings.LaunchOnBoot; |
Utilities.LaunchOnBootSet(Settings.LaunchOnBoot); |
launchOnBootToolStripMenuItem.Checked = Settings.Default.LaunchOnBoot; |
Utilities.LaunchOnBootSet(Settings.Default.LaunchOnBoot); |
|
mouseToolStripMenuItem.Checked = Settings.Mouse; |
keyboardToolStripMenuItem.Checked = Settings.Keyboard; |
mouseToolStripMenuItem.Checked = Settings.Default.Mouse; |
keyboardToolStripMenuItem.Checked = Settings.Default.Keyboard; |
} |
|
#endregion |
/trunk/misu/MisuMainForm.resx |
@@ -289,7 +289,7 @@ |
<data name="launchOnBootToolStripMenuItem.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64"> |
<value> |
iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAYAAAD0eNT6AAAABHNCSVQICAgIfAhkiAAAAAFzUkdCAK7O |
HOkAAAAEZ0FNQQAAsY8L/GEFAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAAGXRFWHRTb2Z0d2FyZQB3d3cu |
HOkAAAAEZ0FNQQAAsY8L/GEFAAAACXBIWXMAAA7CAAAOwgEVKEqAAAAAGXRFWHRTb2Z0d2FyZQB3d3cu |
aW5rc2NhcGUub3Jnm+48GgAAV2RJREFUeF7tnQl4VOX5t3GpdrGttf7t19pWQau2mATFggRrqUkIm7tR |
kyCoLLIlYZNVIQjKIrKLgoi4K+4GUNwQRVxxYcvMZJ1MEtaQmQBZ5kx4v/ck70g4eRJmJnP2331d94WX |
rWFmMvPc7zlzlnYAAACA2dm7l/3m0CH298pKfyevV0qoqgrc4PUG7vB6/UN9Pv8Y/u+mVFVJc3y+wDL+ |
@@ -669,7 +669,7 @@ |
<data name="mouseToolStripMenuItem.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64"> |
<value> |
iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAYAAAD0eNT6AAAABHNCSVQICAgIfAhkiAAAAAFzUkdCAK7O |
HOkAAAAEZ0FNQQAAsY8L/GEFAAAACXBIWXMAAA3WAAAN1gGQb3mcAAAAGXRFWHRTb2Z0d2FyZQB3d3cu |
HOkAAAAEZ0FNQQAAsY8L/GEFAAAACXBIWXMAAA3VAAAN1QE91ljxAAAAGXRFWHRTb2Z0d2FyZQB3d3cu |
aW5rc2NhcGUub3Jnm+48GgAAQf5JREFUeF7t3Xm4ZWV15/GuFjFddrfRmIduO61VRc0DQ1EDZVUhUMxQ |
FAoiIEMxg0Axz0ZAiMwziIDMEWRQZBBQkUQEE9SgYiQmNo4oDhmM6ZiYiLvXKi/9nLz3B/e+++x37/c9 |
5/vH53l4FlXn7LP3Wr+96g7n/KeqqgAUZv23TFrXTDcLzZZmldnbvMecbM4xl5sbzd3mEfOkecZ8x/yt |
@@ -958,7 +958,7 @@ |
<data name="keyboardToolStripMenuItem.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64"> |
<value> |
iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAYAAAD0eNT6AAAABHNCSVQICAgIfAhkiAAAAAFzUkdCAK7O |
HOkAAAAEZ0FNQQAAsY8L/GEFAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAAGXRFWHRTb2Z0d2FyZQB3d3cu |
HOkAAAAEZ0FNQQAAsY8L/GEFAAAACXBIWXMAAA7CAAAOwgEVKEqAAAAAGXRFWHRTb2Z0d2FyZQB3d3cu |
aW5rc2NhcGUub3Jnm+48GgAAMJ5JREFUeF7t3QuQ1NW94HHM6+Z1s3nd5GY3qd17K28T7O5Zxe5GM3Ur |
W9nU3cUVREDh+kAG6O6RGTAqSUi48cVjhBGUGeQRByIY5amCDJa85CXIQ6NGAREJAsYgimiiifz3nJ5D |
Sf78gIGePv/z7/P9Vn3Krb1VM+f/T9XvHKb/3d2JiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiI |
@@ -1173,7 +1173,7 @@ |
<data name="settingsToolStripMenuItem.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64"> |
<value> |
iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAYAAAD0eNT6AAAABHNCSVQICAgIfAhkiAAAAAFzUkdCAK7O |
HOkAAAAEZ0FNQQAAsY8L/GEFAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAAGXRFWHRTb2Z0d2FyZQB3d3cu |
HOkAAAAEZ0FNQQAAsY8L/GEFAAAACXBIWXMAAA7CAAAOwgEVKEqAAAAAGXRFWHRTb2Z0d2FyZQB3d3cu |
aW5rc2NhcGUub3Jnm+48GgAAcNlJREFUeF7t3Qd4VNeZN3A2vW2yySab/TabXSMRp3g32ST7fXEHzVDd |
G8b03rGNC6bDqFA1IzpGxgUb25gOLhRTpBGiNxVEE5JQpWo0kpA0op3vnuEQxOiVpujOvefe+f+f5/fE |
QdLM3DLnfefOvee2QhBE7jDG/qH6jO1f6gttf6griGtbmx/3oqcgblhtQfzYuvzYhNq82Ll1+fHvevLj |
@@ -1662,7 +1662,7 @@ |
<data name="aboutToolStripMenuItem.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64"> |
<value> |
iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAYAAAD0eNT6AAAABHNCSVQICAgIfAhkiAAAAAFzUkdCAK7O |
HOkAAAAEZ0FNQQAAsY8L/GEFAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAAGXRFWHRTb2Z0d2FyZQB3d3cu |
HOkAAAAEZ0FNQQAAsY8L/GEFAAAACXBIWXMAAA7CAAAOwgEVKEqAAAAAGXRFWHRTb2Z0d2FyZQB3d3cu |
aW5rc2NhcGUub3Jnm+48GgAAYplJREFUeF7t3QeUFUXaBmAYGHLOSSQLiAEUMKCsrigGDCCsgoFVURED |
IgoqZlEEDCxGEBNBBQFRJGeQzDAgOYeZIQmS8d9U//ddxR3lRSZ0V1f3fd9znnM84zC3bldV173dXVU5 |
GIZxPomigjhHNBe3iPtEN/GqeFcME9+KyWKeWCw2iM1i3x/8R5g/0J/98ff0364X+rf0b+rf1tcYKvQ1 |
@@ -2090,7 +2090,7 @@ |
<data name="quitToolStripMenuItem.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64"> |
<value> |
iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAYAAAD0eNT6AAAABHNCSVQICAgIfAhkiAAAAAFzUkdCAK7O |
HOkAAAAEZ0FNQQAAsY8L/GEFAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAAGXRFWHRTb2Z0d2FyZQB3d3cu |
HOkAAAAEZ0FNQQAAsY8L/GEFAAAACXBIWXMAAA7CAAAOwgEVKEqAAAAAGXRFWHRTb2Z0d2FyZQB3d3cu |
aW5rc2NhcGUub3Jnm+48GgAASYVJREFUeF7t3XmcHWWd73HSnc7S2TeyB8hCFtZIIGyBECJCCFtCAgKB |
BJJOIGYBQsKmgNtEEUWvVwFRBhHE4MKMgAxcR7kXx1HkXhFBHBXBGQVUBJRFCMlzq47nMNB+E7qfXz1P |
VZ3z+b5e73+K0L9T51dVv6e7T1ftQAghhJBSpkti98TqxG2JnyReSjyXeDTxpcRxie4JQgghhJQ4YxNt |
/trunk/misu/Properties/Settings.Designer.cs |
@@ -0,0 +1,73 @@ |
//------------------------------------------------------------------------------ |
// <auto-generated> |
// This code was generated by a tool. |
// Runtime Version:4.0.30319.42000 |
// |
// Changes to this file may cause incorrect behavior and will be lost if |
// the code is regenerated. |
// </auto-generated> |
//------------------------------------------------------------------------------ |
|
namespace misu.Properties { |
|
|
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] |
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "16.8.1.0")] |
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { |
|
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); |
|
public static Settings Default { |
get { |
return defaultInstance; |
} |
} |
|
[global::System.Configuration.UserScopedSettingAttribute()] |
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()] |
[global::System.Configuration.DefaultSettingValueAttribute("False")] |
public bool LaunchOnBoot { |
get { |
return ((bool)(this["LaunchOnBoot"])); |
} |
set { |
this["LaunchOnBoot"] = value; |
} |
} |
|
[global::System.Configuration.UserScopedSettingAttribute()] |
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()] |
[global::System.Configuration.DefaultSettingValueAttribute("True")] |
public bool Keyboard { |
get { |
return ((bool)(this["Keyboard"])); |
} |
set { |
this["Keyboard"] = value; |
} |
} |
|
[global::System.Configuration.UserScopedSettingAttribute()] |
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()] |
[global::System.Configuration.DefaultSettingValueAttribute("False")] |
public bool Mouse { |
get { |
return ((bool)(this["Mouse"])); |
} |
set { |
this["Mouse"] = value; |
} |
} |
|
[global::System.Configuration.UserScopedSettingAttribute()] |
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()] |
public global::System.Collections.Specialized.StringCollection Keys { |
get { |
return ((global::System.Collections.Specialized.StringCollection)(this["Keys"])); |
} |
set { |
this["Keys"] = value; |
} |
} |
} |
} |
/trunk/misu/misu.csproj |
@@ -46,6 +46,7 @@ |
<HintPath>..\packages\MouseKeyHook.5.6.0\lib\net40\Gma.System.MouseKeyHook.dll</HintPath> |
</Reference> |
<Reference Include="System" /> |
<Reference Include="System.Configuration" /> |
<Reference Include="System.Core" /> |
<Reference Include="System.Runtime.Serialization" /> |
<Reference Include="System.Threading.Tasks.Dataflow, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> |
@@ -92,6 +93,11 @@ |
<DesignTime>True</DesignTime> |
<DependentUpon>Resources.resx</DependentUpon> |
</Compile> |
<Compile Include="Properties\Settings.Designer.cs"> |
<AutoGen>True</AutoGen> |
<DesignTimeSharedInput>True</DesignTimeSharedInput> |
<DependentUpon>Settings.settings</DependentUpon> |
</Compile> |
<Compile Include="Utilities.cs" /> |
<EmbeddedResource Include="AboutForm.resx"> |
<DependentUpon>AboutForm.cs</DependentUpon> |
@@ -109,6 +115,10 @@ |
<None Include="img\misu.psd" /> |
<None Include="packages.config" /> |
<None Include="Properties\DataSources\Form1.datasource" /> |
<None Include="Properties\Settings.settings"> |
<Generator>SettingsSingleFileGenerator</Generator> |
<LastGenOutput>Settings.Designer.cs</LastGenOutput> |
</None> |
</ItemGroup> |
<ItemGroup> |
<None Include="App.config" /> |
@@ -127,11 +137,5 @@ |
<Content Include="misu.ico" /> |
</ItemGroup> |
<ItemGroup /> |
<ItemGroup> |
<ProjectReference Include="..\Configuration\Configuration.csproj"> |
<Project>{c2434c11-3aab-43b5-ad54-a0655b8b8fb9}</Project> |
<Name>Configuration</Name> |
</ProjectReference> |
</ItemGroup> |
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> |
</Project> |