Recent

Author Topic: Call procedure in class function (SOLVED)  (Read 182 times)

TheLastCayen

  • Jr. Member
  • **
  • Posts: 50
Call procedure in class function (SOLVED)
« on: November 21, 2019, 10:55:48 pm »
Hi,

I am using:
 - Windows 7 64 bit
 - Lazarus 2.0.6 64 bit
 - FPC 3.0.4 64 bit

In order to use a windows function, I had to create a class function. From that class function, I am not able to access any other procedure, function or object property from the rest of my code.  How can I use that class function to interact with my code? This is the code I have a problem with:

Code: Pascal  [Select]
  1. Const
  2.   WH_KEYBOARD_LL = 13;
  3.  
  4. type
  5.   KeybdLLHookStruct = record
  6.     vkCode      : cardinal;
  7.     scanCode    : cardinal;
  8.     flags       : cardinal;
  9.     time        : cardinal;
  10.     dwExtraInfo : cardinal;
  11.   end;
  12.  
  13. procedure TForm1.FormCreate(Sender: TObject);
  14. begin
  15.     HookHandle := SetWindowsHookExa(WH_KEYBOARD_LL, HookPROC(@TForm1.AHookProc),HInstance, 0);
  16. end;  
  17.  
  18. procedure TForm1.FormDestroy(Sender: TObject);
  19. begin
  20.     UnhookWindowsHookEx(HookHandle);
  21. end;
  22.  
  23. procedure TForm1.SimpleAutoClick;
  24. begin
  25.   If TimerSimpleClick.Enabled = False then
  26.     begin
  27.       TimerSimpleClick.Interval:=strtoint(LabeledEdit1.Text);
  28.       TimerSimpleClick.Enabled := True;
  29.     end
  30.   else
  31.     TimerSimpleClick.Enabled := False;
  32. end;
  33.  
  34. class Function Tform1.AHookProc(nCode:LongInt; wParam:WPARAM;Lparam:LParam):LRESULT; StdCall;
  35. var
  36.   keyboard : ^KeybdLLHookStruct absolute lParam;
  37. begin
  38.   Result :=  CallnextHookEx(HookHandle,nCode,wParam, lparam);
  39.   if wParam = WM_KEYDOWN then
  40.     case keyboard^.vkCode of
  41.       VK_F4: TForm1.SimpleAutoClick;                 //Error: Only class methods, class properties and class variables can be accessed in class methods
  42.       VK_F5: TimerSimpleClick.Enabled := True;   //Error: Only class methods, class properties and class variables can be accessed in class methods
  43.       //VK_F5: PlayProfile;
  44.       //VK_F7: AddToProfile;
  45.       //VK_F8: DelFromProfile;
  46.     end;
  47. end;
  48.  
  49.  

In the worst-case scenario, I was thinking to use variables and a timer that checks the status of those variables but it feels like a cheap patch and a waste of resources:( So how real programmer deal with that kind of scenario?

Thank you

« Last Edit: November 22, 2019, 12:02:36 am by TheLastCayen »

jamie

  • Hero Member
  • *****
  • Posts: 2264
Re: Call procedure in class function
« Reply #1 on: November 21, 2019, 11:33:54 pm »
Form1. not TFORM.

You need to interact with the Instance of the form not the class of it.

 form1.SimpleAutoClose;

 Make that a normal method I guess.
etc.
Number 1 at blue screen app creations!

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7865
Re: Call procedure in class function
« Reply #2 on: November 21, 2019, 11:43:30 pm »
And make it a STATIC class proc, otherwise it will go wrong. Class procs still have a reference to the class type as hidden param

For bonus points, don't use the form1 variable, but pass form via the parameter (LPARAM etc) to the class proc handler. This is useful if you have handler react to multiple classes individually

TheLastCayen

  • Jr. Member
  • **
  • Posts: 50
Re: Call procedure in class function
« Reply #3 on: November 22, 2019, 12:02:24 am »
Thank you Jamie, it fixes my issue. I was so focused on the error I didn't notice I forgot to remove the t after I copy and past;)

sorry marcov, I am still a noob, I don't understand what you mean by making it a static class proc. Can you give me an example?

Thank you