Skip to content

Commit

Permalink
Custom hotkeys
Browse files Browse the repository at this point in the history
  • Loading branch information
Cvolton committed Aug 3, 2021
1 parent 2b8f294 commit c7dfb02
Show file tree
Hide file tree
Showing 10 changed files with 1,034 additions and 900 deletions.
7 changes: 7 additions & 0 deletions AeroShot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@
<PropertyGroup>
<ApplicationIcon>icon.ico</ApplicationIcon>
</PropertyGroup>
<PropertyGroup>
<ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.VisualBasic" />
<Reference Include="PresentationFramework" />
<Reference Include="System" />
<Reference Include="System.Data" />
Expand All @@ -65,6 +69,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="DIBConverter.cs" />
<Compile Include="KeyHelpers.cs" />
<Compile Include="Hotkeys.cs">
<SubType>Form</SubType>
</Compile>
Expand All @@ -81,6 +86,7 @@
<SubType>Form</SubType>
</Compile>
<Compile Include="UnsafeBitmap.cs" />
<Compile Include="VersionHelpers.cs" />
<Compile Include="WindowsAPI.cs" />
</ItemGroup>
<ItemGroup>
Expand All @@ -94,6 +100,7 @@
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="app.manifest" />
<None Include="README.md" />
</ItemGroup>
<ItemGroup>
Expand Down
5 changes: 3 additions & 2 deletions Hotkeys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ public class Hotkeys : Form

public Hotkeys()
{
_settings = new Settings();
_windowId = new[] { GetHashCode(), GetHashCode() ^ 327 };
WindowsApi.RegisterHotKey(Handle, _windowId[0], MOD_ALT, (int)Keys.PrintScreen);
WindowsApi.RegisterHotKey(Handle, _windowId[1], MOD_ALT | MOD_CONTROL, (int)Keys.PrintScreen);
WindowsApi.RegisterHotKey(Handle, _windowId[0], _settings.hotkeyModifier, _settings.hotkeyKey);
//WindowsApi.RegisterHotKey(Handle, _windowId[1], MOD_ALT | MOD_CONTROL, (int)Keys.PrintScreen);

this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.FormClose);
}
Expand Down
23 changes: 23 additions & 0 deletions KeyHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace AeroShot
{
static class KeyHelpers
{
//TODO: localized key names
public static string[] modifiers = { "", "Alt", "Ctrl", "Ctrl + Alt", "Shift", "Alt + Shift", "Ctrl + Shift", "Ctrl + Alt + Shift" };
//TODO: is there no better way to implement this?
public static Dictionary<Keys, int> modifierCodes = new Dictionary<Keys, int> {
{ Keys.Alt, 1 },
{ Keys.Control, 2 },
{ Keys.Alt ^ Keys.Control, 3 },
{ Keys.Shift, 4 },
{ Keys.Alt ^ Keys.Shift, 5 },
{ Keys.Shift ^ Keys.Control, 6 },
{ Keys.Alt ^ Keys.Shift ^ Keys.Control, 7}
};
}
}
1,575 changes: 786 additions & 789 deletions Main.Designer.cs

Large diffs are not rendered by default.

61 changes: 46 additions & 15 deletions Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
Expand All @@ -29,6 +30,10 @@ public sealed partial class MainForm : Form
private const uint SPI_GETFONTSMOOTHING = 0x004A;
private const uint SPI_GETFONTSMOOTHINGTYPE = 0x200A;

private int hotkeyKey;
private int hotkeyModifier;
private bool needsRestart = false;

private readonly RegistryKey _registryKey;
Settings _settings = new Settings();

Expand Down Expand Up @@ -57,17 +62,6 @@ public MainForm()
clearTypeCheckbox.Checked = _settings.clearTypeCheckbox;
shadowCheckbox.Checked = _settings.shadowCheckbox;

// July 9, 2021 starfrost: disable if not true
if (!Settings.IsWindowsVista())
{
optimizeVistaCheckbox.Enabled = false;
optimizeVistaGroupBox.Enabled = false;
}
else
{
optimizeVistaCheckbox.Checked = _settings.optimizeVistaCheckbox;
}

cropModeRemoveAllButton.Checked = _settings.cropModeRemoveAllButton;
cropModeKeepCenteredButton.Checked = _settings.cropModeKeepCenteredButton;

Expand Down Expand Up @@ -104,7 +98,12 @@ public MainForm()
aeroColorGroupBox.Enabled = aeroColorCheckbox.Checked;
shadowGroupBox.Enabled = shadowCheckbox.Checked;

hotkeyKey = _settings.hotkeyKey;
hotkeyModifier = _settings.hotkeyModifier;

_registryKey = Registry.CurrentUser.CreateSubKey(@"Software\AeroShot");

UpdateVisibleHotkey();
}

private static bool GlassAvailable()
Expand Down Expand Up @@ -312,10 +311,12 @@ private void OkButtonClick(object sender, EventArgs e)

