hi everyone,
i would like implement keys hook in my application but i still have 1 error:
Erreur 1 error C3352: 'int IHMV2::FrmPrincipal::captureKey(int,System::IntPtr,System::IntPtr)' : la fonction spécifiée est incompatible avec le type délégué 'int (int,System::IntPtr,System::IntPtr)'
I don't find the solution, can you help me?
Thanks,
Rémy
code:
delegate int LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
LowLevelKeyboardProc^ objKeyboardProcess;
[DllImport("user32.dll", CharSet = CharSet::Auto, SetLastError = true)]
static int SetWindowsHookEx(int id, LowLevelKeyboardProc^ callback, IntPtr hMod, int dwThreadId);
[DllImport("user32.dll", CharSet = CharSet::Auto, SetLastError = true)]
static bool UnhookWindowsHookEx(IntPtr hook);
[DllImport("user32.dll", CharSet = CharSet::Auto, SetLastError = true)]
static int CallNextHookEx(int hook, int nCode, IntPtr wp, IntPtr lp);
[DllImport("kernel32.dll", CharSet = CharSet::Auto, SetLastError = true)]
static IntPtr GetModuleHandle(String^ name);
[DllImport("user32.dll", CharSet = CharSet::Auto)]
static short GetAsyncKeyState(Keys key);
private: int ptrHook;
public: int captureKey(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0)
{
KBDLLHOOKSTRUCT^ objKeyInfo = (KBDLLHOOKSTRUCT)Marshal::PtrToStructure(lParam, KBDLLHOOKSTRUCT::typeid);
if (objKeyInfo->key == Keys::RWin || objKeyInfo->key == Keys::LWin || objKeyInfo->key == Keys::Tab && HasAltModifier(objKeyInfo->flags) || objKeyInfo->key == Keys::Escape && (ModifierKeys & Keys::Control) == Keys::Control)
{
return 1;
}
}
return CallNextHookEx(ptrHook, nCode, wParam, lParam);
}
bool HasAltModifier(int flags)
{
return (flags & 0x20) == 0x20;
}
public:
[StructLayout(LayoutKind::Sequential)]
value struct KBDLLHOOKSTRUCT
{
public: Keys key;
public: int scanCode;
public: int flags;
public: int time;
public: IntPtr extra;
};
private: System::Void FrmPrincipal_Load(System::Object^ sender, System::EventArgs^ e) {
ProcessModule^ objCurrentModule = Process::GetCurrentProcess()->MainModule;
objKeyboardProcess = gcnew LowLevelKeyboardProc(&FrmPrincipal::captureKey);
ptrHook = SetWindowsHookEx(13, objKeyboardProcess, GetModuleHandle(objCurrentModule->ModuleName), 0);
}