[Go to site: main page, start]

Introduction

This recipe is for registering system wide hotkeys--key combinations that are captured whether or not your app/window has the current focus. Works only under MS Windows.

What Objects are Involved

  • Any window object or descendant (which is any wx widget)
  • a function to handle HotKey events (EVT_HOTKEY)

  • the win32 extensions

Process Overview

First, register the hotkey. Bind the event to the event handler.

Special Concerns

Must have the win32 extensions installed. The Window.RegisterHotKey function takes the win32con.VK_* keycodes, rather than the wx.WVK_* keycodes.

Code Sample

   1 import wx
   2 import win32con #for the VK keycodes
   3 
   4 class FrameWithHotKey(wx.Frame):
   5     def __init__(self, *args, **kwargs):
   6         wx.Frame.__init__(self, *args, **kwargs)
   7         self.regHotKey()
   8         self.Bind(wx.EVT_HOTKEY, self.handleHotKey, id=self.hotKeyId)
   9     def regHotKey(self):
  10         """
  11         This function registers the hotkey Alt+F1 with id=100
  12         """
  13         self.hotKeyId = 100
  14         self.RegisterHotKey(
  15             self.hotKeyId, #a unique ID for this hotkey
  16             win32con.MOD_ALT, #the modifier key
  17             win32con.VK_F1) #the key to watch for
  18     def handleHotKey(self, evt):
  19         """
  20         Prints a simple message when a hotkey event is received.
  21         """
  22         print "do hot key actions"
  23         

Comments

RegisterHotKey (last edited 2008-03-11 10:50:31 by localhost)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.