向应用程序发送密钥代码而不先激活它?

发布于 2024-10-17 15:25:19 字数 177 浏览 4 评论 0原文

我试图将空格键发送到应用程序而不先激活它。以下代码几乎满足了我的要求,但它首先将应用程序带到前台。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

眼眸 2024-10-24 15:25:19

我认为您无法将击键发送到不活动的应用程序。

但是,您可以在激活应用程序并为其执行代码后立即隐藏该应用程序。但是,这确实会导致应用程序在隐藏之前短暂闪烁,除非应用程序以全屏运行。

tell application "System Events"
    tell application "X" to activate
    key code 49
    set visible of process "X" to false
end tell

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.

tell application "System Events"
    tell application "X" to activate
    key code 49
    set visible of process "X" to false
end tell
已下线请稍等 2024-10-24 15:25:19

发送击键基本上可以看作是使用键盘,但唯一的区别是需要按下的键已经被预定义了。围绕此的其余流程不会改变。这意味着应用程序本身仍然需要打开并激活,然后才能实际向其发送击键。

然而,根据应用程序的不同,可能可以使用应用程序 API 中的某些 Applescript 函数向应用程序发送不同的输入,而无需先激活它。以消息 API 为例:

tell application "Messages"
      set theBuddy to buddy "[email protected]" of service "iMessage"
      send "Hi there" to theBuddy
end tell

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:

tell application "Messages"
      set theBuddy to buddy "[email protected]" of service "iMessage"
      send "Hi there" to theBuddy
end tell
节枝 2024-10-24 15:25:19

现在有一个特定的命令工具,很容易处理
https://github.com/socsieng/sendkeys

There is now a specific command tool that is easy to deal with
https://github.com/socsieng/sendkeys

相权↑美人 2024-10-24 15:25:19

之后停用它怎么样?

activate application "X"
tell application "System Events" to key code 49
activate me

How about deactivating it afterwards?

activate application "X"
tell application "System Events" to key code 49
activate me
空城仅有旧梦在 2024-10-24 15:25:19

位置位于屏幕外,以防止其在用户可以看到的地方闪烁或降低其不透明度。

Position is offscreen to prevent it from flashing where the user can see it or reduce its opacity.

北方。的韩爷 2024-10-24 15:25:19

有调查课程。

  1. 如果应用程序是您自己开发的,您有以下选择:

    *只需公开一个公共属性并将其设置为您要发送的密钥即可。
    *让您的应用程序轮询文件夹中的文件,然后通过该文件夹发送指令。

  2. 使用某种类型的 Windows API 挂钩,您无需激活应用程序即可获得应用程序的控制权。我非常确定,如果我下定决心,我可以控制计算机上的任何内容。

简单来说,开箱即用,它不一定需要你发送一个按键,你只是想指示它做某事。有大量的选项 Interface Marshalling、Interops、OLE、DDE,看起来我正好及时出现在这个网站上!

There are courses of investigation.

  1. 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.

  2. 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文