Detect if the Fn key has been pressed lazarus / 21-General ---------------------------------------------------------------------- Adriaan van Os · 2024-10-22 09:05 There is no Fn modifier in TShiftStateEnum   TShiftStateEnum = (ssShift, ssAlt, ssCtrl,     ssLeft, ssRight, ssMiddle, ssDouble,     ssMeta, ssSuper, ssHyper, ssAltGr, ssCaps, ssNum,     ssScroll,ssTriple,ssQuad,ssExtra1,ssExtra2); This has been discussed before https://forum.lazarus.freepascal.org/in dex.php/topic,50245.msg366277.html#msg366277 The reply there was that this is handled by the hardware, the BIOS or whatever, modifying the scancode. I doubt that this is true on MacOS, as NSEvent.inc defines const   NSAlphaShiftKeyMask = 1 shl 16;   NSShiftKeyMask = 1 shl 17;   NSControlKeyMask = 1 shl 18;   NSAlternateKeyMask = 1 shl 19;   NSCommandKeyMask = 1 shl 20;   NSNumericPadKeyMask = 1 shl 21;   NSHelpKeyMask = 1 shl 22;   NSFunctionKeyMask = 1 shl 23;   NSDeviceIndependentModifierFlagsMask = $ffff0000; So, there actually is a modifier NSFunctionKeyMask on MacOS. And the standard View menu item Enter/Exit Full Screen has a keyboard equivalent that is either Control-Command-F or Fn-F. If it is Fn-F, it needs to be handled in the software. As a proof, here is the NSEvent when pressing Fn-F (lldb) po event NSEvent: type=KeyDown loc=(99.2148,494.188) time=10836.7 flags=0x800100 win=0x11e620bb0 winNum=602 ctxt=0x0 chars="f" unmodchars="f" repeat=0 keyCode=3 where flags does contain the NSFunctionKeyMask modifier. Regards, Adriaan van Os ---------------------------------------------------------------------- Thaddy · 2024-10-22 13:15 It works and it is one of ssMeta, ssSuper, ssHyper. No working mac here so simply try them. ---------------------------------------------------------------------- Adriaan van Os · 2024-10-22 13:53 In other words, it was hacked into another modifier, but we don't know which one ? Good software starts with clear names for identifiers and constants. So, that requires a constant ssFn = ....one of the others ...; Regards, Adriaan van Os ---------------------------------------------------------------------- Bart · 2024-10-22 17:37 Quote from: Thaddy on October 22, 2024, 03:15:36 pmIt works and it is one of ssMeta, ssSuper, ssHyper. No working mac here so simply try them. I don't think so... At leats Google and Wikipedia suggest otherwise. Bart ---------------------------------------------------------------------- Thaddy · 2024-10-23 08:02 Try this Bart, works also for the any key..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 program handlectrl;{msdn ctrl handler with hidden window example transation  if the gdi32.dll or user32.dll library are loaded, SetConsoleCtrlHandler does not get called for the CTRL_LOGOFF_EVENTand CTRL_SHUTDOWN_EVENT events. The workaround specified is to create a hidden window, if no window already exists, by calling the CreateWindowEx method with the dwExStyle parameter set to 0 and listen for the WM_QUERYENDSESSION and WM_ENDSESSION window messages. If a window already exists, add the two messages to the existing Window Procedure.}{$mode delphi}{$H+}{$modeswitch functionreferences}{$modeswitch anonymousfunctions} uses   Windows;  function  CtrlHandler(fdwCtrlType:dword):Boolean;winapi;begin  case fdwCtrlType of  // Handle the CTRL-C signal.  CTRL_C_EVENT:     begin        writeln('Ctrl-C event');        Beep(750, 300);        result := true;      end;   // CTRL-CLOSE: confirm that the user wants to exit.  CTRL_CLOSE_EVENT:    begin      Beep(600, 200);      writeln('Ctrl- Close event');      result := true;    end;      // Pass other signals to the next handler.  CTRL_BREAK_EVENT:    begin      Beep(900, 200);      writeln('Ctrl-Break event');      result := false;    end;  CTRL_LOGOFF_EVENT:    begin      Beep(1000, 200);      writeln('Ctrl- Logoff event');      result := false;    end;  CTRL_SHUTDOWN_EVENT:  begin      Beep(750, 500);      writeln('Ctrl-Shutdown event');      result := false;    end;  else    result := false;  end;end;  function WindowProc(aHwnd:HWND;uMsg:Uint;awParam:WPARAM;alParam:LPARAM):Boolean ;winapi;begin  case uMsg of  WM_QUERYENDSESSION:    begin      // Check `lParam` for which system shutdown function and handle events.    // See https://learn.microsoft.com/windows/win32/shutdown/wm- queryendsession      writeln('Gonna close the app');      result := TRUE; // Respect user's intent and allow shutdown.    end;  WM_ENDSESSION:    begin      // Check `lParam` for which system shutdown function and handle events.      // See https://learn.microsoft.com/windows/win32/shutdown/wm-endsession      result := false; // We have handled this message.      exit;    end;  WM_KEYUP:    begin      MessageBox(0, 'Hello world!', 'sometext', MB_OK or MB_USERICON);      Result :=true;          end;  end;  //else    result := DefWindowProc(aHwnd, uMsg, awParam, alParam) <> 0;  end;//end; var  wc:WNDCLASS;  cl:ATOM;  aHwnd:HWND;  msg:TMsg;begin  wc.lpszClassName := 'HiddenWindowClass';  wc.lpfnWndProc := @WindowProc;  cl := registerclass(wc);  if cl = 0 then  begin    writeln('Could not register class');    Halt(2);  end;  writeln('Class registered');  aHwnd := CreateWindowEx(        0,      wc.lpszClassName,        'Console Control Handler Sample',        0,        CW_USEDEFAULT,        CW_USEDEFAULT,        CW_USEDEFAULT,      CW_USEDEFAULT,        0,        0,        0,        nil    );   if aHwnd = 0 then  begin    writeln('ERROR: Could not create window');  halt(3);  end;  ShowWindow(aHwnd, SW_HIDE);  if SetConsoleCtrlHandler(@CtrlHandler, TRUE) then  begin    writeln('The Control Handler is installed.');    writeln(' -- Now try pressing Ctrl+C or Ctrl+Break, or');    writeln('    try logging off or closing the console...');    writeln('(...waiting in a loop for events...)');    // Pump message loop for the window we created.    while PtrInt(GetMessage(msg, 0, 0, 0)) > 0 do    begin         TranslateMessage(msg);      DispatchMessage(msg);    end;  end else  begin    writeln('ERROR: Could not set control handler');    halt(1); end;(*  if SetConsoleCtrlHandler(@CtrlHandler, true) then  begin    writeln('The Control Handler is installed.');    writeln(' -- Now try pressing Ctrl+C or Ctrl+Break, or');    writeln('    try logging off or closing the console...');    writeln('(...waiting in a loop for events...)');    while 1=1 do     begin       sleep(0);    end;  end else  begin    writeln('ERROR: Could not set control handler');    Halt(1);  end;  *)  UnRegisterClass(wc.lpszClassName,0);end.Global key handlers can also be installed on Linux, so if you insist I can prove you even more wrong. There is a slight provision for fn in that it depends on the hardware, though the above code also can show, with a slight modification, what the scan code is. ---------------------------------------------------------------------- Thaddy · 2024-10-23 09:17 On liinux, install libevdev-dev for similar fiuctionality. ---------------------------------------------------------------------- Thaddy · 2024-10-23 09:28 Forget the above. It is too hardware specific. Discovered after changing between laptops and th code for fn not working there. ---------------------------------------------------------------------- Adriaan van Os · 2024-10-23 14:57 I found zero occurrences of NSFunctionKeyMask in the source code of Lazarus 3.6 (except for the Cocoa headers and the fpc ptc package). If this is not correct, please point me to the right line of code. Regards, Adriaan van Os ---------------------------------------------------------------------- TRon · 2024-10-23 15:05 @Adriaan van Os I do not know anything about MacOS but why is the documentation stating NSFunctionKeyMask is deprecated ? There would be no benefit to add support for deprecated functionality so you might have to figure this one out yourself. Also, I am only able to read something about the numeric keypad in that doc.... ---------------------------------------------------------------------- Thaddy · 2024-10-23 15:54 According to a close friend and Mac user, but a C programmer at least one of those should work; ssMeta, ssSuper, ssHyper,ssTriple,ssQuad,ssExtra1,ssExtra2); on Apple hardware, but I can not test it anymore. Maybe ask Jonas? ---------------------------------------------------------------------- carl_caulkett · 2024-10-23 16:47 Quote from: Thaddy on October 23, 2024, 05:54:21 pmAccording to a close friend and Mac user, but a C programmer at least one of those should work; ssMeta, ssSuper, ssHyper,ssTriple,ssQuad,ssExtra1,ssExtra2); on Apple hardware, but I can not test it anymore. Maybe ask Jonas? I can confirm that the ssMeta key, in combination with other keys works with Linux. I'm putting together a cross-platform app in Lazarus and I have the Cmd+Comma combination working to bring up a settings page, and it works in macOS and Ubuntu Linux. ---------------------------------------------------------------------- dsiders · 2024-10-23 19:25 According to stack overflow (and other) answers, the Fn key does not generate a scan code. It is handled by the low-level keyboard drivers on platforms where it is implemented. Windows uses the Registry (of course it does). ---------------------------------------------------------------------- MarkMLl · 2024-10-23 21:58 This ain't my fight, but I'd suggest that the real issue is what the LCL does with that key. From having looked at keyboards in the context of trying to wring every possible combination out of the system to implement an APL terminal, I'd suggest that it's what the widget set reports that's important: and I've certainly seen different behaviour between gtk2 and Qt particularly when comparing IBM and Sun keyboards. It should not be necessary to consult a lower-level API, the use of which could potentially interfere with the operation of the widget set. If the LCL interprets different widget sets differently it's potentially a bug, or at least the subject of a feature request once the current and intended behaviours have been fully defined. MarkMLl ---------------------------------------------------------------------- dsiders · 2024-10-23 22:21 Quote from: MarkMLl on October 23, 2024, 11:58:38 pmThis ain't my fight, but I'd suggest that the real issue is what the LCL does with that key. . If Fn doesn't generate a distinct scan code, there is nothing LCL can do with it... as a virtual key code or as a shift modifier. Just my .02USD. ---------------------------------------------------------------------- Thaddy · 2024-10-24 04:32 Well, it does generate a code, but it is not distinct across machines/brands/bios. Which makes it unusable. (you can make it work on your computer, though) What I discovered yesterday is that it is detectable - of course, otherwise it would not "function" - but completely useless. ---------------------------------------------------------------------- PascalDragon · 2024-10-24 19:26 Quote from: dsiders on October 23, 2024, 09:25:19 pmAccording to stack overflow (and other) answers, the Fn key does not generate a scan code. It is handled by the low-level keyboard drivers on platforms where it is implemented. Windows uses the Registry (of course it does). It's more likely the Fn key is handled inside the hardware itself instead of the driver, because that is usually simply a generic HID driver. In that case there won't be any notification to the OS about the key.