wxpython how to reigster global hotkey

317 views
Skip to first unread message

Rill

unread,
Jun 12, 2014, 3:45:20 AM6/12/14
to wxPython-users
I try pyhk, too much reliance on.

My require hotkeys for keyboard, for example:
hot.addHotkey(['Ctrl', 'Alt','f1'], fun)

hope support window + mac


--
jiang zhixiang

Mike Stover

unread,
Jun 12, 2014, 4:36:29 PM6/12/14
to wxpytho...@googlegroups.com
While I am not 100% sure of what you are asking, I believe you may want to check out accelerator tables. Using Mike Driscoll's blog as a guide wxPython Keyboard Shortcuts (accelerators) I was able to get around an annoying issue with one of my programs not properly using Ctrl+C to copy data to the pc clipboard.

Mike Driscoll

unread,
Jun 12, 2014, 4:54:27 PM6/12/14
to wxpytho...@googlegroups.com


On Thursday, June 12, 2014 3:36:29 PM UTC-5, Mike Stover wrote:
While I am not 100% sure of what you are asking, I believe you may want to check out accelerator tables. Using Mike Driscoll's blog as a guide wxPython Keyboard Shortcuts (accelerators) I was able to get around an annoying issue with one of my programs not properly using Ctrl+C to copy data to the pc clipboard.



That certainly sounds like an AccleratorTable would solve the problem. If the OP needs to get lower level, then they'll want to try binding to EVT_KEY_DOWN or possibly EVT_CHAR. I would definitely start with the AccleratorTable though.

Mike

Rill

unread,
Jun 13, 2014, 3:44:34 AM6/13/14
to wxPython-users
I found RegisterHotKey() can help me.

        self.regHotKey()
        self.Bind(wx.EVT_HOTKEY, self.handleHotKey, id=self.hotKeyId)
       
    def regHotKey(self):
        # This function registers the hotkey Alt+F1 with id=100
        self.hotKeyId = 100
        self.RegisterHotKey(
            self.hotKeyId, #a unique ID for this hotkey
            win32con.MOD_CONTROL | win32con.MOD_ALT, #the modifier key
            win32con.VK_F1) #the key to watch for
       
    def handleHotKey(self, evt):
        print "do hot key actions"


--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
jiang zhixiang

Mike Stover

unread,
Jun 13, 2014, 8:38:48 AM6/13/14
to wxpytho...@googlegroups.com
While this will work on Windows I do not believe it will work for Mac's due to the use of win32con to bind the keys. By using the accelerator tables and Mike Driscoll's suggestion of catching EVT_KEY_DOWN and EVT_CHAR it will be easier to support multiple OS platforms. Of course you could add more to the code of your program that would detect the running OS and switch how it will bind the keys, it may just take more time and coding to get it working.

Mike Driscoll

unread,
Jun 13, 2014, 9:09:12 AM6/13/14
to wxpytho...@googlegroups.com
On Fri, Jun 13, 2014 at 7:38 AM, Mike Stover <hakug...@gmail.com> wrote:
While this will work on Windows I do not believe it will work for Mac's due to the use of win32con to bind the keys. By using the accelerator tables and Mike Driscoll's suggestion of catching EVT_KEY_DOWN and EVT_CHAR it will be easier to support multiple OS platforms. Of course you could add more to the code of your program that would detect the running OS and switch how it will bind the keys, it may just take more time and coding to get it working.


You are correct. Anytime you use a module from PyWin32, it will only work on Windows. This code will NOT work on Mac or Linux.  


-----------------
Mike Driscoll

Blog:   http://blog.pythonlibrary.org
Book: https://gumroad.com/l/bppWr
Reply all
Reply to author
Forward
0 new messages