Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
teetow committed Oct 9, 2018
0 parents commit 30f51d8
Show file tree
Hide file tree
Showing 22 changed files with 3,504 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
################################################################################
# This .gitignore file was automatically created by Microsoft(R) Visual Studio.
################################################################################

/.vs/screenzap/v15
/packages/Nito.AsyncEx.3.0.1
/screenzap
25 changes: 25 additions & 0 deletions screenzap.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26730.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "screenzap", "screenzap\screenzap.csproj", "{834014AB-8686-4A31-95F7-6FBB47F45687}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{834014AB-8686-4A31-95F7-6FBB47F45687}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{834014AB-8686-4A31-95F7-6FBB47F45687}.Debug|Any CPU.Build.0 = Debug|Any CPU
{834014AB-8686-4A31-95F7-6FBB47F45687}.Release|Any CPU.ActiveCfg = Release|Any CPU
{834014AB-8686-4A31-95F7-6FBB47F45687}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {2AD6C31C-F876-4482-8FB6-7DDD1A3D3F0F}
EndGlobalSection
EndGlobal
18 changes: 18 additions & 0 deletions screenzap/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="screenzap.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<userSettings>
<screenzap.Properties.Settings>
<setting name="currentCombo" serializeAs="String">
<value>Ctrl+Alt+Shift+4</value>
</setting>
</screenzap.Properties.Settings>
</userSettings>
</configuration>
48 changes: 48 additions & 0 deletions screenzap/Autorun.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Microsoft.Win32;

/// <summary>
/// Utility.
/// </summary>
public class Util
{
private const string RUN_LOCATION = @"Software\Microsoft\Windows\CurrentVersion\Run";

/// <summary>
/// Sets the autostart value for the assembly.
/// </summary>
/// <param name="keyName">Registry Key Name</param>
/// <param name="assemblyLocation">Assembly location (e.g. Assembly.GetExecutingAssembly().Location)</param>
public static void SetAutoStart(string keyName, string assemblyLocation)
{
RegistryKey key = Registry.CurrentUser.CreateSubKey(RUN_LOCATION);
key.SetValue(keyName, assemblyLocation);
}

/// <summary>
/// Returns whether auto start is enabled.
/// </summary>
/// <param name="keyName">Registry Key Name</param>
/// <param name="assemblyLocation">Assembly location (e.g. Assembly.GetExecutingAssembly().Location)</param>
public static bool IsAutoStartEnabled(string keyName, string assemblyLocation)
{
RegistryKey key = Registry.CurrentUser.OpenSubKey(RUN_LOCATION);
if (key == null)
return false;

string value = (string)key.GetValue(keyName);
if (value == null)
return false;

return (value == assemblyLocation);
}

/// <summary>
/// Unsets the autostart value for the assembly.
/// </summary>
/// <param name="keyName">Registry Key Name</param>
public static void UnSetAutoStart(string keyName)
{
RegistryKey key = Registry.CurrentUser.CreateSubKey(RUN_LOCATION);
key.DeleteValue(keyName);
}
}
206 changes: 206 additions & 0 deletions screenzap/KeyboardHook.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

public sealed class KeyboardHook : IDisposable
{
// Registers a hot key with Windows.
[DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
// Unregisters the hot key with Windows.
[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);

/// <summary>
/// Represents the window that is used internally to get the messages.
/// </summary>
private class Window : NativeWindow, IDisposable
{
private static int WM_HOTKEY = 0x0312;

public Window()
{
// create the handle for the window.
this.CreateHandle(new CreateParams());
}

/// <summary>
/// Overridden to get the notifications.
/// </summary>
/// <param name="m"></param>
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);

// check if we got a hot key pressed.
if (m.Msg == WM_HOTKEY)
{
// get the keys.
Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF);
ModifierKeys modifier = (ModifierKeys)((int)m.LParam & 0xFFFF);

// invoke the event to notify the parent.
if (KeyPressed != null)
KeyPressed(this, new KeyPressedEventArgs(modifier, key));
}
}

public event EventHandler<KeyPressedEventArgs> KeyPressed;

#region IDisposable Members

public void Dispose()
{
this.DestroyHandle();
}

#endregion
}

private Window _window = new Window();
private int _currentId;

public KeyboardHook()
{
// register the event of the inner native window.
_window.KeyPressed += delegate (object sender, KeyPressedEventArgs args)
{
if (KeyPressed != null)
KeyPressed(this, args);
};
}

/// <summary>
/// Registers a hot key in the system.
/// </summary>
/// <param name="modifier">The modifiers that are associated with the hot key.</param>
/// <param name="key">The key itself that is associated with the hot key.</param>
public void RegisterHotKey(ModifierKeys modifier, Keys key)
{
// increment the counter.
_currentId = _currentId + 1;

// register the hot key.
if (!RegisterHotKey(_window.Handle, _currentId, (uint)modifier, (uint)key))
throw new InvalidOperationException("Couldn’t register the hot key.");
}

public void UnregisterHotkey(int id)
{
if (!UnregisterHotKey(_window.Handle, id))
{
throw new InvalidOperationException("Couldn't unregister the hot key.");
}
else
{
_currentId--;
}
}

/// <summary>
/// A hot key has been pressed.
/// </summary>
public event EventHandler<KeyPressedEventArgs> KeyPressed;

#region IDisposable Members

public void Dispose()
{
// unregister all the registered hot keys.
for (int i = _currentId; i > 0; i--)
{
UnregisterHotKey(_window.Handle, i);
}

// dispose the inner native window.
_window.Dispose();
}

#endregion
}

/// <summary>
/// Event Args for the event that is fired after the hot key has been pressed.
/// </summary>
public class KeyPressedEventArgs : EventArgs
{
private ModifierKeys _modifier;
private Keys _key;

internal KeyPressedEventArgs(ModifierKeys modifier, Keys key)
{
_modifier = modifier;
_key = key;
}

public ModifierKeys Modifier
{
get { return _modifier; }
}

public Keys Key
{
get { return _key; }
}
}

/// <summary>
/// The enumeration of possible modifiers.
/// </summary>
[Flags]
public enum ModifierKeys : uint
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Win = 8
}

public struct KeyCombo
{
public Keys Modifiers;
public Keys Key;
private static KeysConverter converter = new KeysConverter();

public KeyCombo(object comboObj) : this()
{
this.Modifiers = (Keys)comboObj & Keys.Modifiers;
this.Key = (Keys)comboObj & ~Keys.Modifiers;
}

public KeyCombo(Keys modifiers, Keys key)
{
this.Modifiers = modifiers;
this.Key = key;
}

public KeyCombo(string keyCombo)
{
keyCombo = keyCombo.Replace("-", "+");
var comboObj = converter.ConvertFromString(keyCombo);
this = new KeyCombo(comboObj);
}

public override string ToString()
{
return converter.ConvertToString(this.Modifiers | this.Key);
}

internal ModifierKeys getModifierKeys()
{
ModifierKeys keys = new ModifierKeys();

if ((this.Modifiers & Keys.Control) == Keys.Control)
keys |= ModifierKeys.Control;
if ((this.Modifiers & Keys.Alt) == Keys.Alt)
keys |= ModifierKeys.Alt;
if ((this.Modifiers & Keys.Shift) == Keys.Shift)
keys |= ModifierKeys.Shift;
if ((this.Modifiers & Keys.LWin) == Keys.LWin)
keys |= ModifierKeys.Win;
if ((this.Modifiers & Keys.RWin) == Keys.RWin)
keys |= ModifierKeys.Win;
return keys;
}
}
Loading

0 comments on commit 30f51d8

Please sign in to comment.