Recent

Author Topic: [Solved] Cannot detect key[Down|Press|Up] activity in Lazarus on MacOS X 11-14  (Read 1717 times)

BradleySlavik

  • New member
  • *
  • Posts: 7
Running Lazarus on MacOS 11 and 14 (two different machines).

Using simple Pong program from user Handoko.

I have opened Form with Object Inspector and set KeyPreview to True.
This added the line:
   KeyPreview:=True;
to the TForm1.FormCreate procedure. Hurrah!

Added declarations and definitions for TForm1.FormKey[Down|Press|Create]
Just trying to detect keypress in the most basic fashion.
Started with body of ShowMessage('Key Down!') and no filtering to see if code was run.
Even tried WriteLn('Key Up!').
But none of these procedures, in spite no logic, seem to reach the procedures.
Clearly I am not understanding something very basic.
I would certainly appreciate any insight on where to add debugging code or which pages to read under LCL or other library that I have failed to understand.

The program works perfectly under Windows 10.
I am happy to dig into Lazarus to fix the underlying code so it works on MacOS, but I sure would appreciate some pointers as to where to start digging.

I have been reading about LCLIntf, and that looks like a good place to start.

Any thoughts?


« Last Edit: November 24, 2024, 10:30:56 pm by BradleySlavik »

jamie

  • Hero Member
  • *****
  • Posts: 6791
doing that in the OBJECT inspector should have not added anything in the OnCreate/FormCreate event.

Look at the YourPRoject.LFM file so see how things are setup.

Jamie

The only true wisdom is knowing you know nothing

dseligo

  • Hero Member
  • *****
  • Posts: 1443
Try this: go to Menu/Project Options, Application, and then you have button Create Application Bundle.

BradleySlavik

  • New member
  • *
  • Posts: 7
doing that in the OBJECT inspector should have not added anything in the OnCreate/FormCreate event.

Look at the YourPRoject.LFM file so see how things are setup.

Great! I look in there. It says KeyPreview = True


BradleySlavik

  • New member
  • *
  • Posts: 7
Try this: go to Menu/Project Options, Application, and then you have button Create Application Bundle.

It says "The Application Bundle was created for "Pathname_to_project"

Running program still does not detect key presses.
Pressing ordinary keys does not make any noise.
If I press arrow keys, the computer makes clicking/popping noise. So something knows they are being pushed, but not letting the program under Lazarus receive them.

I have bluetooth keyboard. I tried plugging it in but that changes nothing. These are Intel Macs. Not the new Apple silicon ones.

Thanks for telling me about the Project options section. I will start looking through that.

BradleySlavik

  • New member
  • *
  • Posts: 7
Now I am seeing new messages.

Warning: ld: warning: -multiply_defined is obsolete
Error: -macosx_version_min has been renamed to -macos_version_min
Warning: ld: warning: ignoring duplicate libraries: '-lc'
Warning: ld: warning: no platform load command found in 'ProjectPath/x86-64-darwin/project1-or', assuming: macOS

The machine that I have not tried to make bundle does not have these errors.

Handoko

  • Hero Member
  • *****
  • Posts: 5382
  • My goal: build my own game engine using Lazarus
I don't have MacOS computer for testing the code. Anyone have who has MacOS computer please help, the code is here:
https://forum.lazarus.freepascal.org/index.php/topic,42439.msg297949.html#msg297949

@BradleySlavik

The Pong demo I wrote, using GetKeyStated function. Maybe it is not implemented on MacOS or having bug. Alternatively you can use FormKeyDown event, here has a example code of using FormKeyDown:
https://forum.lazarus.freepascal.org/index.php/topic,57229.msg425440.html#msg425440

carl_caulkett

  • Hero Member
  • *****
  • Posts: 649
I don't have MacOS computer for testing the code. Anyone have who has MacOS computer please help, the code is here:
https://forum.lazarus.freepascal.org/index.php/topic,42439.msg297949.html#msg297949

I've just tried it with Lazarus 3.99 (4.99) + FPC 3.3.1 running on macOS 15.1.1 Sequoia. The project builds and runs, but the bat does not respond to any cursor movements.

It seems that GetKeyState is not implemented on macOS. Cmd clicking the GetKeyState function call, just reveals this (non)implementation...
Code: Pascal  [Select][+][-]
  1. function TWidgetSet.GetKeyState(nVirtKey: Integer): Smallint;
  2. begin
  3.   Result := 0;
  4. end;
  5.  

However, your suggestion of using the OnKeyUp and OnKeyDown events works fine!
« Last Edit: November 24, 2024, 01:42:34 pm by carl_caulkett »
"It builds... ship it!"

Handoko

  • Hero Member
  • *****
  • Posts: 5382
  • My goal: build my own game engine using Lazarus
Thank you for reporting it.
So, GetKeyState is an unimplemented feature in MacOS.

