0% found this document useful (0 votes)
6 views2 pages

Keybinding Method

The document outlines a C# implementation for setting up a low-level keyboard hook using P/Invoke to interact with Windows API functions. It includes methods for hooking and unhooking keyboard events, handling key presses, and managing key bindings with toggle switches. The code also ensures proper cleanup of hooks when the application exits.

Uploaded by

tgrguildleader1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Keybinding Method

The document outlines a C# implementation for setting up a low-level keyboard hook using P/Invoke to interact with Windows API functions. It includes methods for hooking and unhooking keyboard events, handling key presses, and managing key bindings with toggle switches. The code also ensures proper cleanup of hooks when the application exits.

Uploaded by

tgrguildleader1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

[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)]


public static extern IntPtr GetModuleHandle(string lpModuleName);

private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr


lParam);

private const int WH_KEYBOARD_LL = 13;


private const int WM_KEYDOWN = 0x0100;

private IntPtr hookID = IntPtr.Zero;


private LowLevelKeyboardProc hookCallback;

private Dictionary<Guna2Button, Keys> keyBindings = new Dictionary<Guna2Button,


Keys>();
private Dictionary<Guna2Button, Guna2ToggleSwitch> toggleMapping = new
Dictionary<Guna2Button, Guna2ToggleSwitch>();
private Guna2Button waitingForKeyButton = null;

hookCallback = HookCallback;
hookID = SetHook(hookCallback);
Application.ApplicationExit += Application_ApplicationExit;

keyBindings[headkey] = Keys.None;
toggleMapping[headkey] = guna2ToggleSwitch1;

//////Defination///////

private void Application_ApplicationExit(object sender, EventArgs e)


{
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)
{
int vkCode = Marshal.ReadInt32(lParam);
Keys key = (Keys)vkCode;

if (waitingForKeyButton != null)
{

if (key == Keys.Escape)
{
keyBindings[waitingForKeyButton] = Keys.None;
waitingForKeyButton.Text = "None";
}
else
{
keyBindings[waitingForKeyButton] = key;
waitingForKeyButton.Text = key.ToString();
}
waitingForKeyButton = null;
}
else
{

foreach (var kvp in keyBindings)


{
if (kvp.Value == key)
{
Guna2ToggleSwitch toggleSwitch = toggleMapping[kvp.Key];
toggleSwitch.Checked = !toggleSwitch.Checked;
}
}
}
}
return CallNextHookEx(hookID, nCode, wParam, lParam);
}

///////Button Code///////

waitingForKeyButton = button;
button.Text = "...";

You might also like