Skip to content

Commit cd0f9d9

Browse files
committed
HotKeys without UI Settings.
1 parent 601b5a1 commit cd0f9d9

23 files changed

+1044
-302
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Linq;
3+
using System.ComponentModel;
4+
5+
namespace SmartSystemMenu.Extensions
6+
{
7+
static class EnumExtensions
8+
{
9+
public static string GetDescription(this Enum value)
10+
{
11+
var attribute = value.GetType().GetField(value.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false).FirstOrDefault() as DescriptionAttribute;
12+
var description = attribute == null ? null : attribute.Description;
13+
return description;
14+
}
15+
}
16+
}

SmartSystemMenu/Extensions/PriorityExtensions.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ public static int GetMenuItemId(this Priority priority)
88
{
99
switch (priority)
1010
{
11-
case Priority.RealTime: return (int)SystemMenu.SC_PRIORITY_REAL_TIME;
12-
case Priority.High: return (int)SystemMenu.SC_PRIORITY_HIGH;
13-
case Priority.AboveNormal: return (int)SystemMenu.SC_PRIORITY_ABOVE_NORMAL;
14-
case Priority.Normal: return (int)SystemMenu.SC_PRIORITY_NORMAL;
15-
case Priority.BelowNormal: return (int)SystemMenu.SC_PRIORITY_BELOW_NORMAL;
16-
case Priority.Idle: return (int)SystemMenu.SC_PRIORITY_IDLE;
17-
default: return (int)SystemMenu.SC_PRIORITY_NORMAL;
11+
case Priority.RealTime: return (int)MenuItemId.SC_PRIORITY_REAL_TIME;
12+
case Priority.High: return (int)MenuItemId.SC_PRIORITY_HIGH;
13+
case Priority.AboveNormal: return (int)MenuItemId.SC_PRIORITY_ABOVE_NORMAL;
14+
case Priority.Normal: return (int)MenuItemId.SC_PRIORITY_NORMAL;
15+
case Priority.BelowNormal: return (int)MenuItemId.SC_PRIORITY_BELOW_NORMAL;
16+
case Priority.Idle: return (int)MenuItemId.SC_PRIORITY_IDLE;
17+
default: return (int)MenuItemId.SC_PRIORITY_NORMAL;
1818
}
1919
}
2020

SmartSystemMenu/Forms/MainForm.cs

Lines changed: 120 additions & 103 deletions
Large diffs are not rendered by default.

