Recent

Author Topic: Hooking the keyboard  (Read 10553 times)

zoiddani

  • New Member
  • *
  • Posts: 29
Hooking the keyboard
« on: January 30, 2011, 11:59:17 am »
Hello,
I need to hook the keyboard in windows.
From a tutorial I got this code
Code: [Select]
SetWindowsHookEx(WH_KEYBOARD,
           {callback —>} @KeyboardHookProc,
                          HInstance,
                          GetCurrentThreadId())
However this code is in Delphi. It works with $mode delphi, but not with objfpc.
I'm quite addicted to objfpc, and I wanted to rewrite the code in it. But this SetWindowsHookEx has another declaration then in delphi.
I get this message from the compiler:
Code: [Select]
code.pas(44,45) Error: Incompatible type for arg no. 2: Got "<address of function(LongInt, Word, LongInt):LongInt;StdCall>", expected "<procedure variable type of function(LongInt, LongInt, LongInt):LongInt;StdCall>"
Could somebody show me an example of using this in objfpc?
Thank you

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Hooking the keyboard
« Reply #1 on: January 30, 2011, 01:35:26 pm »
Weird. Usually Delphi is the one which doesn't expect @ to pass procedural types. Try removing the @ symbol.

zoiddani

  • New Member
  • *
  • Posts: 29
Re: Hooking the keyboard
« Reply #2 on: January 31, 2011, 07:43:16 pm »
Thank you. Actually I only had to rewrite the declaration and it was solved. However, when I'm trying to set up a global hook, I have to use this code:

Code: [Select]
@SetHookHandle:=GetProcAddress(LibHandle,'SetHookHandle');

global.pas(63,5) Error: Can't assign values to an address

With delphi mode, this works, but I want to avoid that. Any ideas how to solve this?
Thanks, Zoiddani

Laksen

  • Hero Member
  • *****
  • Posts: 802
    • J-Software
Re: Hooking the keyboard
« Reply #3 on: January 31, 2011, 07:55:08 pm »
I think it's something like this:

Pointer(SetHookHandle):=GetProcAddress(LibHandle,'SetHookHandle');

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Hooking the keyboard
« Reply #4 on: February 01, 2011, 02:20:51 am »
better:
Code: [Select]
type
 TSetHookHandle = <procedural type, declare required params and modifiers here>

SetHookHandle:=TSetHookHandle(GetProcAddress(LibHandle,'SetHookHandle'));

 

TinyPortal © 2005-2018