2012-06-15 76 views
4

有沒有辦法在常見的Apple筆記本電腦上模擬媒體按鍵的按鍵(音量增大/減小,播放,暫停,上一個,下一個)?在Mac上模擬媒體按鍵

怎麼樣?

回答

9

這需要花費一些時間和許多竅門(嘗試使用ctypes,IOKit本機接口,Quartz和/或Cocoa)。這似乎是目前一個簡單的解決方案:

#!/usr/bin/python 

import Quartz 

# NSEvent.h 
NSSystemDefined = 14 

# hidsystem/ev_keymap.h 
NX_KEYTYPE_SOUND_UP = 0 
NX_KEYTYPE_SOUND_DOWN = 1 
NX_KEYTYPE_PLAY = 16 
NX_KEYTYPE_NEXT = 17 
NX_KEYTYPE_PREVIOUS = 18 
NX_KEYTYPE_FAST = 19 
NX_KEYTYPE_REWIND = 20 

def HIDPostAuxKey(key): 
    def doKey(down): 
     ev = Quartz.NSEvent.otherEventWithType_location_modifierFlags_timestamp_windowNumber_context_subtype_data1_data2_(
      NSSystemDefined, # type 
      (0,0), # location 
      0xa00 if down else 0xb00, # flags 
      0, # timestamp 
      0, # window 
      0, # ctx 
      8, # subtype 
      (key << 16) | ((0xa if down else 0xb) << 8), # data1 
      -1 # data2 
      ) 
     cev = ev.CGEvent() 
     Quartz.CGEventPost(0, cev) 
    doKey(True) 
    doKey(False) 

for _ in range(10): 
    HIDPostAuxKey(NX_KEYTYPE_SOUND_UP) 
HIDPostAuxKey(NX_KEYTYPE_PLAY) 

(雖然我需要這在Python現在,我的問題沒有得到真正的Python相關的,當然你可以很容易地轉換,爲任何其他語言,尤其ObjC。)

5

謝謝Albert!我對你的腳本進行了一些擴展,使其成爲一個可執行文件,可以由Quicksilver或另一個啓動器/觸發器處理程序調用。

#!/usr/bin/python 

# CLI program to control the mediakeys on OS X. Used to emulate the mediakey on a keyboard with no such keys. 
# Easiest used in combination with a launcher/trigger software such as Quicksilver. 
# Main part taken from http://stackoverflow.com/questions/11045814/emulate-media-key-press-on-mac 
# Glue to make it into cli program by Fredrik Wallner http://www.wallner.nu/fredrik/ 

import Quartz 
import sys 

# NSEvent.h 
NSSystemDefined = 14 

# hidsystem/ev_keymap.h 
NX_KEYTYPE_SOUND_UP = 0 
NX_KEYTYPE_SOUND_DOWN = 1 
NX_KEYTYPE_PLAY = 16 
NX_KEYTYPE_NEXT = 17 
NX_KEYTYPE_PREVIOUS = 18 
NX_KEYTYPE_FAST = 19 
NX_KEYTYPE_REWIND = 20 

supportedcmds = {'playpause': NX_KEYTYPE_PLAY, 'next': NX_KEYTYPE_NEXT, 'prev': NX_KEYTYPE_PREVIOUS, 'volup': NX_KEYTYPE_SOUND_UP, 'voldown': NX_KEYTYPE_SOUND_DOWN} 

def HIDPostAuxKey(key): 
    def doKey(down): 
     ev = Quartz.NSEvent.otherEventWithType_location_modifierFlags_timestamp_windowNumber_context_subtype_data1_data2_(
      NSSystemDefined, # type 
      (0,0), # location 
      0xa00 if down else 0xb00, # flags 
      0, # timestamp 
      0, # window 
      0, # ctx 
      8, # subtype 
      (key << 16) | ((0xa if down else 0xb) << 8), # data1 
      -1 # data2 
      ) 
     cev = ev.CGEvent() 
     Quartz.CGEventPost(0, cev) 
    doKey(True) 
    doKey(False) 

if __name__ == "__main__": 
    try: 
     command = sys.argv[1] 
     assert(command in supportedcmds) 
     HIDPostAuxKey(supportedcmds[command]) 
    except (IndexError, AssertionError): 
     print "Usage: %s command" % (sys.argv[0],) 
     print "\tSupported commands are %s" % supportedcmds.keys() 

該腳本可以在https://gist.github.com/4078034

+0

用它來與[ControllerMate(http://www.orderedbytes.com/controllermate/)以模擬鍵盤上的媒體鍵很成功找到沒有他們,但有額外的F鍵。這很棒,因爲它不需要單獨調用每個應用程序。 – sordid