/
Program.cs
119 lines (98 loc) · 4.38 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace PortalCounter
{
static class Program
{
[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);
private const int WH_KEYBOARD_LL = 13;
private const int WM_KEYDOWN = 0x0100;
private static LowLevelKeyboardProc _proc = HookCallback;
private static IntPtr _hookID = IntPtr.Zero;
private static bool chatactive = false;
private static System.Timers.Timer aTimer = new System.Timers.Timer(1000);
private static bool preventSpam = false;
private static MainForm mForm = new MainForm();
public static bool hook = true;
[STAThread]
static void Main()
{
aTimer.Elapsed += aTimer_Elapsed;
_hookID = SetHook(_proc);
Application.EnableVisualStyles();
//Application.SetCompatibleTextRenderingDefault(false);
Application.Run(mForm);
UnhookWindowsHookEx(_hookID);
}
private static void aTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
preventSpam = false;
}
private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr 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);
}
}
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (hook && nCode >= 0 && (wParam == (IntPtr)WM_KEYDOWN || Control.ModifierKeys != Keys.None))
{
Keys vkCode = (Keys)Marshal.ReadInt32(lParam);
// read settings
bool bChat = Properties.Settings.Default.ProtectChat;
Keys kHotKey = PortalCounter.Properties.Settings.Default.HotKey;
Keys kModifier = Properties.Settings.Default.Modifier;
// deactivate chat only if hotkey isnt enter
if (bChat && vkCode == Keys.Enter && kHotKey != Keys.Enter)
chatactive = !chatactive;
if (!chatactive)
{
mForm.BackColor = System.Drawing.Color.Black;
Console.WriteLine(kHotKey + " - " + Control.ModifierKeys);
kHotKey = normalizeHotkey(kHotKey);
// (Hotkey and modifier) or (hokey is actual modifier)
if (!preventSpam && (vkCode.Equals(kHotKey) && Control.ModifierKeys == kModifier) || Control.ModifierKeys.Equals(kHotKey))
{
mForm.startTimer();
if (!kModifier.Equals(Keys.None))
{
preventSpam = true;
aTimer.Start();
}
}
}
else
{
mForm.BackColor = System.Drawing.Color.OrangeRed;
}
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
private static Keys normalizeHotkey(Keys input)
{
if (input.Equals(Keys.ControlKey))
return Keys.Control;
if (input.Equals(Keys.Menu))
return Keys.Alt;
if(input.Equals(Keys.ShiftKey))
return Keys.Shift;
return input;
}
}
}