dsiders

  • Hero Member
  • *****
  • Posts: 1324
It seems that GetKeyState is not implemented on macOS. Cmd clicking the GetKeyState function call, just reveals this (non)implementation...
Code: Pascal  [Select][+][-]
  1. function TWidgetSet.GetKeyState(nVirtKey: Integer): Smallint;
  2. begin
  3.   Result := 0;
  4. end;
  5.  

TWidgetSet is a base class. The macOS-specific implementation is in:

* lcl/interfaces/cocoa/cocoawinapi.inc
* lcl/interfaces/cocoa/cocoawinapih.inc
Preview the next Lazarus documentation release at: https://dsiders.gitlab.io/lazdocsnext

BradleySlavik

  • New member
  • *
  • Posts: 7
I don't have MacOS computer for testing the code. Anyone have who has MacOS computer please help, the code is here:
https://forum.lazarus.freepascal.org/index.php/topic,42439.msg297949.html#msg297949

I've just tried it with Lazarus 3.99 (4.99) + FPC 3.3.1 running on macOS 15.1.1 Sequoia. The project builds and runs, but the bat does not respond to any cursor movements.

It seems that GetKeyState is not implemented on macOS. Cmd clicking the GetKeyState function call, just reveals this (non)implementation...
Code: Pascal  [Select][+][-]
  1. function TWidgetSet.GetKeyState(nVirtKey: Integer): Smallint;
  2. begin
  3.   Result := 0;
  4. end;
  5.  

However, your suggestion of using the OnKeyUp and OnKeyDown events works fine!


OK. I think I am starting to understand. I did not realize that you have to add a procedure using Object Editor.

The Object Editor is idiosyncratic on MacOS.

If you already have a function named the default name that it wants to create, it will fail and complain about unit or method not being find and highlight the last line of the unit section (with the semicolon).

If the default does not already exist, it will allow you to type in your own procedure name.

Now that I have added procedure I can see it is working. I made the body of the procedure Halt; so it will stop the program, because that is proof enough that KeyDown has been detected, and I can run with that. Thank you to everyone for getting me over this hump.

One last question. The right-click to "Find Declaration of GetKeyState" is not working.
How do I fix that?

And since GetKeyState is not implemented on MacOS, I know it might be unMacLike, but let's say I wanted to implement it, how would I start, since I cannot find source file?

Perhaps I need to install Lazarus sources somewhere and point to them?

Thanks!

Bradley






TRon

  • Hero Member
  • *****
  • Posts: 3787
And since GetKeyState is not implemented on MacOS, I know it might be unMacLike, but let's say I wanted to implement it, how would I start, since I cannot find source file?

Perhaps I need to install Lazarus sources somewhere and point to them?
Lazarus comes with its sources. Otherwise it wouldn't be able to compile itself when installing additional components.

In case you do not wish to meddle with those sources (which is a fair point) then they are available at sourceforge here. Other than that the sources are at github and a copy at github.
« Last Edit: November 24, 2024, 06:59:53 pm by TRon »
I do not have to remember anything anymore thanks to total-recall.

carl_caulkett

  • Hero Member
  • *****
  • Posts: 649
It seems that GetKeyState is not implemented on macOS. Cmd clicking the GetKeyState function call, just reveals this (non)implementation...
Code: Pascal  [Select][+][-]
  1. function TWidgetSet.GetKeyState(nVirtKey: Integer): Smallint;
  2. begin
  3.   Result := 0;
  4. end;
  5.  

TWidgetSet is a base class. The macOS-specific implementation is in:

* lcl/interfaces/cocoa/cocoawinapi.inc
* lcl/interfaces/cocoa/cocoawinapih.inc

I wondered about this. The only conclusion is that the macOS-specific code in the units you mentioned does not work.

Surely using the Cmd-Click operation to view the declaration of the expression should take account of OS $DEFINES and base and subclasses, then. Or maybe this is an indication of why GetKeyState doesn't work in this case; when it is invoked, the code is not being routed though to cocoawinapi.inc and cocoawinapih.inc...

« Last Edit: November 24, 2024, 08:23:32 pm by carl_caulkett »
"It builds... ship it!"

Handoko

  • Hero Member
  • *****
  • Posts: 5382
  • My goal: build my own game engine using Lazarus
One last question. The right-click to "Find Declaration of GetKeyState" is not working.

Use Ctrl Click.

Learn more Lazarus IDE tricks here:
https://wiki.freepascal.org/New_IDE_features_since
« Last Edit: November 24, 2024, 08:18:51 pm by Handoko »

carl_caulkett

  • Hero Member
  • *****
  • Posts: 649
One last question. The right-click to "Find Declaration of GetKeyState" is not working.

Use Ctrl Click.

Or Cmd-Click if you are on a Mac  ;)
"It builds... ship it!"

 

TinyPortal © 2005-2018