[Solved] Cannot detect key[Down|Press|Up] activity in Lazarus on MacOS X 11-14 lazarus / 31-LCL ---------------------------------------------------------------------- BradleySlavik · 2024-11-23 23:59 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? ---------------------------------------------------------------------- jamie · 2024-11-24 00:24 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 ---------------------------------------------------------------------- dseligo · 2024-11-24 00:26 Try this: go to Menu/Project Options, Application, and then you have button Create Application Bundle. ---------------------------------------------------------------------- BradleySlavik · 2024-11-24 01:37 Quote from: jamie on November 24, 2024, 01:24:54 amdoing 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 · 2024-11-24 01:42 Quote from: dseligo on November 24, 2024, 01:26:07 amTry 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 · 2024-11-24 01:55 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 · 2024-11-24 11:37 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.h tml#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.h tml#msg425440 ---------------------------------------------------------------------- carl_caulkett · 2024-11-24 12:20 Quote from: Handoko on November 24, 2024, 12:37:58 pmI 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.h tml#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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TWidgetSet.GetKeyState(nVirtKey: Integer): Smallint;begin  Result := 0;end;  However, your suggestion of using the OnKeyUp and OnKeyDown events works fine! ---------------------------------------------------------------------- Handoko · 2024-11-24 12:54 Thank you for reporting it. So, GetKeyState is an unimplemented feature in MacOS. ---------------------------------------------------------------------- dsiders · 2024-11-24 17:28 Quote from: carl_caulkett on November 24, 2024, 01:20:12 pmIt seems that GetKeyState is not implemented on macOS. Cmd clicking the GetKeyState function call, just reveals this (non)implementation... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TWidgetSet.GetKeyState(nVirtKey: Integer): Smallint;begin  Result := 0;end;  TWidgetSet is a base class. The macOS-specific implementation is in: * lcl/interfaces/cocoa/cocoawinapi.inc * lcl/interfaces/cocoa/cocoawinapih.inc ---------------------------------------------------------------------- BradleySlavik · 2024-11-24 17:41 Quote from: carl_caulkett on November 24, 2024, 01:20:12 pmQuote from: Handoko on November 24, 2024, 12:37:58 pmI 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.h tml#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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TWidgetSet.GetKeyState(nVirtKey: Integer): Smallint;begin  Result := 0;end;  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 · 2024-11-24 17:57 Quote from: BradleySlavik on November 24, 2024, 06:41:48 pmAnd 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. ---------------------------------------------------------------------- carl_caulkett · 2024-11-24 19:12 Quote from: dsiders on November 24, 2024, 06:28:23 pmQuote from: carl_caulkett on November 24, 2024, 01:20:12 pmIt seems that GetKeyState is not implemented on macOS. Cmd clicking the GetKeyState function call, just reveals this (non)implementation... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TWidgetSet.GetKeyState(nVirtKey: Integer): Smallint;begin  Result := 0;end;  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... ---------------------------------------------------------------------- Handoko · 2024-11-24 19:13 Quote from: BradleySlavik on November 24, 2024, 06:41:48 pmOne 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 ---------------------------------------------------------------------- carl_caulkett · 2024-11-24 19:24 Quote from: Handoko on November 24, 2024, 08:13:56 pmQuote from: BradleySlavik on November 24, 2024, 06:41:48 pmOne 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 ---------------------------------------------------------------------- BradleySlavik · 2024-11-24 21:11 Quote from: carl_caulkett on November 24, 2024, 08:24:46 pmQuote from: Handoko on November 24, 2024, 08:13:56 pmQuote from: BradleySlavik on November 24, 2024, 06:41:48 pmOne 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  I understand what you are saying. Command-click (left-click) does nothing. Command-(right-click) produces pop-up menu that says "Close Page" at top, and "Other tabs" at bottom Right-click produces menu that has "Find Declaration of ShowMessage" (in this case) and when I choose that I get unit1.pas Error: unit not found StdCtrls and the uses line is highlighted. It does not take me to the declaration or definition of ShowMessage or GetKeyState when I click on it. I would like to have that work. I have the source of FreePascal unzipped in a directory pointed to by the Lazarus->Preferences->Environment->Files->FPC Source Directory entry. Do I need to look at a setting for Lazarus? It is currently installed in /Applications/Lazarus. Will that work? Do I need to set something in fpc.cfg or somewhere else? I am not being shown any procedure or function declarations or definitions. If it is possiible, I would like to fix this by adjusting my configuration. I am just starting to learn Lazarus, and there are things that are probably obvious to you, that I have not figured out yet. Thanks! ---------------------------------------------------------------------- BradleySlavik · 2024-11-24 21:28 Quote from: BradleySlavik on November 24, 2024, 10:11:15 pmQuote from: carl_caulkett on November 24, 2024, 08:24:46 pmQuote from: Handoko on November 24, 2024, 08:13:56 pmQuote from: BradleySlavik on November 24, 2024, 06:41:48 pmOne 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  I understand what you are saying. Command-click (left-click) does nothing. OK. I rebooted that system. Command-click NOW takes me to function declaration. Something must have been messed up. I will keep looking for how to get the the definition, so that I can see when functions are not implemented. Thank you all for helping me get started. I have been wanting to port some old software to many modern languages and platforms and it appears that Lazarus will be a great place for me to rewrite that code in Pascal. Bradley