_registryKey.SetValue("SaveInactiveTransparent", saveInactiveTransparentCheckbox.Checked ? 1 : 0, RegistryValueKind.DWord);

_registryKey.SetValue("OptimizeVista", optimizeVistaCheckbox.Checked ? 1 : 0, RegistryValueKind.DWord);

_registryKey.SetValue("CropMode", cropModeKeepCenteredButton.Checked ? 1 : 0, RegistryValueKind.DWord);

_registryKey.SetValue("HotkeyKey", hotkeyKey, RegistryValueKind.DWord);

_registryKey.SetValue("HotkeyModifier", hotkeyModifier, RegistryValueKind.DWord);

// Save delay settings in an 8-byte long
b = new byte[8];
b[0] = (byte)(delayCheckbox.Checked ? 1 : 0);
Expand All @@ -325,16 +326,46 @@ private void OkButtonClick(object sender, EventArgs e)
data = BitConverter.ToInt64(b, 0);
_registryKey.SetValue("Delay", data, RegistryValueKind.QWord);

if (needsRestart)
Application.Restart();

this.Close();
}

private void CancelButtonClick(object sender, EventArgs e)
{
this.Close();
}
}

public class ColorDisplay : UserControl
private void UpdateVisibleHotkey()
{
keyboardShortcutButton.Text = $"{KeyHelpers.modifiers[hotkeyModifier]} + {((Keys)hotkeyKey).ToString()}";
}

private void keyboardShortcutButton_Click(object sender, EventArgs e)
{
Button sndr = sender as Button;
sndr.Text = "...";
}

private void keyboardShortcutButton_KeyUp(object sender, KeyEventArgs e)
{
Button sndr = sender as Button;
//TODO: can i just detect if its a modifier key?
if (sndr.Text == "..."
&& !(e.KeyCode == Keys.Alt || e.KeyCode == Keys.ControlKey || e.KeyCode == Keys.ShiftKey || e.KeyCode == Keys.LWin || e.KeyCode == Keys.RWin)
&& KeyHelpers.modifierCodes.ContainsKey(e.Modifiers))
{
hotkeyModifier = KeyHelpers.modifierCodes[e.Modifiers];
hotkeyKey = (int)e.KeyCode;
needsRestart = true;
}

UpdateVisibleHotkey();
}
}

