Cocoa - 捕获鼠标就像screencapture实用程序一样(Cocoa - capture mouse just like screencapture utility)
我试图找到一个基本上让我疯狂的话题的答案。 有没有办法像Apple的screencapture实用工具那样捕获鼠标指针? 我想要做的是要求我绘制一个清晰的窗口,不会激活我的应用程序,而是覆盖所有内容,包括Dock。 我见过的唯一软件是使用命令+ shift + 4进行屏幕捕获。
我已经尝试创建一个非激活的NSPanel但它不能用于光标,除非我的应用程序处于活动状态(我不希望它)。 另外NSPanel的问题是,如果我在仪表板中,我无法激活它上面的窗口。
我想要做的是这个要求。 •我的应用程序不得激活(将任何活动应用程序保持为活动状态)•必须能够更改NSCursor•鼠标不会触发停靠隐藏/取消隐藏(或任何其他事件)•甚至可以在仪表板中工作
我之前尝试过此代码,但其限制与上面列出的相同。
NSScreen * s = [NSScreen mainScreen]; [self.window setStyleMask:NSNonactivatingPanelMask]; [self.window setHasShadow:NO]; [self.window setOpaque:NO]; [self.window setBackgroundColor:[NSColor clearColor]]; [self.window setFrame:s.frame display:NO]; [self.window orderFront:self]; [self.window setLevel:NSMainMenuWindowLevel + 1]; [self.trackingView addCursorRect:self.window.frame cursor:[NSCursor closedHandCursor]];
I am trying to find the answer to a topic which has basically driven me insane. Is there a way to capture the mouse pointer much like Apple's screencapture utility works? What I am trying to do requires me to draw a clear window that does not activate my app but instead overlays everything, including the dock. The only software I have seen do this is screencapture by utilizing command+shift+4.
I have tried creating a non-activating NSPanel but it doesn't work with cursor rects unless my app is active (which I don't want it to be). Also the problem with NSPanel is that if I am in Dashboard I cannot activate the window on it.
The requirements for what I am trying to do are this. • My app must not activate (keep whatever active app as active) • Must be able to change NSCursor • Mouse does not trigger dock hiding/unhiding (or any other event) • Work even in dashboard
I have tried this code before but its limitations are the same as listed above.
NSScreen * s = [NSScreen mainScreen]; [self.window setStyleMask:NSNonactivatingPanelMask]; [self.window setHasShadow:NO]; [self.window setOpaque:NO]; [self.window setBackgroundColor:[NSColor clearColor]]; [self.window setFrame:s.frame display:NO]; [self.window orderFront:self]; [self.window setLevel:NSMainMenuWindowLevel + 1]; [self.trackingView addCursorRect:self.window.frame cursor:[NSCursor closedHandCursor]];
原文:https://stackoverflow.com/questions/19646681
最满意答案
你可以使用字典的
pop
方法:for key in expected_keys: foo.__dict__[key] = d.pop(key) foo.additional_attributes(**d)
You can use the
pop
method of dictionaries:for key in expected_keys: foo.__dict__[key] = d.pop(key) foo.additional_attributes(**d)
相关问答
更多-
用pythonic的方式在字典中打印最大值的键[复制](Print the key of the max value in a dictionary the pythonic way [duplicate])[2022-02-28]
print max(d.keys(), key=lambda x: d[x]) 甚至更短(来自评论): print max(d, key=d.get) print max(d.keys(), key=lambda x: d[x]) or even shorter (from comment): print max(d, key=d.get) -
您可以使用update 。 由于早期的dicts具有优先级,您必须以相反的顺序更新,并使用last更新: def merge_dicts(a, *dict_args): d = {} for dictionary in reversed(dict_args): d.update(dictionary) d.update(a) return d 或者作为一个单行,使用itertools.chain : from itertools import chain ...
-
使用字典理解: attributes = {x['Name']: x['Value'] for x in verbose_attributes} Use a dictionary-comprehension: attributes = {x['Name']: x['Value'] for x in verbose_attributes}
-
可能更具可读性: def info(self, msg, *args, **kwargs): if 'extra' not in kwargs: kwargs['extra'] = dict() kwargs['extra']['session_id'] = self.session_id Possibly a bit more readable: def info(self, msg, *args, **kwargs): if 'extra' not in kwa ...
-
Pythonic字典遍历(Pythonic dictionary traversal)[2022-10-30]
你可以使用字典的pop方法: for key in expected_keys: foo.__dict__[key] = d.pop(key) foo.additional_attributes(**d) You can use the pop method of dictionaries: for key in expected_keys: foo.__dict__[key] = d.pop(key) foo.additional_attributes(**d) -
你的方法会完成它......但我不喜欢读整个文件并创建一个数据帧来计算列数。 你可以通过阅读每个文件的第一行并计算逗号数来做同样的事情。 请注意,我添加1是因为总是会有一个比列有更少的逗号。 columns = [open(f).readline().count(',') + 1 for _, f in directory.items()] Your method will get it done... but I don't like reading in the entire file and cre ...
-
我不是特别相信这是否更加pythonic ...也许更有效率? 不知道,真的 pos = [1, 2, 3, 4] neg = [5, 6, 7, 8] def vectorize(pos, neg): vec = dict.fromkeys(pos, 1) vec.update(dict.fromkeys(neg, 0)) return vec print vectorize(pos, neg) 输出: {1: 1, 2: 1, 3: 1, 4: 1, 5: 0, 6: 0, ...
-
一个简单的解决方案是保留一组“已访问”的已知节点: def reach(travel_dict, x, visited=None): if visited is None: visited = set() # see note visited.add(x) for y in travel_dict.get(x, []): if y not in visited: yield y for z in reac ...
-
你可以使用词典理解: my_dict = {1: [2,3], 2: [4, 5], 3: [6, 7]} new_d = {a:sum(b) for a, b in my_dict.items()} 输出: {1: 5, 2: 9, 3: 13} You can use a dictionary comprehension: my_dict = {1: [2,3], 2: [4, 5], 3: [6, 7]} new_d = {a:sum(b) for a, b in my_dict.items()} ...
-
你总是可以做类似子类dict事情。 这是完全未经测试的,但有一些影响: class NoisyDict(dict): def __init__(self, *args, **kwargs): self.handlers = {} #Python 3 style super().__init__(*args, **kwargs) def add_handler(self, key, callback): self.handler ...