简体   繁体   中英

How do I register global system keyboard hotkeys on Mac OS X in wxPython/Python 2.7?

I'm writing a GUI-based app in Python 2.7 using wxPython 2.8. I'm trying to run everything on a Mac, and unfortunately it seems that wxPython's wx.Window.RegisterHotKey() method only works on Windows. I would like to be able to set a global hotkey or key combination while the application is running, and have that key combination get passed on to the application itself, even when it doesn't have focus. How can I allow my application to be notified when a (user-defined) global hotkey/combination is pressed on Mac OS X, even when the app doesn't have focus?

When the app has focus, you should be able to use wxPython's AcceleratorTable:

http://www.blog.pythonlibrary.org/2008/07/02/wxpython-working-with-menus-toolbars-and-accelerators/

http://www.blog.pythonlibrary.org/2010/12/02/wxpython-keyboard-shortcuts-accelerators/

I don't have a Mac, so for you'll have to try asking on the wxPython mailing list or the wx-mac mailing list for help about registering a hotkey outside of wx.

This is currently not available on platforms other than Win32. I have an open ticket for it on the WX tracker: http://trac.wxwidgets.org/ticket/12354 You'll find a link to how to accomplish this with Objective C, and you should be able to do it with PyObjC, but both WXPython and the outlined method for capturing hotkeys on OSX require running in an event loop in the main thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM