Recent

Author Topic: OS X: Menu and Beep on KeyDown  (Read 1379 times)

Key-Real

  • New Member
  • *
  • Posts: 21
OS X: Menu and Beep on KeyDown
« on: May 02, 2021, 07:40:48 pm »
Hi,

I have two Issues under Mac OS.

First: If I start the app, the menu is not usable(you can not click on it). Than I change to another Window, and go back then, I can press on the menu(It works how expected).

Second: If I press a Key, the system beep sounds. I trace the Keyboard in TCocoaApp.poll (not included in the test), how to not beep ?





Code: Pascal  [Select][+][-]
  1.  
  2. {$mode objfpc}
  3. {$modeswitch objectivec1}
  4.  
  5. uses CocoaAll, ctypes, MacOSAll;
  6.  
  7. type
  8.       TCocoaApp = objcclass (NSApplication)
  9.           procedure poll; message 'poll';
  10.       end;
  11.  
  12.       TCocoaAppDelegate = objcclass (NSObject, NSApplicationDelegateProtocol)
  13.       end;
  14.  
  15.  
  16. procedure TCocoaApp.poll;
  17. var
  18.   event: NSEvent;
  19.   pool: NSAutoreleasePool;
  20. begin
  21.   pool := NSAutoreleasePool.alloc.init;
  22.   event := nextEventMatchingMask_untilDate_inMode_dequeue(NSAnyEventMask, {NSDate.distantPast}nil, NSDefaultRunLoopMode, true);
  23.   if event <> nil then begin
  24.       sendEvent(event);
  25.       updateWindows;
  26.   end;
  27.   pool.release;
  28. end;
  29.  
  30.  
  31. procedure SetupMainMenu;
  32.  
  33.   procedure AddMenu (menu: NSMenu);
  34.   var
  35.     menuItem: NSMenuItem;
  36.   begin
  37.     menuItem := NSMenuItem.alloc.initWithTitle_action_keyEquivalent(menu.title, nil, NSSTR('')).autorelease;
  38.     menuItem.setSubmenu(menu);
  39.     NSApp.mainMenu.addItem(menuItem);
  40.   end;
  41.  
  42. var
  43.   mainMenu: NSMenu;
  44.   appleMenu: NSMenu;
  45.   windowMenu: NSMenu;
  46. begin
  47.   // main menu
  48.   mainMenu := NSMenu.alloc.init.autorelease;
  49.   NSApp.setMainMenu(mainMenu);
  50.   // apple menu
  51.   appleMenu := NSMenu.alloc.initWithTitle(NSSTR('')).autorelease;
  52.   appleMenu.addItemWithTitle_action_keyEquivalent(NSSTR('Quit '+NSProcessInfo.processInfo.processName.UTF8String), objcselector('terminate:'), NSSTR('q'));
  53.   AddMenu(appleMenu);
  54.   // window menu
  55.   windowMenu := NSMenu.alloc.initWithTitle(NSSTR('Window')).autorelease;
  56.   windowMenu.addItemWithTitle_action_keyEquivalent(NSSTR('Minimize'), objcselector('performMiniaturize:'), NSSTR('m'));
  57.   windowMenu.addItemWithTitle_action_keyEquivalent(NSSTR('Zoom'), objcselector('performZoom:'), NSSTR(''));
  58.   AddMenu(windowMenu);
  59. end;
  60.  
  61. procedure Cocoa_Init;
  62. var
  63.   pool: NSAutoreleasePool;
  64.   app: TCocoaApp;
  65.   delegate: TCocoaAppDelegate;
  66. begin  
  67.  
  68.   // https://hero.handmade.network/forums/code-discussion/t/1409-main_game_loop_on_os_x
  69.  
  70.   pool := NSAutoreleasePool.alloc.init;
  71.  
  72.   app := TCocoaApp(TCocoaApp.sharedApplication);
  73.  
  74.   delegate := TCocoaAppDelegate.alloc.init;
  75.   app.setDelegate(delegate);
  76.  
  77.   NSApp.setActivationPolicy(NSApplicationActivationPolicyRegular);
  78.   NSApp.activateIgnoringOtherApps(true);
  79.  
  80.   if NSApp.mainMenu = nil then SetupMainMenu;
  81.  
  82.   app.finishLaunching;
  83.   pool.release;
  84. end;
  85.  
  86.  
  87. procedure Cocoa_PollEvents;
  88. begin
  89.   // note: do we really need to call on main thread? isn't the first thread "main"?
  90.   TCocoaApp(TCocoaApp.sharedApplication).poll;
  91.   NSApp.performSelectorOnMainThread_withObject_waitUntilDone(objcselector('poll'), nil, true);
  92. end;
  93.  
  94.  
  95. procedure Cocoa_CreateWindow(width, height: integer; title: PChar);
  96. const
  97.   NSWindowCollectionBehaviorFullScreenPrimary = 1 shl 7 { available in 10_7 };
  98. var
  99.   window: NSWindow;
  100.   windowFlags: NSUInteger = 0;
  101. begin
  102.   windowFlags := NSTitledWindowMask + NSClosableWindowMask + NSMiniaturizableWindowMask + NSResizableWindowMask;
  103.   window := NSWindow.alloc.initWithContentRect_styleMask_backing_defer(NSMakeRect(123, 123, width, height ), windowFlags, NSBackingStoreBuffered, false);
  104.   window.setTitle(NSSTR(title));
  105.   window.setCollectionBehavior(NSWindowCollectionBehaviorFullScreenPrimary);
  106.   window.setAcceptsMouseMovedEvents(true);
  107.   window.makeKeyAndOrderFront(nil);
  108. end;
  109.  
  110.  
  111.  begin
  112.   Cocoa_Init;
  113.   Cocoa_CreateWindow(640,480,'test');
  114.   repeat
  115.     Cocoa_PollEvents;  
  116.   until false;
  117.  end.
  118.  
  119.  
  120.  


thx

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1504
  • Former Delphi 1-7, 10.2 user
Re: macOS: Menu and Beep on KeyDown
« Reply #1 on: May 03, 2021, 01:31:44 am »
1. Did you create and populate an application bundle for your executable?

2. See Prevent "not allowed" beep after keystroke.
Lazarus 2.1 r65061 FPC 3.3.1 r49223 macOS 10.14.6 Xcode 11.3.1
Lazarus 2.3 r65500 FPC 3.3.1 r49634 macOS 11.5 aarch64 Xcode 12.5.1
Lazarus 2.1 r61574 FPC 3.3.1 r42318 FreeBSD 12.1 amd64 VMware VM
Lazarus 2.1 r61574 FPC 3.0.4 Ubuntu 20.04 Parallels VM
Lazarus 2.0.10 FPC 3.2.0 Win10 Parallels VM

 

TinyPortal © 2005-2018