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

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

问题描述

基本上,我试图想出解决办法,因为我想用我的iMac作为我的MacBook Air外接显示器。我也想不过使用iMac键盘为我的MacBook Air出于某种原因,苹果已决定,一旦你preSS和按住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宏,将模拟键盘pressing并按住Command F2五秒钟无需去购买另一苹果键盘。

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是不对的。我是pretty确保F2的主要code为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 (你需要使用(键code 120)而不仅仅是 120 ),但更大的问题是, 键,上/下只有作品如预期与的修改的钥匙。

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.

非组合键能(使用(键code< N>)语法)发送,在上/下方面被忽略,作键不放(键code< N>)键向上(关键code< N>)语句实际上是相同的键code< N> (即紧跟一个键向上事件键按下事件被发送)。

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.

适合您的情况(与替换键,键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

正如我所说的:这可能无法正常工作;还注意到,循环是从紧,这意味着它会让你的机器pretty忙(如果重复发送键,但不一定越快越好是一种选择,你可以插入一个短延迟)。

某些可选背景信息


  • 键向上键,命令,同时还要求系统事件的背景下,是不是在系统Events.sdef ,应用程序的字典(仅键code 击键列) - 这可能表明苹果不正式支持他们

  • 在OS X 10.9.1(不像在OS X 10.8 - 不知道的更早版本)存在将额外只要您使用A键preSS发送一个奇怪的错误键,(键code&LT; N&GT;)。说明

  • 确定键盘code值的方式(从其他多种SO答案收集,大多这里):

    • 重点codeS ,在交互使用一个免费的GUI应用程序 - 非常方便

    • 您的系统(十六进制格式的列表codeS)在下面的头文件:结果
      <$c$c>/System/Library/Frameworks/Carbon.framework/Versions/A/Frameworks/HIToolbox.framework/Versions/A/Headers/Events.h

    • 小数$一览C $ CS(不完全)

    • The key up and key down commands, while also requiring the System Events context, are NOT exposed in the System Events.sdef, the app's dictionary (only key code and keystroke 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的preSS按住⌘F2 5秒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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