2

As you see the title. I was tried bind_all, but it seems not work while not focus on the dialog.

#
# After a post to c.l.py by Richie Hindle:
# http://groups.google.com/groups?th=80e876b88fabf6c9
#
import os
import sys
import ctypes
from ctypes import wintypes
import win32con

byref = ctypes.byref
user32 = ctypes.windll.user32

HOTKEYS = {
  1 : (win32con.VK_F3, win32con.MOD_CONTROL),
  2 : (win32con.VK_F4, win32con.MOD_CONTROL)
}

def handle_win_f3 ():
  os.startfile (os.environ['TEMP'])

def handle_win_f4 ():
  print 'hello'
  user32.PostQuitMessage (0)

HOTKEY_ACTIONS = {
  1 : handle_win_f3,
  2 : handle_win_f4
}

#
# RegisterHotKey takes:
#  Window handle for WM_HOTKEY messages (None = this thread)
#  arbitrary id unique within the thread
#  modifiers (MOD_SHIFT, MOD_ALT, MOD_CONTROL, MOD_WIN)
#  VK code (either ord ('x') or one of win32con.VK_*)
#
for id, (vk, modifiers) in HOTKEYS.items ():
  print "Registering id", id, "for key", vk
  if not user32.RegisterHotKey (None, id, modifiers, vk):
    print "Unable to register id", id

#
# Home-grown Windows message loop: does
#  just enough to handle the WM_HOTKEY
#  messages and pass everything else along.
#
try:
  msg = wintypes.MSG ()
  while user32.GetMessageA (byref (msg), None, 0, 0) != 0:
    if msg.message == win32con.WM_HOTKEY:
      action_to_take = HOTKEY_ACTIONS.get (msg.wParam)
      if action_to_take:
        action_to_take ()

    user32.TranslateMessage (byref (msg))
    user32.DispatchMessageA (byref (msg))

finally:
  for id in HOTKEYS.keys ():
    user32.UnregisterHotKey (None, id)

this is the code to register a global hotkey, and I just want to use it in my dialog to toggle display, or is there some other function? Could you help me?

Lellansin
  • 852
  • 1
  • 9
  • 15
  • What do you mean by "global hotkey"? Do you mean it works in all applications even if the python program isn't running, or do you mean it is global to the python application only? – Bryan Oakley Nov 20 '13 at 17:51
  • that's like to use the `user32.RegisterHotKey` to register a hotkey. – Lellansin Nov 21 '13 at 02:41
  • 1
    This question has been incorrectly marked as duplicate. This question asks how to register a system-wide hotkey for a tkinter app; the supposed duplicate question linked asks how to bind events to all the widgets in a tkinter app. – henrebotha May 07 '14 at 18:59

0 Answers0