SmartSystemMenu/Forms/PositionForm.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ private void ButtonApplyClick(object sender, EventArgs e)
3636
_window.ShowNormal();
3737
_window.SetPosition(left, top);
3838
_window.Menu.UncheckAlignmentMenu();
39-
_window.Menu.CheckMenuItem(SystemMenu.SC_ALIGN_CUSTOM, true);
39+
_window.Menu.CheckMenuItem(MenuItemId.SC_ALIGN_CUSTOM, true);
4040
}
4141
catch
4242
{

SmartSystemMenu/Forms/SettingsForm.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ private void ButtonApplyClick(object sender, EventArgs e)
242242

243243
foreach (DataGridViewRow row in gvStartProgram.Rows)
244244
{
245-
settings.MenuItems.StartProgramItems.Add(new StartProgramItem { Title = row.Cells[0].Value.ToString(), FileName = row.Cells[1].Value.ToString(), Arguments = row.Cells[2].Value.ToString() });
245+
settings.MenuItems.StartProgramItems.Add(new StartProgramMenuItem { Title = row.Cells[0].Value.ToString(), FileName = row.Cells[1].Value.ToString(), Arguments = row.Cells[2].Value.ToString() });
246246
}
247247

248248
settings.LanguageName = cmbLanguage.SelectedValue == null ? "" : cmbLanguage.SelectedValue.ToString();

SmartSystemMenu/Forms/SizeForm.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ private void ButtonApplyClick(object sender, EventArgs e)
3535
_window.ShowNormal();
3636
_window.SetSize(width, height);
3737
_window.Menu.UncheckSizeMenu();
38-
_window.Menu.CheckMenuItem(SystemMenu.SC_SIZE_CUSTOM, true);
39-
_window.Menu.UncheckMenuItems(SystemMenu.SC_ROLLUP);
38+
_window.Menu.CheckMenuItem(MenuItemId.SC_SIZE_CUSTOM, true);
39+
_window.Menu.UncheckMenuItems(MenuItemId.SC_ROLLUP);
4040
}
4141
catch
4242
{

SmartSystemMenu/Forms/TransparencyForm.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ private void ButtonApplyClick(object sender, EventArgs e)
3030
var value = (Byte)numericTransparency.Value;
3131
_window.SetTrancparency(value);
3232
_window.Menu.UncheckTransparencyMenu();
33-
_window.Menu.CheckMenuItem(SystemMenu.SC_TRANS_CUSTOM, true);
33+
_window.Menu.CheckMenuItem(MenuItemId.SC_TRANS_CUSTOM, true);
3434
}
3535
catch
3636
{
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using SmartSystemMenu.Native;
5+
using SmartSystemMenu.Settings;
6+
7+
namespace SmartSystemMenu.HotKeys
8+
{
9+
class HotKeyHook : IDisposable
10+
{
11+
private IntPtr _hookHandle;
12+
private KeyboardHookProc _hookProc;
13+
private IList<MenuItem> _menuItems;
14+
15+
public event EventHandler<HotKeyHookEventArgs> Hooked;
16+
17+
public bool Start(string moduleName, IList<MenuItem> menuItems)
18+
{
19+
_menuItems = menuItems;
20+
_hookProc = HookProc;
21+
var moduleHandle = NativeMethods.GetModuleHandle(moduleName);
22+
_hookHandle = NativeMethods.SetWindowsHookEx(NativeConstants.WH_KEYBOARD_LL, _hookProc, moduleHandle, 0);
23+
var hookStarted = _hookHandle != IntPtr.Zero;
24+
return hookStarted;
25+
}
26+
27+
public bool Stop()
28+
{
29+
if (_hookHandle == IntPtr.Zero)
30+
{
31+
return true;
32+
}
33+
var hookStoped = NativeMethods.UnhookWindowsHookEx(_hookHandle);
34+
return hookStoped;
35+
}
36+
37+
public void Dispose()
38+
{
39+
Dispose(true);
40+
GC.SuppressFinalize(this);
41+
}
42+
43+
protected virtual void Dispose(bool disposing)
44+
{
45+
if (disposing)
46+
{
47+
// get rid of managed resources
48+
}
49+
50+
Stop();
51+
}
52+
53+
~HotKeyHook()
54+
{
55+
Dispose(false);
56+
}
57+
58+
private int HookProc(int code, IntPtr wParam, ref KBDLLHOOKSTRUCT lParam)
59+
{
60+
if (code == NativeConstants.HC_ACTION)
61+
{
62+
if (wParam.ToInt32() == NativeConstants.WM_KEYDOWN)
63+
{
64+
foreach (var item in _menuItems.Where(x => x.HotKeyEnabled))
65+
{
66+
var key1 = true;
67+
var key2 = true;
68+
69+
if (item.Key1 != VirtualKeyModifier.None)
70+
{
71+
var key1State = NativeMethods.GetAsyncKeyState((int)item.Key1) & 0x8000;
72+
key1 = Convert.ToBoolean(key1State);
73+
}
74+
75+
if (item.Key2 != VirtualKeyModifier.None)
76+
{
77+
var key2State = NativeMethods.GetAsyncKeyState((int)item.Key2) & 0x8000;
78+
key2 = Convert.ToBoolean(key2State);
79+
}
80+
81+
if (key1 && key2 && lParam.vkCode == (int)item.Key3)
82+
{
83+
var handler = Hooked;
84+
if (handler != null)
85+
{
86+
var menuItemId = MenuItemId.GetMenuItemId(item.Name);
87+
var eventArgs = new HotKeyHookEventArgs(menuItemId);
88+
handler.BeginInvoke(this, eventArgs, null, null);
89+
break;
90+
}
91+
}
92+
93+
}
94+
}
95+
}
96+
97+
return NativeMethods.CallNextHookEx(_hookHandle, code, wParam, ref lParam);
98+
}
99+
}
100+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
3+
namespace SmartSystemMenu.HotKeys
4+
{
5+
class HotKeyHookEventArgs : EventArgs
6+
{
7+
public int MenuItemId { get; private set; }
8+
9+
public HotKeyHookEventArgs(int menuItemId)
10+
{
11+
MenuItemId = menuItemId;
12+
}
13+
}
14+
}

0 commit comments

Comments
 (0)