title: Applescript Press and hold ⌘ F2 for 5 seconds?
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <pre><code>tell application "System Events"
       key down Command
       key down F2
       delay 5
       key up Command
       key up F2
  end tell
  </code></pre>
  
  <p>Does anyone know how I might accomplish this?</p>