May I please kindly ask I need to merge all my .CS Files into 1 big .CS Main File in order for everything to work
I have the following code which I found on the Internet
I guess that this First Section of Code is for the .CS File - LowLevelKeyboardHook.CS
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class LowLevelKeyboardHook
{
private const int WH_KEYBOARD_LL = 13;
private const int WM_KEYDOWN = 0x0100;
private const int WM_SYSKEYDOWN = 0x0104;
private const int WM_KEYUP = 0x101;
private const int WM_SYSKEYUP = 0x105;
[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);
public delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
public event EventHandler<Keys> OnKeyPressed;
public event EventHandler<Keys> OnKeyUnpressed;
private LowLevelKeyboardProc _proc;
private IntPtr _hookID = IntPtr.Zero;
public LowLevelKeyboardHook()
{
_proc = HookCallback;
}
public void HookKeyboard()
{
_hookID = SetHook(_proc);
}
public void UnHookKeyboard()
{
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 && wParam == (IntPtr)WM_KEYDOWN || wParam == (IntPtr)WM_SYSKEYDOWN)
{
int vkCode = Marshal.ReadInt32(lParam);
OnKeyPressed.Invoke(this, ((Keys)vkCode));
}
else if(nCode >= 0 && wParam == (IntPtr)WM_KEYUP ||wParam == (IntPtr)WM_SYSKEYUP)
{
int vkCode = Marshal.ReadInt32(lParam);
OnKeyUnpressed.Invoke(this, ((Keys)vkCode));
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
}
Now these Code are for the Main.CS File
kbh = new LowLevelKeyboardHook();
kbh.OnKeyPressed += kbh_OnKeyPressed;
kbh.OnKeyUnpressed += kbh_OnKeyUnpressed;
kbh.HookKeyboard();
I suspect the following Code could be found in the HookCallBack in the - LowLevelKeyboardHook.CS File
bool lctrlKeyPressed;
bool f1KeyPressed;
void kbh_OnKeyPressed(object sender, Keys e)
{
if (e == Keys.LControlKey)
{
lctrlKeyPressed = true;
}
else if (e == Keys.F1)
{
f1KeyPressed= true;
}
CheckKeyCombo();
}
void kbh_OnKeyUnPressed(object sender, Keys e)
{
if (e == Keys.LControlKey)
{
lctrlKeyPressed = false;
}
else if (e == Keys.F1)
{
f1KeyPressed= false;
}
}
void CheckKeyCombo()
{
if (lctrlKeyPressed && f1KeyPressed)
{
//Open Form
}
}
And Finally Here is the Code for the Main.CS File
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
LowLevelKeyboardHook kbh = new LowLevelKeyboardHook();
kbh.OnKeyPressed += kbh_OnKeyPressed;
kbh.OnKeyUnpressed += kbh_OnKeyUnpressed;
kbh.HookKeyboard();
Application.Run();
kbh.UnHookKeyboard();
}
What I would like to know is - How can I combine all These Codes into 1 Main Big .CS File ?
E.G. How do I combine everything in the File - LowLevelKeyboardHook.CS into the Main.CS File ?
Hope to hear from you soon
Thank you very much for your time and kind help
Kind Regards as Always Sir