Soo. nun kommt er xD
In die Program.cs über Main kommt:
private static IntPtr _hookID = IntPtr.Zero;
Dann in den Main private fügst du folgendes ein:
Program._hookID = KeyMonitor.Start();
Application.Run(new Form1());//Anpassen
KeyMonitor.UnhookWindowsHookEx(Program._hookID);
KeyMonitor.cs:
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace SampAntiCheat
{
internal class KeyMonitor
{
private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
private const int WH_KEYBOARD_LL = 13;
private const int WM_KEYDOWN = 256;
private static KeyMonitor.LowLevelKeyboardProc _proc = new KeyMonitor.LowLevelKeyboardProc(KeyMonitor.HookCallback);
private static IntPtr _hookID = IntPtr.Zero;
public static IntPtr Start()
{
KeyMonitor._hookID = KeyMonitor.SetHook(KeyMonitor._proc);
return KeyMonitor._hookID;
}
private static IntPtr SetHook(KeyMonitor.LowLevelKeyboardProc proc)
{
IntPtr result;
using (Process currentProcess = Process.GetCurrentProcess())
{
using (ProcessModule mainModule = currentProcess.MainModule)
{
result = KeyMonitor.SetWindowsHookEx(13, proc, KeyMonitor.GetModuleHandle(mainModule.ModuleName), 0u);
}
}
return result;
}
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && wParam == (IntPtr)256)
{
int num = Marshal.ReadInt32(lParam);
string text = ((Keys)num).ToString();
if (text.Equals("F1"))
{
//Was soll Passieren, wenn du F1 drückst?
MessageBox.Show("Du hast F1 gedrückt.");
}
}
return KeyMonitor.CallNextHookEx(KeyMonitor._hookID, nCode, wParam, lParam);
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook, KeyMonitor.LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public 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);
}
}
Alles anzeigen