public class ColorDisplay : UserControl
{
private readonly SolidBrush _border = new SolidBrush(SystemColors.Window);

Expand Down
43 changes: 33 additions & 10 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */

using Microsoft.VisualBasic.ApplicationServices;
using System;
using System.Reflection;
using System.Runtime.InteropServices;
Expand All @@ -31,18 +32,18 @@ You should have received a copy of the GNU General Public License

namespace AeroShot
{
internal static class Program
class Program : WindowsFormsApplicationBase
{
[STAThread]
private static void Main()
//[STAThread]
public Program()
{
/*if (Environment.OSVersion.Version.Major < 6)
{
MessageBox.Show("Windows Vista or newer is required.", Application.ProductName);
return;
}*/

bool isFirstInstance;
/*bool isFirstInstance;
// set if truly first instance:
var mutex = new System.Threading.Mutex(true, "AeroShot", out isFirstInstance);
Expand All @@ -51,14 +52,36 @@ private static void Main()
{
MessageBox.Show("An instance of AeroShot or AeroShotCRE is already running.\r\nPress Alt+PrtSc to take a screenshot.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
//temporary workaround - we need to lose focus for inactive screenshot captures to work
//MessageBox.Show("Press OK to start the application", Application.ProductName);
}*/
//temporary workaround - we need to lose focus for inactive screenshot captures to work
//MessageBox.Show("Press OK to start the application", Application.ProductName);
EnableVisualStyles = true;
MainForm = new SysTray();
IsSingleInstance = true;

Application.EnableVisualStyles();
/*Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new SysTray());
GC.KeepAlive(mutex);
Application.Run(new SysTray());*/
//GC.KeepAlive(mutex);
}

public static void Main(string[] args)
{
Instance = new Program();
Instance.Run(args);
}

public static void Restart()
{
Instance.IsSingleInstance = false;
Application.Restart();
}

protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
{
MessageBox.Show("An instance of AeroShot or AeroShotCRE is already running.\r\nPress Alt+PrtSc to take a screenshot.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
}

private static Program Instance;
}
}
98 changes: 15 additions & 83 deletions Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,84 +52,10 @@ public class Settings
public bool optimizeVistaCheckbox;
public bool cropModeRemoveAllButton;
public bool cropModeKeepCenteredButton;
public int hotkeyKey = 44;
public int hotkeyModifier = 1;
private readonly RegistryKey _registryKey;

bool AeroGlassForWin8IsRunning()
{
return Process.GetProcessesByName("aerohost").Length > 0;
}

bool HasAeroAfterglow()
{
return (Environment.OSVersion.Version.Build >= 6730 && Environment.OSVersion.Version.Build < 8432) || AeroGlassForWin8IsRunning();
}

bool HasAeroTransparency()
{
return (Environment.OSVersion.Version.Major == 6
&& Environment.OSVersion.Version.Major > 5001 // pre-reset is hell, 500x does't have Aero
&& Environment.OSVersion.Version.Build < 8432 || AeroGlassForWin8IsRunning());
}

public static bool IsWindowsVista() => (Environment.OSVersion.Version.Major == 6 && Environment.OSVersion.Version.Major == 0); // 6.0.5048-6.0.6469

public static bool IsWindows7() => (Environment.OSVersion.Version.Major == 6 && Environment.OSVersion.Version.Major == 1); // 6.1.6519-6.2.7850

public static bool IsWindows8() => (Environment.OSVersion.Version.Major == 6 && Environment.OSVersion.Version.Major == 2); // 6.2.7875-6.3.9299

public static bool IsWindows81() => (Environment.OSVersion.Version.Major == 6 && Environment.OSVersion.Version.Major == 3); // 6.3.9364-6.3.9785

public static bool IsWindows10() => ((Environment.OSVersion.Version.Major == 6 && Environment.OSVersion.Version.Major == 4) // 6.4.9821-10.0.21390.2025
|| Environment.OSVersion.Version.Major == 10);

public static bool IsWindows11() // 10.0.21990+
{
if (Environment.OSVersion.Version.Major != 10)
{
return false;
}
else
{
// open the current version
RegistryKey RK = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion");

if (RK == null)
{
return false; // panic, best to assume not running 11
}
else
{
// get the current value
string BuildNumber = (string)RK.GetValue("CurrentBuild");

try
{
int BN = Convert.ToInt32(BuildNumber);

if (BN >= 21990)
{
return true;
}
else
{
return false;
}
}
catch (FormatException)
{
return false;

}
catch (OverflowException)
{
return false;
}


}
}
}

public Settings()
{
object value;
Expand Down Expand Up @@ -226,7 +152,7 @@ public Settings()
if ((value = _registryKey.GetValue("SaveActiveLight")) != null && value.GetType() == (typeof(int)))
saveActiveLightCheckbox = ((int)value & 1) == 1;
else
saveActiveLightCheckbox = HasAeroAfterglow();
saveActiveLightCheckbox = VersionHelpers.HasAeroAfterglow();

if ((value = _registryKey.GetValue("SaveInactiveDark")) != null && value.GetType() == (typeof(int)))
saveInactiveDarkCheckbox = ((int)value & 1) == 1;
Expand All @@ -236,27 +162,33 @@ public Settings()
if ((value = _registryKey.GetValue("SaveInactiveLight")) != null && value.GetType() == (typeof(int)))
saveInactiveLightCheckbox = ((int)value & 1) == 1;
else
saveInactiveLightCheckbox = HasAeroAfterglow();
saveInactiveLightCheckbox = VersionHelpers.HasAeroAfterglow();

if ((value = _registryKey.GetValue("SaveMask")) != null && value.GetType() == (typeof(int)))
saveMaskCheckbox = ((int)value & 1) == 1;
else
saveMaskCheckbox = HasAeroTransparency();
saveMaskCheckbox = VersionHelpers.HasAeroTransparency();

if ((value = _registryKey.GetValue("SaveActiveTransparent")) != null && value.GetType() == (typeof(int)))
saveActiveTransparentCheckbox = ((int)value & 1) == 1;
else
saveActiveTransparentCheckbox = HasAeroTransparency();
saveActiveTransparentCheckbox = VersionHelpers.HasAeroTransparency();

if ((value = _registryKey.GetValue("SaveInactiveTransparent")) != null && value.GetType() == (typeof(int)))
saveInactiveTransparentCheckbox = ((int)value & 1) == 1;
else
saveInactiveTransparentCheckbox = HasAeroTransparency();
saveInactiveTransparentCheckbox = VersionHelpers.HasAeroTransparency();

if ((value = _registryKey.GetValue("OptimizeVista")) != null && value.GetType() == (typeof(int)))
/*if ((value = _registryKey.GetValue("OptimizeVista")) != null && value.GetType() == (typeof(int)))
optimizeVistaCheckbox = ((int)value & 1) == 1;
else
optimizeVistaCheckbox = (Environment.OSVersion.Version.Major == 6 && Environment.OSVersion.Version.Minor == 0) ? true : false;
optimizeVistaCheckbox = (Environment.OSVersion.Version.Major == 6 && Environment.OSVersion.Version.Minor == 0) ? true : false;*/

if ((value = _registryKey.GetValue("HotkeyKey")) != null && value.GetType() == (typeof(int)))
hotkeyKey = (int)value;

if ((value = _registryKey.GetValue("HotkeyModifier")) != null && value.GetType() == (typeof(int)))
hotkeyModifier = (int)value;

if ((value = _registryKey.GetValue("Delay")) != null && value.GetType() == (typeof(long)))
{
Expand Down
Loading

0 comments on commit c7dfb02

Please sign in to comment.