From 6b05a75154094f1bda08a6fb3bb6ff4299241aa8 Mon Sep 17 00:00:00 2001 From: Markellus Date: Sun, 11 Jun 2017 13:53:45 +0200 Subject: [PATCH 1/4] Cleanup, don't allow multiple instances, bugfixes --- AltF4/AltF4.csproj | 6 +- AltF4/AltF4Handler.cs | 104 +++++++++++++++++++++++++++++++ AltF4/NativeMethods.cs | 33 ++++++++++ AltF4/Program.cs | 93 +++++---------------------- AltF4/Properties/AssemblyInfo.cs | 8 +-- AltF4/SafeList.cs | 23 +++++++ 6 files changed, 181 insertions(+), 86 deletions(-) create mode 100644 AltF4/AltF4Handler.cs create mode 100644 AltF4/NativeMethods.cs create mode 100644 AltF4/SafeList.cs diff --git a/AltF4/AltF4.csproj b/AltF4/AltF4.csproj index 725678e..33c1e5d 100644 --- a/AltF4/AltF4.csproj +++ b/AltF4/AltF4.csproj @@ -52,15 +52,15 @@ + + + - - Always - \ No newline at end of file diff --git a/AltF4/AltF4Handler.cs b/AltF4/AltF4Handler.cs new file mode 100644 index 0000000..82341df --- /dev/null +++ b/AltF4/AltF4Handler.cs @@ -0,0 +1,104 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using static AltF4.NativeMethods; + +namespace AltF4 +{ + internal class AltF4Handler + { + public static AltF4Handler Get() + { + return _instance; + } + + private static AltF4Handler _instance = new AltF4Handler(); + + private static SafeList _listKeys; + + private static IntPtr _hookID = IntPtr.Zero; + + private bool _fired; + + private LowLevelKeyboardProc _callback; + + public event EventHandler OnAltF4; + + private AltF4Handler() + { + _listKeys = new SafeList(); + _callback = HookCallback; + _hookID = SetHook(_callback); + _fired = false; + } + + ~AltF4Handler() + { + UnhookWindowsHookEx(_hookID); + } + + private IntPtr SetHook(LowLevelKeyboardProc proc) + { + using (Process curProcess = Process.GetCurrentProcess()) + { + using (ProcessModule curModule = curProcess.MainModule) + { + return SetWindowsHookEx(WH_KEYBOARD_LL, proc, + GetModuleHandle(curModule.ModuleName), 0); + } + } + } + + private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) + { + if (nCode < 0) + { + return CallNextHookEx(_hookID, nCode, wParam, lParam); + } + + int vkCode = Marshal.ReadInt32(lParam); + Keys code = (Keys)vkCode; + + if (KeyDown(wParam)) + { + _listKeys.SafeAdd(code); + } + else if (KeyUp(wParam)) + { + _listKeys.SafeRemove(code); + } + + if (_listKeys.Contains(Keys.F4) && + (_listKeys.Contains(Keys.LMenu) || _listKeys.Contains(Keys.Alt))) + { + if(!_fired) + { + _fired = true; + OnAltF4?.Invoke(this, EventArgs.Empty); + } + return (IntPtr)1; + } + else + { + _fired = false; + } + + return CallNextHookEx(_hookID, nCode, wParam, lParam); + } + + private bool KeyDown(IntPtr wParam) + { + return (wParam == (IntPtr)WM_KEYDOWN || wParam == (IntPtr)WM_SYSKEYDOWN); + } + + private bool KeyUp(IntPtr wParam) + { + return (wParam == (IntPtr)WM_KEYUP || wParam == (IntPtr)WM_SYSKEYUP); + } + } +} diff --git a/AltF4/NativeMethods.cs b/AltF4/NativeMethods.cs new file mode 100644 index 0000000..d2339b3 --- /dev/null +++ b/AltF4/NativeMethods.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; + +namespace AltF4 +{ + internal static class NativeMethods + { + public const int WH_KEYBOARD_LL = 13; + public const int WM_KEYDOWN = 0x0100; + public const int WM_SYSKEYDOWN = 0x0104; + public const int WM_KEYUP = 0x0101; + public const int WM_SYSKEYUP = 0x0105; + + [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] + public static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId); + + [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + public static extern bool UnhookWindowsHookEx(IntPtr hhk); + + [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] + public static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam); + + [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] + public static extern IntPtr GetModuleHandle(string lpModuleName); + + internal delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam); + } +} diff --git a/AltF4/Program.cs b/AltF4/Program.cs index 3f3bf5e..5f483f8 100644 --- a/AltF4/Program.cs +++ b/AltF4/Program.cs @@ -10,86 +10,34 @@ namespace AltF4 { class Program { - private const int WH_KEYBOARD_LL = 13; - private const int WM_KEYDOWN = 0x0100; - private const int WM_SYSKEYDOWN = 0x0104; - private const int WM_KEYUP = 0x0101; - private const int WM_SYSKEYUP = 0x0105; - private static LowLevelKeyboardProc _proc = HookCallback; - private static IntPtr _hookID = IntPtr.Zero; - - internal delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam); - - private static List listKeys = new List(); - - private static bool bKilled = false; private static Process procFocus; private static void Main(string[] args) { + Process[] _procRunning = Process.GetProcessesByName("AltF4"); + + if(_procRunning.Length > 0) + { + Environment.Exit(0); + } + AutomationFocusChangedEventHandler focusHandler = OnFocusChanged; Automation.AddAutomationFocusChangedEventHandler(focusHandler); - _hookID = SetHook(_proc); + AltF4Handler.Get().OnAltF4 += OnAltF4; Application.Run(); - - UnhookWindowsHookEx(_hookID); } - private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) + private static void OnAltF4(object sender, EventArgs e) { - if (nCode >= 0) + if (procFocus != null && !procFocus.HasExited) { - int vkCode = Marshal.ReadInt32(lParam); - Keys code = (Keys)vkCode; - - if (wParam == (IntPtr)WM_KEYDOWN || wParam == (IntPtr)WM_SYSKEYDOWN) + try { - - - if (!listKeys.Contains(code)) - { - listKeys.Add(code); - } - - if (listKeys.Contains(Keys.F4) && - (listKeys.Contains(Keys.LMenu) || listKeys.Contains(Keys.Alt))) - { - if (!bKilled && procFocus != null && !procFocus.HasExited) - { - procFocus.Kill(); - bKilled = true; - } - return (IntPtr)1; - } + procFocus.Kill(); } - else if (wParam == (IntPtr)WM_KEYUP || wParam == (IntPtr)WM_SYSKEYUP) - { - if (listKeys.Contains(code)) - { - listKeys.Remove(code); - } - - if (!listKeys.Contains(Keys.F4) && - !listKeys.Contains(Keys.LMenu) && - !listKeys.Contains(Keys.Alt)) - { - bKilled = false; - } - } - } - - return CallNextHookEx(_hookID, nCode, wParam, lParam); - } - - private static IntPtr SetHook(LowLevelKeyboardProc proc) - { - using (Process curProcess = Process.GetCurrentProcess()) - using (ProcessModule curModule = curProcess.MainModule) - { - return SetWindowsHookEx(WH_KEYBOARD_LL, proc, - GetModuleHandle(curModule.ModuleName), 0); + catch { } } } @@ -101,19 +49,6 @@ private static void OnFocusChanged(object sender, AutomationFocusChangedEventArg int processId = focusedElement.Current.ProcessId; procFocus = Process.GetProcessById(processId); } - } - - [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] - private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId); - - [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] - [return: MarshalAs(UnmanagedType.Bool)] - private static extern bool UnhookWindowsHookEx(IntPtr hhk); - - [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] - private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam); - - [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] - private static extern IntPtr GetModuleHandle(string lpModuleName); + } } } diff --git a/AltF4/Properties/AssemblyInfo.cs b/AltF4/Properties/AssemblyInfo.cs index e7203e1..e1a004d 100644 --- a/AltF4/Properties/AssemblyInfo.cs +++ b/AltF4/Properties/AssemblyInfo.cs @@ -5,11 +5,11 @@ // General Information about an assembly is controlled through the following // set of attributes. Change these attribute values to modify the information // associated with an assembly. -[assembly: AssemblyTitle("AltF4 Rage Quit Enabler")] +[assembly: AssemblyTitle("Extreme AltF4")] [assembly: AssemblyDescription("Overrides Alt F4 to kill a process immediatly")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("AltF4 Rage Quit Enabler")] +[assembly: AssemblyProduct("Extreme AltF4")] [assembly: AssemblyCopyright("Copyright © Marcel Bulla 2017")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] @@ -32,5 +32,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.1.0")] -[assembly: AssemblyFileVersion("1.0.1.0")] +[assembly: AssemblyVersion("1.1.0.0")] +[assembly: AssemblyFileVersion("1.1.0.0")] diff --git a/AltF4/SafeList.cs b/AltF4/SafeList.cs new file mode 100644 index 0000000..7d7a1af --- /dev/null +++ b/AltF4/SafeList.cs @@ -0,0 +1,23 @@ +using System.Collections.Generic; + +namespace AltF4 +{ + internal class SafeList : List + { + public void SafeAdd(T item) + { + if (!this.Contains(item)) + { + this.Add(item); + } + } + + public void SafeRemove(T item) + { + if (this.Contains(item)) + { + this.Remove(item); + } + } + } +} From 6701df309317caae56719587a4839efe0efbf5e4 Mon Sep 17 00:00:00 2001 From: Markellus Date: Sun, 18 Jun 2017 12:34:12 +0200 Subject: [PATCH 2/4] changed license to MS-RL --- AltF4/AltF4Handler.cs | 13 ++++++++----- AltF4/NativeMethods.cs | 13 ++++++++----- AltF4/Program.cs | 15 ++++++++++++--- AltF4/SafeList.cs | 9 ++++++++- AltF4/app.manifest | 32 +++++++++++++------------------ LICENSE | 43 +++++++++++++++++++++--------------------- 6 files changed, 70 insertions(+), 55 deletions(-) diff --git a/AltF4/AltF4Handler.cs b/AltF4/AltF4Handler.cs index 82341df..fd9c51a 100644 --- a/AltF4/AltF4Handler.cs +++ b/AltF4/AltF4Handler.cs @@ -1,10 +1,13 @@ -using System; -using System.Collections.Generic; +/********************************************** + * Extreme AltF4 * + * (C) 2017 Marcel Bulla * + * https://github.com/markellus/Extreme-AltF4 * + * See file LICENSE for license information * + **********************************************/ + +using System; using System.Diagnostics; -using System.Linq; using System.Runtime.InteropServices; -using System.Text; -using System.Threading.Tasks; using System.Windows.Forms; using static AltF4.NativeMethods; diff --git a/AltF4/NativeMethods.cs b/AltF4/NativeMethods.cs index d2339b3..d03378c 100644 --- a/AltF4/NativeMethods.cs +++ b/AltF4/NativeMethods.cs @@ -1,9 +1,12 @@ -using System; -using System.Collections.Generic; -using System.Linq; +/********************************************** + * Extreme AltF4 * + * (C) 2017 Marcel Bulla * + * https://github.com/markellus/Extreme-AltF4 * + * See file LICENSE for license information * + **********************************************/ + +using System; using System.Runtime.InteropServices; -using System.Text; -using System.Threading.Tasks; namespace AltF4 { diff --git a/AltF4/Program.cs b/AltF4/Program.cs index 5f483f8..21f58c5 100644 --- a/AltF4/Program.cs +++ b/AltF4/Program.cs @@ -1,19 +1,26 @@ using System; using System.Diagnostics; using System.Windows.Forms; -using System.Runtime.InteropServices; using System.Windows.Automation; -using System.Collections.Generic; -using System.IO; namespace AltF4 { + /// + /// Main class + /// class Program { + /// + /// The program which is currently in foreground. + /// private static Process procFocus; + /// + /// Program entry method + /// private static void Main(string[] args) { + //check if the program is already running Process[] _procRunning = Process.GetProcessesByName("AltF4"); if(_procRunning.Length > 0) @@ -21,11 +28,13 @@ private static void Main(string[] args) Environment.Exit(0); } + //Add event handlers AutomationFocusChangedEventHandler focusHandler = OnFocusChanged; Automation.AddAutomationFocusChangedEventHandler(focusHandler); AltF4Handler.Get().OnAltF4 += OnAltF4; + //Start program loop Application.Run(); } diff --git a/AltF4/SafeList.cs b/AltF4/SafeList.cs index 7d7a1af..1bf6d01 100644 --- a/AltF4/SafeList.cs +++ b/AltF4/SafeList.cs @@ -1,4 +1,11 @@ -using System.Collections.Generic; +/********************************************** + * Extreme AltF4 * + * (C) 2017 Marcel Bulla * + * https://github.com/markellus/Extreme-AltF4 * + * See file LICENSE for license information * + **********************************************/ + +using System.Collections.Generic; namespace AltF4 { diff --git a/AltF4/app.manifest b/AltF4/app.manifest index 7825f40..edc58ef 100644 --- a/AltF4/app.manifest +++ b/AltF4/app.manifest @@ -1,4 +1,14 @@  + + + @@ -11,25 +21,9 @@ - - - - - - - - - - - - - - - - - + + + diff --git a/LICENSE b/LICENSE index 8872af1..a2c144a 100644 --- a/LICENSE +++ b/LICENSE @@ -1,22 +1,21 @@ -DISCLAIMER: - -This repository does NOT contain open source code/software. You may look up the source code under the conditions defined in the following license. - -_________________________________________________________ - -Extreme AltF4 LICENSE - -Copyright 2017 Marcel Bulla - -1. The author of this software grants the following permissions to any person obtaining a copy of this software, the source code and associated documentation files (the "Software"): - -1.1 The right to use, copy, publish and/or distribute the Software - Under the following conditions: - -1.1.1 The Software, including the installer, may not be modified IN ANY WAY, -1.1.2 The Software may only be distributed in its original form, -1.1.3 The Software and the distribution/publishing service must remain free of charge at all time, -1.1.4 The distributor/publisher must not use any form of advertisement, except for the Software himself. - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file +Microsoft Reciprocal License (MS-RL) + +This license governs use of the accompanying software. If you use the software, you accept this license. If you do not accept the license, do not use the software. + +1. Definitions +The terms "reproduce," "reproduction," "derivative works," and "distribution" have the same meaning here as under U.S. copyright law. +A "contribution" is the original software, or any additions or changes to the software. +A "contributor" is any person that distributes its contribution under this license. +"Licensed patents" are a contributor's patent claims that read directly on its contribution. + +2. Grant of Rights +(A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free copyright license to reproduce its contribution, prepare derivative works of its contribution, and distribute its contribution or any derivative works that you create. +(B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of its contribution in the software or derivative works of the contribution in the software. + +3. Conditions and Limitations +(A) Reciprocal Grants- For any file you distribute that contains code from the software (in source code or binary format), you must provide recipients the source code to that file along with a copy of this license, which license will govern that file. You may license other files that are entirely your own work and do not contain code from the software under any terms you choose. +(B) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks. +(C) If you bring a patent claim against any contributor over patents that you claim are infringed by the software, your patent license from such contributor to the software ends automatically. +(D) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution notices that are present in the software. +(E) If you distribute any portion of the software in source code form, you may do so only under this license by including a complete copy of this license with your distribution. If you distribute any portion of the software in compiled or object code form, you may only do so under a license that complies with this license. +(F) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the extent permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular purpose and non-infringement. \ No newline at end of file From 526f1d982eaa5228644042a65d924742d7ca71fe Mon Sep 17 00:00:00 2001 From: Markellus Date: Sun, 18 Jun 2017 12:37:08 +0200 Subject: [PATCH 3/4] Updateing setup build script and README --- README.md | 4 ++-- Setup.iss | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 27d3ae2..7fe59b9 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Extreme AltF4 is a tool that overrides the default behavior of windows when pres ### Prerequisites -Extreme AltF4 requires .NET Framework 4.5 or higher to run properly. +Extreme AltF4 requires .NET Framework 4.6.1 or higher to run properly. ### Installing @@ -27,4 +27,4 @@ Marcel Bulla (Markellus) ## License -This project has a custom license! - see the [LICENSE](LICENSE) file for details. \ No newline at end of file +See the [LICENSE](LICENSE) file for details. \ No newline at end of file diff --git a/Setup.iss b/Setup.iss index 021bfdf..4c47e00 100644 --- a/Setup.iss +++ b/Setup.iss @@ -30,12 +30,12 @@ ShowLanguageDialog=no UsePreviousGroup=False DisableProgramGroupPage=yes UninstallRestartComputer=True -VersionInfoVersion=1.0.1 +VersionInfoVersion=1.1.0 VersionInfoCompany=Marcel Bulla VersionInfoDescription=Extreme AltF4 - AltF4 Rage Quit Enabler VersionInfoCopyright=(C) Marcel Bulla VersionInfoProductName=Extreme AltF4 - AltF4 Rage Quit Enabler -VersionInfoProductVersion=1.0.1 +VersionInfoProductVersion=1.1.0 InternalCompressLevel=ultra MinVersion=0,6.2 DefaultDirName={pf}/Markellus Software/Extreme AltF4 From 84b437132a6996b24605e3079deab2d674ea27c9 Mon Sep 17 00:00:00 2001 From: Markellus Date: Sun, 18 Jun 2017 12:45:41 +0200 Subject: [PATCH 4/4] Fixed typo --- AltF4/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AltF4/Program.cs b/AltF4/Program.cs index 21f58c5..0a820ab 100644 --- a/AltF4/Program.cs +++ b/AltF4/Program.cs @@ -23,7 +23,7 @@ private static void Main(string[] args) //check if the program is already running Process[] _procRunning = Process.GetProcessesByName("AltF4"); - if(_procRunning.Length > 0) + if(_procRunning.Length > 1) { Environment.Exit(0); }