Skip to content

Commit 70396bc

Browse files
authored
add exception handling for register & unregister hotkey (#3263)
1 parent 54a49d6 commit 70396bc

File tree

2 files changed

+46
-22
lines changed

2 files changed

+46
-22
lines changed

Flow.Launcher/Helper/HotKeyMapper.cs

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Flow.Launcher.Core;
99
using ChefKeys;
1010
using System.Globalization;
11+
using Flow.Launcher.Infrastructure.Logger;
1112

1213
namespace Flow.Launcher.Helper;
1314

@@ -39,38 +40,49 @@ internal static void OnToggleHotkeyWithChefKeys()
3940

4041
private static void SetHotkey(string hotkeyStr, EventHandler<HotkeyEventArgs> action)
4142
{
42-
if (hotkeyStr == "LWin" || hotkeyStr == "RWin")
43-
{
44-
SetWithChefKeys(hotkeyStr);
45-
return;
46-
}
47-
4843
var hotkey = new HotkeyModel(hotkeyStr);
4944
SetHotkey(hotkey, action);
5045
}
5146

5247
private static void SetWithChefKeys(string hotkeyStr)
5348
{
54-
ChefKeysManager.RegisterHotkey(hotkeyStr, hotkeyStr, OnToggleHotkeyWithChefKeys);
55-
ChefKeysManager.Start();
49+
try
50+
{
51+
ChefKeysManager.RegisterHotkey(hotkeyStr, hotkeyStr, OnToggleHotkeyWithChefKeys);
52+
ChefKeysManager.Start();
53+
}
54+
catch (Exception e)
55+
{
56+
Log.Error(
57+
string.Format("|HotkeyMapper.SetWithChefKeys|Error registering hotkey: {0} \nStackTrace:{1}",
58+
e.Message,
59+
e.StackTrace));
60+
string errorMsg = string.Format(InternationalizationManager.Instance.GetTranslation("registerHotkeyFailed"), hotkeyStr);
61+
string errorMsgTitle = InternationalizationManager.Instance.GetTranslation("MessageBoxTitle");
62+
MessageBoxEx.Show(errorMsg, errorMsgTitle);
63+
}
5664
}
5765

5866
internal static void SetHotkey(HotkeyModel hotkey, EventHandler<HotkeyEventArgs> action)
5967
{
6068
string hotkeyStr = hotkey.ToString();
61-
62-
if (hotkeyStr == "LWin" || hotkeyStr == "RWin")
63-
{
64-
SetWithChefKeys(hotkeyStr);
65-
return;
66-
}
67-
6869
try
6970
{
71+
if (hotkeyStr == "LWin" || hotkeyStr == "RWin")
72+
{
73+
SetWithChefKeys(hotkeyStr);
74+
return;
75+
}
76+
7077
HotkeyManager.Current.AddOrReplace(hotkeyStr, hotkey.CharKey, hotkey.ModifierKeys, action);
7178
}
72-
catch (Exception)
79+
catch (Exception e)
7380
{
81+
Log.Error(
82+
string.Format("|HotkeyMapper.SetHotkey|Error registering hotkey {2}: {0} \nStackTrace:{1}",
83+
e.Message,
84+
e.StackTrace,
85+
hotkeyStr));
7486
string errorMsg = string.Format(InternationalizationManager.Instance.GetTranslation("registerHotkeyFailed"), hotkeyStr);
7587
string errorMsgTitle = InternationalizationManager.Instance.GetTranslation("MessageBoxTitle");
7688
MessageBoxEx.Show(errorMsg, errorMsgTitle);
@@ -79,15 +91,26 @@ internal static void SetHotkey(HotkeyModel hotkey, EventHandler<HotkeyEventArgs>
7991

8092
internal static void RemoveHotkey(string hotkeyStr)
8193
{
82-
if (hotkeyStr == "LWin" || hotkeyStr == "RWin")
94+
try
8395
{
84-
RemoveWithChefKeys(hotkeyStr);
85-
return;
86-
}
96+
if (hotkeyStr == "LWin" || hotkeyStr == "RWin")
97+
{
98+
RemoveWithChefKeys(hotkeyStr);
99+
return;
100+
}
87101

88-
if (!string.IsNullOrEmpty(hotkeyStr))
102+
if (!string.IsNullOrEmpty(hotkeyStr))
103+
HotkeyManager.Current.Remove(hotkeyStr);
104+
}
105+
catch (Exception e)
89106
{
90-
HotkeyManager.Current.Remove(hotkeyStr);
107+
Log.Error(
108+
string.Format("|HotkeyMapper.RemoveHotkey|Error removing hotkey: {0} \nStackTrace:{1}",
109+
e.Message,
110+
e.StackTrace));
111+
string errorMsg = string.Format(InternationalizationManager.Instance.GetTranslation("unregisterHotkeyFailed"), hotkeyStr);
112+
string errorMsgTitle = InternationalizationManager.Instance.GetTranslation("MessageBoxTitle");
113+
MessageBoxEx.Show(errorMsg, errorMsgTitle);
91114
}
92115
}
93116

Flow.Launcher/Languages/en.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
<!-- MainWindow -->
1717
<system:String x:Key="registerHotkeyFailed">Failed to register hotkey "{0}". The hotkey may be in use by another program. Change to a different hotkey, or exit another program.</system:String>
18+
<system:String x:Key="unregisterHotkeyFailed">Failed to unregister hotkey "{0}". Please try again or see log for details</system:String>
1819
<system:String x:Key="MessageBoxTitle">Flow Launcher</system:String>
1920
<system:String x:Key="couldnotStartCmd">Could not start {0}</system:String>
2021
<system:String x:Key="invalidFlowLauncherPluginFileFormat">Invalid Flow Launcher plugin file format</system:String>

0 commit comments

Comments
 (0)