PDA

Просмотр полной версии : C# хук клавиш для варика


ltg
17.02.2022, 07:45
добречка всем дорогие программисты
с нуля начинаю и пишу хук клавиш именно для доты
using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WinFormsApp3
{

class HotKey
{
//If the function is executed successfully, the return value is not 0.
//If the function fails, the return value is 0. To get extended error information, call GetLastError.
[DllImport("user32.dll", SetLastError = true)]
public static extern bool RegisterHotKey(
IntPtr hWnd,//The handle of the window to define the hotkey
int id,//Define the hot key ID (cannot be repeated with other IDs)
KeyModifiers fsModifiers,//Identify whether the hotkey will take effect when you press Alt, Ctrl, Shift, Windows, etc.
Keys vk//Define the content of the hotkey
);


[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnregisterHotKey(
IntPtr hWnd,//The handle of the window to cancel the hotkey
int id//To cancel the hot key ID
);


//The name of the auxiliary key is defined (the number is converted into a character for easy memory, or the enumeration can be removed and the value can be used directly)
[Flags()]
public enum KeyModifiers
{
None = 0,
Alt = 1,
Ctrl = 2,
Shift = 4,
WindowsKey = 8
}
}

public class GlobalKeyboardHook
{
[DllImport("user32.dll")]
static extern int CallNextHookEx(IntPtr hhk, int code, int wParam, ref keyBoardHookStruct lParam);
[DllImport("user32.dll")]
static extern IntPtr SetWindowsHookEx(int idHook, LLKeyboardHook callback, IntPtr hInstance, uint theardID);
[DllImport("user32.dll")]
static extern bool UnhookWindowsHookEx(IntPtr hInstance);
[DllImport("kernel32.dll")]
static extern IntPtr LoadLibrary(string lpFileName);

public delegate int LLKeyboardHook(int Code, int wParam, ref keyBoardHookStruct lParam);

public struct keyBoardHookStruct
{
public int vkCode;
public int scanCode;
public int flags;
public int time;
public int dwExtraInfo;
}

const int WH_KEYBOARD_LL = 13;
const int WM_KEYDOWN = 0x0100;
const int WM_KEYUP = 0x0101;
const int WM_SYSKEYDOWN = 0x0104;
const int WM_SYSKEYUP = 0x0105;

LLKeyboardHook llkh;
public List<Keys> HookedKeys = new List<Keys>();

IntPtr Hook = IntPtr.Zero;

public event KeyEventHandler KeyDown;
public event KeyEventHandler KeyUp;

public GlobalKeyboardHook()
{
llkh = new LLKeyboardHook(HookProc);

}
~GlobalKeyboardHook()
{ unhook(); }

public void hook()
{
IntPtr hInstance = LoadLibrary("User32");
Hook = SetWindowsHookEx(WH_KEYBOARD_LL, llkh, hInstance, 0);
}

public void unhook()
{
UnhookWindowsHookEx(Hook);
}

public int HookProc(int Code, int wParam, ref keyBoardHookStruct lParam)
{
if (Code >= 0)
{
Keys key = (Keys)lParam.vkCode;
if (HookedKeys.Contains(key))
{
KeyEventArgs kArg = new KeyEventArgs(key);
if ((wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN) && (KeyDown != null))
KeyDown(this, kArg);
else if ((wParam == WM_KEYUP || wParam == WM_SYSKEYUP) && (KeyUp != null))
KeyUp(this, kArg);
if (kArg.Handled)
return 1;
}
}
return CallNextHookEx(Hook, Code, wParam, ref lParam);
}
}
}

сама форма
GlobalKeyboardHook gHook;
private void Form1_Load(object sender, EventArgs e) //
{
gHook = new GlobalKeyboardHook(); // Создаём новый GlobalKeyBoardHook
gHook.KeyDown += new KeyEventHandler(gHook_KeyDown); // Объявляем новый ивент
foreach (Keys key in Enum.GetValues(typeof(Keys)))
gHook.HookedKeys.Add(key);
}

public void gHook_KeyDown(object sender, KeyEventArgs e)
{
textBox1.Text += ((char)e.KeyValue).ToString();
}



private void button1_Click_1(object sender, EventArgs e)
{
gHook.hook();
}

private void button2_Click(object sender, EventArgs e)
{
gHook.unhook();
}
помогите как соединить к варкрафту?? кто ни будь сталкивался?