Applescript 按住 ⌘ F2 5 秒钟? [英] Applescript Press and hold ⌘ F2 for 5 seconds?
问题描述
我基本上是想弄清楚这一点,因为我想将 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
).
一些可选的背景信息:
key up
和key down
命令虽然也需要System Events
上下文,但不会在System Events 中公开.sdef
,应用程序的字典(仅列出了key code
和keystroke
) - 这可能表明Apple 不正式支持它们.- 在 OS X 10.9.1 上(与在 OS X 10.8 上不同 - 不知道早期版本)有一个奇怪的错误,当您使用
key down
时,会发送一个额外的a"按键带有(keycode
说明符.) - 确定键码值的方法(从其他各种 SO 答案中收集,主要是此处):
- Key Codes,一款免费的 GUI 应用用于交互式使用 - 非常方便.
- 您系统上的以下头文件(十六进制格式的代码列表):
/System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/HIToolbox.framework/Versions/A/Headers/Events.h
- 十进制代码列表(不完整)一个>:
- The
key up
andkey down
commands, while also requiring theSystem Events
context, are NOT exposed in theSystem Events.sdef
, the app's dictionary (onlykey code
andkeystroke
are listed) - this may indicate that Apple doesn't officially support them. - On OS X 10.9.1 (unlike on OS X 10.8 - don't know about earlier versions) there is a bizarre bug where an extra "a" keypress is sent whenever you use
key down
with a(keycode <n>)
specifier. - Ways of determining key-code values (gleaned from various other SO answers, mostly here):
- Key Codes, a free GUI app for interactive use - very handy.
- The following header file on your system (list of codes in hex format):
/System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/HIToolbox.framework/Versions/A/Headers/Events.h
- List of decimal codes (incomplete):
这篇关于Applescript 按住 ⌘ F2 5 秒钟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!