Как заставить хук ловить 2 клавиши? - C#
Формулировка задачи:
Есть глобальный хук
Вызов
Но проблема в том что он ловит только 1 клавишу. А надо чтобы ловил комбинации.
Как видно я добавил в код переменные _key2 и конструкцию которая проверяет не равен ли он -1 и вставляет его в код. Но не работает.
Помогите. Заранее спс.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Sp108.com
{
public class Hook : IDisposable
{
#region Declare WinAPI functions
[DllImport("kernel32.dll")]
private static extern IntPtr LoadLibrary(string lpFileName);
[DllImport("user32.dll")]
private static extern IntPtr SetWindowsHookEx(int idHook, KeyboardHookProc callback, IntPtr hInstance, uint threadId);
[DllImport("user32.dll")]
private static extern IntPtr CallNextHookEx(IntPtr idHook, int nCode, int wParam, IntPtr lParam);
[DllImport("user32.dll")]
private static extern bool UnhookWindowsHookEx(IntPtr hInstance);
#endregion
#region Constants
private const int WH_KEYBOARD_LL = 13;
private const int WH_KEYDOWN = 0x0100;
#endregion
// код клавиши на которую ставим хук
private int _key;
private int _key2;
public event KeyPressEventHandler KeyPressed;
private delegate IntPtr KeyboardHookProc(int code, IntPtr wParam, IntPtr lParam);
private KeyboardHookProc _proc;
private IntPtr _hHook = IntPtr.Zero;
public Hook(int keyCode, int keyCode2 = -1)
{
_key = keyCode;
_key2 = keyCode2;
_proc = HookProc;
}
public void SetHook()
{
var hInstance = LoadLibrary("User32");
_hHook = SetWindowsHookEx(WH_KEYBOARD_LL, _proc, hInstance, 0);
}
public void Dispose()
{
UnHook();
}
public void UnHook()
{
UnhookWindowsHookEx(_hHook);
}
private IntPtr HookProc(int code, IntPtr wParam, IntPtr lParam)
{
if(_key2 != -1)
{
if ((code >= 0 && wParam == (IntPtr)WH_KEYDOWN) && Marshal.ReadInt32(lParam) == _key && Marshal.ReadInt32(lParam) == _key2)
{
if (KeyPressed != null)
{
KeyPressed(this, new KeyPressEventArgs(Convert.ToChar(code)));
}
}
} else
{
if ((code >= 0 && wParam == (IntPtr)WH_KEYDOWN) && Marshal.ReadInt32(lParam) == _key)
{
if (KeyPressed != null)
{
KeyPressed(this, new KeyPressEventArgs(Convert.ToChar(code)));
}
}
}
// пробрасываем хук дальше
return CallNextHookEx(_hHook, code, (int)wParam, lParam);
}
}
} private void Form1_Load(object sender, EventArgs e)
{
_hook = new com.Hook(0x60);
_hook.KeyPressed += new KeyPressEventHandler(_hook_hide);
_hook.SetHook();
}
void _hook_hide(object sender, KeyPressEventArgs e) //Событие нажатия клавиш
{
this.Visible = !this.Visible;
this.Activate();
}Решение задачи: «Как заставить хук ловить 2 клавиши?»
textual
Листинг программы
[StructLayout(LayoutKind.Sequential)]
private struct KeyboardDescription
{
public uint KeyCode;
public uint ScanCode;
public KeyboardFlags Flags;
public uint Time;
public IntPtr ExtraInfo;
}
[Flags]
private enum KeyboardFlags
{
Extended = 0x01,
Injected = 0x10,
AltDown = 0x20,
}
private static IntPtr HookCallback(int code, IntPtr wParam, IntPtr lParam)
{
if (code == 0)
{
var description = (KeyboardDescription)Marshal.PtrToStructure(lParam, typeof(KeyboardDescription));
var keys = (Keys)description.KeyCode;
var isDown = wParam.ToInt32() == 0x100 || wParam.ToInt32() == 0x104;
if (isDown)
{
// Нажата
}
else
{
// Поднята
}
}
return CallNextHookEx(hookHandle, code, wParam, lParam);
}