ShareX/ShareX/HotkeyManager.cs

229 lines
8.6 KiB
C#
Raw Normal View History

2013-11-03 23:53:49 +13:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2023-01-10 09:31:02 +13:00
Copyright (c) 2007-2023 ShareX Team
2013-11-03 23:53:49 +13:00
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
2014-12-11 09:25:20 +13:00
using ShareX.HelpersLib;
using ShareX.Properties;
using System;
2013-11-03 23:53:49 +13:00
using System.Collections.Generic;
using System.Diagnostics;
2013-11-03 23:53:49 +13:00
using System.Linq;
using System.Windows.Forms;
namespace ShareX
{
public class HotkeyManager
{
public List<HotkeySettings> Hotkeys { get; private set; }
2013-11-03 23:53:49 +13:00
public bool IgnoreHotkeys { get; set; }
public delegate void HotkeyTriggerEventHandler(HotkeySettings hotkeySetting);
public delegate void HotkeysToggledEventHandler(bool hotkeysEnabled);
2013-11-03 23:53:49 +13:00
public HotkeyTriggerEventHandler HotkeyTrigger;
public HotkeysToggledEventHandler HotkeysToggledTrigger;
2013-11-03 23:53:49 +13:00
private HotkeyForm hotkeyForm;
public HotkeyManager(HotkeyForm form)
2013-11-03 23:53:49 +13:00
{
hotkeyForm = form;
hotkeyForm.HotkeyPress += hotkeyForm_HotkeyPress;
2013-11-14 22:31:38 +13:00
hotkeyForm.FormClosed += (sender, e) => hotkeyForm.InvokeSafe(() => UnregisterAllHotkeys(false));
2016-02-12 13:15:23 +13:00
}
public void UpdateHotkeys(List<HotkeySettings> hotkeys, bool showFailedHotkeys)
{
if (Hotkeys != null)
{
UnregisterAllHotkeys();
}
2013-11-03 23:53:49 +13:00
Hotkeys = hotkeys;
RegisterAllHotkeys();
if (showFailedHotkeys)
2015-09-30 05:58:38 +13:00
{
ShowFailedHotkeys();
}
2013-11-03 23:53:49 +13:00
}
private void hotkeyForm_HotkeyPress(ushort id, Keys key, Modifiers modifier)
{
if (!IgnoreHotkeys && (!Program.Settings.DisableHotkeysOnFullscreen || !CaptureHelpers.IsActiveWindowFullscreen()))
2013-11-03 23:53:49 +13:00
{
HotkeySettings hotkeySetting = Hotkeys.Find(x => x.HotkeyInfo.ID == id);
if (hotkeySetting != null)
{
OnHotkeyTrigger(hotkeySetting);
}
}
}
protected void OnHotkeyTrigger(HotkeySettings hotkeySetting)
{
2021-06-10 10:14:01 +12:00
HotkeyTrigger?.Invoke(hotkeySetting);
2013-11-03 23:53:49 +13:00
}
public void RegisterHotkey(HotkeySettings hotkeySetting)
{
if (!Program.Settings.DisableHotkeys || hotkeySetting.TaskSettings.Job == HotkeyType.DisableHotkeys)
2013-11-03 23:53:49 +13:00
{
UnregisterHotkey(hotkeySetting, false);
2013-11-03 23:53:49 +13:00
if (hotkeySetting.HotkeyInfo.Status != HotkeyStatus.Registered && hotkeySetting.HotkeyInfo.IsValidHotkey)
2013-11-03 23:53:49 +13:00
{
hotkeyForm.RegisterHotkey(hotkeySetting.HotkeyInfo);
if (hotkeySetting.HotkeyInfo.Status == HotkeyStatus.Registered)
{
DebugHelper.WriteLine("Hotkey registered: " + hotkeySetting);
}
else if (hotkeySetting.HotkeyInfo.Status == HotkeyStatus.Failed)
{
DebugHelper.WriteLine("Hotkey register failed: " + hotkeySetting);
}
2013-11-03 23:53:49 +13:00
}
2022-08-01 04:42:59 +12:00
else
{
hotkeySetting.HotkeyInfo.Status = HotkeyStatus.NotConfigured;
}
2013-11-03 23:53:49 +13:00
}
if (!Hotkeys.Contains(hotkeySetting))
{
Hotkeys.Add(hotkeySetting);
}
}
public void RegisterAllHotkeys()
{
foreach (HotkeySettings hotkeySetting in Hotkeys.ToArray())
{
RegisterHotkey(hotkeySetting);
}
}
public void UnregisterHotkey(HotkeySettings hotkeySetting, bool removeFromList = true)
{
if (hotkeySetting.HotkeyInfo.Status == HotkeyStatus.Registered)
{
hotkeyForm.UnregisterHotkey(hotkeySetting.HotkeyInfo);
if (hotkeySetting.HotkeyInfo.Status == HotkeyStatus.NotConfigured)
{
DebugHelper.WriteLine("Hotkey unregistered: " + hotkeySetting);
}
else if (hotkeySetting.HotkeyInfo.Status == HotkeyStatus.Failed)
{
DebugHelper.WriteLine("Hotkey unregister failed: " + hotkeySetting);
}
}
if (removeFromList)
{
Hotkeys.Remove(hotkeySetting);
}
}
public void UnregisterAllHotkeys(bool removeFromList = true, bool temporary = false)
2013-11-03 23:53:49 +13:00
{
if (Hotkeys != null)
2013-11-03 23:53:49 +13:00
{
foreach (HotkeySettings hotkeySetting in Hotkeys.ToArray())
{
if (!temporary || hotkeySetting.TaskSettings.Job != HotkeyType.DisableHotkeys)
{
UnregisterHotkey(hotkeySetting, removeFromList);
}
}
}
}
public void ToggleHotkeys(bool hotkeysDisabled)
{
if (!hotkeysDisabled)
{
RegisterAllHotkeys();
}
else
{
UnregisterAllHotkeys(false, true);
}
2021-06-10 10:14:01 +12:00
HotkeysToggledTrigger?.Invoke(hotkeysDisabled);
2013-11-03 23:53:49 +13:00
}
public void ShowFailedHotkeys()
{
List<HotkeySettings> failedHotkeysList = Hotkeys.Where(x => x.HotkeyInfo.Status == HotkeyStatus.Failed).ToList();
2013-11-03 23:53:49 +13:00
if (failedHotkeysList.Count > 0)
2013-11-03 23:53:49 +13:00
{
string failedHotkeys = string.Join("\r\n", failedHotkeysList.Select(x => x.TaskSettings.ToString() + ": " + x.HotkeyInfo.ToString()));
string hotkeyText = failedHotkeysList.Count > 1 ? Resources.HotkeyManager_ShowFailedHotkeys_hotkeys : Resources.HotkeyManager_ShowFailedHotkeys_hotkey;
string text = string.Format(Resources.HotkeyManager_ShowFailedHotkeys_Unable_to_register_hotkey, hotkeyText, failedHotkeys);
2013-11-03 23:53:49 +13:00
string[] processNames = new string[] { "ShareX", "OneDrive", "Dropbox", "Greenshot", "ScreenshotCaptor", "FSCapture", "Snagit32", "puush", "Lightshot" };
int ignoreProcess = Process.GetCurrentProcess().Id;
List<string> conflictProcessNames = Process.GetProcesses().Where(x => x.Id != ignoreProcess && !string.IsNullOrEmpty(x.ProcessName) &&
2022-10-15 12:10:59 +13:00
processNames.Any(x2 => x.ProcessName.Equals(x2, StringComparison.OrdinalIgnoreCase))).
2022-06-21 18:30:32 +12:00
Select(x => string.Format("{0} ({1})", x.MainModule.FileVersionInfo.ProductName, x.MainModule.ModuleName)).Distinct().ToList();
if (conflictProcessNames != null && conflictProcessNames.Count > 0)
{
text += "\r\n\r\n" + Resources.HotkeyManager_ShowFailedHotkeys_These_applications_could_be_conflicting_ + "\r\n\r\n" + string.Join("\r\n", conflictProcessNames);
}
MessageBox.Show(text, "ShareX - " + Resources.HotkeyManager_ShowFailedHotkeys_Hotkey_registration_failed, MessageBoxButtons.OK, MessageBoxIcon.Warning);
2013-11-03 23:53:49 +13:00
}
}
public void ResetHotkeys()
{
UnregisterAllHotkeys();
Hotkeys.AddRange(GetDefaultHotkeyList());
RegisterAllHotkeys();
if (Program.Settings.DisableHotkeys)
{
TaskHelpers.ToggleHotkeys();
}
2013-11-03 23:53:49 +13:00
}
public static List<HotkeySettings> GetDefaultHotkeyList()
2013-11-03 23:53:49 +13:00
{
return new List<HotkeySettings>
2013-11-03 23:53:49 +13:00
{
new HotkeySettings(HotkeyType.RectangleRegion, Keys.Control | Keys.PrintScreen),
new HotkeySettings(HotkeyType.PrintScreen, Keys.PrintScreen),
2013-11-03 23:53:49 +13:00
new HotkeySettings(HotkeyType.ActiveWindow, Keys.Alt | Keys.PrintScreen),
new HotkeySettings(HotkeyType.ScreenRecorder, Keys.Shift | Keys.PrintScreen),
new HotkeySettings(HotkeyType.ScreenRecorderGIF, Keys.Control | Keys.Shift | Keys.PrintScreen)
2013-11-03 23:53:49 +13:00
};
}
}
}