Applescript 按住 ⌘ F2 5 秒钟? [英] Applescript Press and hold ⌘ F2 for 5 seconds?

查看:17
本文介绍了Applescript 按住 ⌘ F2 5 秒钟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上是想弄清楚这一点,因为我想将 iMac 用作 macbook air 的外接显示器.我也想在我的 macbook air 上使用 iMac 键盘,但是出于某种原因,Apple 决定一旦你按住 Command F2 激活目标显示模式(意味着它现在是一个外部显示器),与 iMac 配对的键盘就不能与 iMac 取消配对.

I'm basically trying to figure this out because I want to use my iMac as an external monitor for my macbook air. I also want to use the iMac keyboard for my macbook air however for some reason, Apple has decided that once you press and hold Command F2 to activate Target Display Mode (meaning it is now an external monitor) that the keyboard paired with the iMac cannot be unpaired with the iMac.

为了解决这个问题,我想我最初只是将键盘与 macbook air 配对(让 iMac 没有键盘)并创建一个 Applescript 宏来模拟键盘按下并按住 Command F2 5 秒钟,从而消除了需要去买另一个苹果键盘.

To work around this I thought I would just pair the keyboard with the macbook air initially (leaving the iMac without a keyboard) and create an Applescript macro that would simulate the keyboard pressing and holding the Command F2 for five seconds eliminating the need to go buy another Apple keyboard.

这是我到目前为止所拥有的,但它不起作用.它告诉我 F2 不对.我很确定 F2 的键码是 120.

Here's what I have so far and it doesn't work. It's telling me F2 is not right. I'm pretty sure F2's key code is 120.

tell application "System Events"
     key down Command
     key down F2
     delay 5
     key up Command
     key up F2
end tell

有谁知道我如何做到这一点?

Does anyone know how I might accomplish this?

推荐答案

从 OS X 10.9.1 开始的观察:

Observations as of OS X 10.9.1:

您发送 F2 的方式有问题(您需要使用 (key code 120) 而不是 120),但更大的问题是 key up/down 只能按预期使用 modifier 键.

There's a problem with the way you're sending F2 (you need to use (key code 120) instead of just 120), but the larger problem is that key up/down only works as expected with modifier keys.

虽然可以发送非修饰键(使用(key code <n>)语法),但忽略向上/向下方面强>,使 key down (key code )key up (key code ) 语句key code <n>(即,发送 Key Down 事件后紧跟 Key Up 事件).

While NON-modifier keys can be sent (using (key code <n>) syntax), the up / down aspect is ignored, making both key down (key code <n>) and key up (key code <n>) statements effectively the same as key code <n> (i.e., a Key Down event immediately followed by a Key Up event is sent).

有一个建议的解决方法这里,基于以短序列重复发送击键 - 值得一试,但从从技术角度来看,这与保持键[组合]按住不同,所以我不确定它会起作用.

There's a suggested workaround here, based on repeatedly sending keystrokes in short sequence - it's worth a try, but from a technical perspective it's not the same as keeping a key [combination] held down, so I'm not sure it'll work.

根据您的情况(并将key down 替换为key code),我们得到:

Adapted to your situation (and replacing key down with key code), we get:

tell application "System Events"
    set now to the seconds of the (current date)
    set later to now + 5
    if later > 60 then set later to later - 60
    key down command
    # Workaround: send F2 repeatedly.
    repeat while the seconds of the (current date) is not later
        key code 120
    end repeat
    key up command
end tell

正如我所说:这可能行不通;还请注意,循环是紧密的",这意味着它会使您的机器非常忙(如果重复发送密钥,但不一定尽可能快是一种选择,您可以插入一个简短的 delay).

As I said: this may not work; also note that the loop is "tight" meaning that it'll make your machine pretty busy (if sending keys repeatedly, but not necessarily as fast as possible is an option, you could insert a short delay).

一些可选的背景信息:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