我想在Python中爲OSX寫一個簡單的宏記錄器 - 當腳本在後臺運行並重播它們時,它可以捕獲鼠標和鍵盤事件。後者可以使用autopy,前者有沒有類似的簡單庫?我可以使用Python捕獲OSX中的鍵盤和鼠標事件嗎?
回答
在OSX上似乎沒有辦法在Python上這樣做。
我知道你可以使用curses
來捕獲按鍵輸入,但我不確定鼠標輸入。不僅如此,但如果我沒有把它誤認爲包含在2.7.2的std庫中。
這裏有一個解決方案,而無需使用curses
:
http://docs.python.org/faq/library.html#how-do-i-get-a-single-keypress-at-a-time
這個問題被問了一些時間回到這裏 - Python cross-platform listening for keypresses?
您可能會發現樣本代碼有幫助!
今天我遇到了這個問題的幾個解決方案,並認爲我會回過頭來分享一下,這樣其他人可以節省搜索時間。
模擬鍵盤和鼠標輸入一個漂亮的跨平臺解決方案:http://www.autopy.org/
我也發現瞭如何在全球範圍內記錄擊鍵簡短而工作(由於山獅)的例子。唯一的警告是你必須使用Python2.6,因爲2.7似乎沒有可用的objc模塊。
#!/usr/bin/python2.6
"""PyObjC keylogger for Python
by ljos https://github.com/ljos
"""
from Cocoa import *
import time
from Foundation import *
from PyObjCTools import AppHelper
class AppDelegate(NSObject):
def applicationDidFinishLaunching_(self, aNotification):
NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(NSKeyDownMask, handler)
def handler(event):
NSLog(u"%@", event)
def main():
app = NSApplication.sharedApplication()
delegate = AppDelegate.alloc().init()
NSApp().setDelegate_(delegate)
AppHelper.runEventLoop()
if __name__ == '__main__':
main()
對於鼠標輸入,簡單地從可用這裏的列表中選擇相關面膜代替NSKeyDownMask
:http://developer.apple.com/library/mac/#documentation/cocoa/Reference/ApplicationKit/Classes/NSEvent_Class/Reference/Reference.html#//apple_ref/occ/clm/NSEvent/addGlobalMonitorForEventsMatchingMask:handler:
例如,NSMouseMovedMask
工程跟蹤鼠標移動。 從那裏,你可以訪問event.locationInWindow()或其他屬性。
Calvin Cheng,謝謝。你的建議適用於OS X 10.8.5。
代碼從http://docs.python.org/faq/library.html#how-do-i-get-a-single-keypress-at-a-time
#!/usr/bin/python
import termios, fcntl, sys, os
fd = sys.stdin.fileno()
oldterm = termios.tcgetattr(fd)
newattr = termios.tcgetattr(fd)
newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
termios.tcsetattr(fd, termios.TCSANOW, newattr)
oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)
try:
while 1:
try:
c = sys.stdin.read(1)
print "Got character", repr(c)
except IOError: pass
finally:
termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)
還有一個解決方案 Key Listeners in python?
- 1. 在Ubuntu中使用Python獲得的鼠標和鍵盤事件
- 2. 我可以捕獲Windows Mobile PIE鍵盤事件嗎?
- 3. 同時捕獲鼠標和鍵盤事件
- 4. 我們可以使用arduino.getKey()獲取鍵盤事件嗎?
- 5. 如何使用UIAutomation獲取鼠標和鍵盤事件
- 6. 如何以我的應用程序捕捉鍵盤/鼠標事件爲焦點?
- 7. 發送鼠標和鍵盤事件
- 8. 如何捕捉鍵盤和鼠標事件?
- 9. 捕獲鼠標事件在Python
- 10. 我可以將按鍵映射到鼠標事件嗎?
- 11. 防止在所有瀏覽器的div內捕獲鼠標和鍵盤事件
- 12. 捕獲任何工具上的鍵盤和鼠標事件通過java庫
- 13. 捕獲鼠標事件
- 14. wxpython捕獲wx.Frame中的鍵盤事件
- 15. SDL使用現代OpenGL導航的鍵盤和鼠標事件
- 16. 如何在同一時間捕獲鍵盤和鼠標事件AS3
- 17. 鼠標捕獲後Silverlight鼠標事件丟失了嗎?
- 18. 在C中捕獲和發送鍵盤/鼠標輸入#
- 19. 禁用TComboBox中的鼠標滾輪和鍵盤事件
- 20. 在linux上使用C跟蹤鍵盤和鼠標事件
- 21. PCL展臺例如:無法獲得鍵盤和鼠標事件
- 22. 我可以捕獲Capslock/Shift鍵嗎?
- 23. 禁用鍵盤和鼠標
- 24. NSDockTile可以接受鼠標事件嗎?
- 25. 捕獲backspace的鍵盤事件
- 26. 在C(linux)中捕獲鼠標事件
- 27. 我可以在不激活任何窗口的情況下收聽鍵盤和鼠標事件嗎?
- 28. 在vim中,鼠標可以比鍵盤更有效嗎?
- 29. 查找鍵盤事件設備,鼠標
- 30. 我們可以使元素(div)對鼠標事件透明嗎?
這裏所提到的一些包有OS X的支持(例如'keyboard'):https://stackoverflow.com/questions/11918999/key -listeners合蟒/ – 2017-10-28 20:05:36