向应用程序发送密钥代码而不先激活它?
我试图将空格键发送到应用程序而不先激活它。以下代码几乎满足了我的要求,但它首先将应用程序带到前台。
tell application "X"
activate
tell application "System Events" to key code 49
end tell
I'm trying to send the spacebar key to an application without activating it first. The following code almost does what I want but it brings the application to the foreground first.
tell application "X"
activate
tell application "System Events" to key code 49
end tell
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我认为您无法将击键发送到不活动的应用程序。
但是,您可以在激活应用程序并为其执行代码后立即隐藏该应用程序。但是,这确实会导致应用程序在隐藏之前短暂闪烁,除非应用程序以全屏运行。
I don't think you can send a keystroke to an inactive application.
But you can hide an app immediately after activating it and executing code for it. This does however, cause the app to briefly flash before it hides, unless the app is running in fullscreen.
发送击键基本上可以看作是使用键盘,但唯一的区别是需要按下的键已经被预定义了。围绕此的其余流程不会改变。这意味着应用程序本身仍然需要打开并激活,然后才能实际向其发送击键。
然而,根据应用程序的不同,可能可以使用应用程序 API 中的某些 Applescript 函数向应用程序发送不同的输入,而无需先激活它。以消息 API 为例:
Sending a keystroke can basically be seen as using a keyboard, but the only difference is that the keys that need to be pressed are already predefined. The rest of the process revolving around this doesn't change. This means that the application itself still needs to be opened and activated before you can actually send keystrokes to it.
Depending on the application however, it might be possible to use certain Applescript functions in the application's API to send different inputs to the application without having to activate it first. Take the Messages API for instance:
现在有一个特定的命令工具,很容易处理
https://github.com/socsieng/sendkeys
There is now a specific command tool that is easy to deal with
https://github.com/socsieng/sendkeys
之后停用它怎么样?
How about deactivating it afterwards?
位置位于屏幕外,以防止其在用户可以看到的地方闪烁或降低其不透明度。
Position is offscreen to prevent it from flashing where the user can see it or reduce its opacity.
有调查课程。
如果应用程序是您自己开发的,您有以下选择:
*只需公开一个公共属性并将其设置为您要发送的密钥即可。
*让您的应用程序轮询文件夹中的文件,然后通过该文件夹发送指令。
使用某种类型的 Windows API 挂钩,您无需激活应用程序即可获得应用程序的控制权。我非常确定,如果我下定决心,我可以控制计算机上的任何内容。
简单来说,开箱即用,它不一定需要你发送一个按键,你只是想指示它做某事。有大量的选项 Interface Marshalling、Interops、OLE、DDE,看起来我正好及时出现在这个网站上!
There are courses of investigation.
If the application is one you have developed yourself you have the following options:
*simply have a public property exposed and set that to the key you want to send it.
*have your application polling a folder for a file and you send your instructions via that.
With a windows API hook of some type you can get control of the application without activating it. I am pretty certain if I put my mind to it I could take control of anything on the computer.
In simple terms think out of the box, it does not necessarily need to be a key press you send, you just want to instruct it to do something. There is loads of options Interface Marshalling, Interops, OLE, DDE, looks like I have turned up on this site just in time!