问题
I have set up a global hotkey with RegisterEventHotkey
. When the user presses it, it gets the currently focused window with CGWindowListCopyWindowInfo
, and then I need to set it always on top.
If the current window is in my process (from which I am executing the code) I can simply convert the windowNumber
from CGWindowListCopyWindowInfo
to a NSWindow
and do setLevel
:
nswin = [NSApp windowWithWindowNumber:windowNumber]
[nswin setLevel: Int(CGWindowLevelForKey(kCGFloatingWindowLevelKey))]
My Problem I am not able to do this if the currently focused window is not in my process. Can you please show me how?
Stuff I tried:
- This app here named "Afloat" used "SIMBL" to accomplish this. From any window you can hit Cmd + A and it will set always on top. However I am trying to do with C/ObjC from my normal desktop app without the use of helpers like SIMBL.
- Source: Force keeping app window on top - Mac OS X
- SIMBL: http://www.culater.net/software/SIMBL/SIMBL.php
- Afloat: https://www.macupdate.com/app/mac/22237/afloat
I come across
CGSSetWindowLevel
inCGPrivate.h
- undocumented stuff - https://gist.github.com/Noitidart/3664c5c2059c9aa6779f#file-cgsprivate-h-L63 - However I recall I tried something like this in the past but would get an error as I tried to connect to a window that wasn't in the calling process.It says here - https://github.com/lipidity/CLIMac/blob/114dfee39d24809f62ccb000ea22dfda15c11ce8/src/CGS/CGSInternal/.svn/text-base/CGSConnection.h.svn-base#L82
Only the owner of a window can manipulate it. So, Apple has the concept of a universal owner that owns all windows and can manipulate them all. There can only be one universal owner at a time (the Dock).
Maybe, is there anyway to pretend for my calling process to temporarily be the dock? Maybe
CGSGetConnectionIDForPSN
for the dock then use that connection?
My use: I'm trying to replicate the functionality my open source, free, browser addon - https://addons.mozilla.org/en-US/firefox/addon/topick/ - so my calling process if Firefox. It works on Windows and Linux right now, and just need to figure out how to do it in mac for non-Firefox windows.
来源:https://stackoverflow.com/questions/36075213/set-non-owned-window-always-on-top-like-the-app-afloat