Problems with threaded Hotkey application

Discussion in 'Python' started by Rsrany, Nov 23, 2005.

  1. Rsrany

    Rsrany Guest

    I've been working on a few gtk applications and need to tie a hot key
    catcher into a thread. I am currently finding threaded
    user32.GetMessageA do not work.

    I have included two programs:
    1) a non-threaded version that works
    2) a threaded version that doesnt work.

    Any constructive suggestions would be helpful

    #########################################################
    Working non-threaded version:
    #########################################################
    import sys
    from ctypes import *
    from ctypes.wintypes import *

    # Define the Windows DLLs, constants and types that we need.
    user32 = windll.user32

    WM_HOTKEY = 0x0312
    MOD_ALT = 0x0001
    MOD_CONTROL = 0x0002
    MOD_SHIFT = 0x0004

    class MSG(Structure):
    _fields_ = [('hwnd', c_int),
    ('message', c_uint),
    ('wParam', c_int),
    ('lParam', c_int),
    ('time', c_int),
    ('pt', POINT)]

    # Register a hotkey for Ctrl+Shift+P.
    hotkeyId = 1
    if not user32.RegisterHotKey(None, hotkeyId, MOD_CONTROL |
    MOD_SHIFT, ord('P')):
    sys.exit("Failed to register hotkey; maybe someone else
    registered it?")

    # Spin a message loop waiting for WM_HOTKEY.
    while 1 :

    msg = MSG()
    while user32.GetMessageA(byref(msg), None, 0, 0) != 0:
    if msg.message == WM_HOTKEY and msg.wParam == hotkeyId:
    print "Yay"
    windll.user32.PostQuitMessage(0)
    user32.TranslateMessage(byref(msg))
    user32.DispatchMessageA(byref(msg))





    #################################################
    And here is the Non Working Threaded version :
    #################################################
    import sys
    from ctypes import *
    from ctypes.wintypes import *
    import threading

    # Define the Windows DLLs, constants and types that we need.
    user32 = windll.user32

    WM_HOTKEY = 0x0312
    MOD_ALT = 0x0001
    MOD_CONTROL = 0x0002
    MOD_SHIFT = 0x0004

    class MSG(Structure):
    _fields_ = [('hwnd', c_int),
    ('message', c_uint),
    ('wParam', c_int),
    ('lParam', c_int),
    ('time', c_int),
    ('pt', POINT)]

    # Register a hotkey for Ctrl+Shift+P.
    hotkeyId = 1
    if not user32.RegisterHotKey(None, hotkeyId, MOD_CONTROL |
    MOD_SHIFT, ord('P')):
    sys.exit("Failed to register hotkey; maybe someone else
    registered it?")

    class KeyCatch(threading.Thread):

    def run(self):

    print "TESTING TO MAKE SURE THREAD IS RUNNING!"

    # Spin a message loop waiting for WM_HOTKEY.
    while 1 :

    msg = MSG()
    while user32.GetMessageA(byref(msg), None, 0, 0) != 0:
    if msg.message == WM_HOTKEY and msg.wParam ==
    hotkeyId:
    print "Yay"
    windll.user32.PostQuitMessage(0)
    user32.TranslateMessage(byref(msg))
    user32.DispatchMessageA(byref(msg))

    GetKey = KeyCatch()
    GetKey.start()

    while 1:
    pass
    Rsrany, Nov 23, 2005
    #1
    1. Advertising

  2. Rsrany

    Tim G Guest

    Tim G, Nov 23, 2005
    #2
    1. Advertising

  3. Rsrany

    Tim G Guest

    And just to confirm, it does in fact work. If you move the
    RegisterHotKey line to within the thread's run method, the thread's
    message loop picks up the hotkey press.
    Tim G, Nov 23, 2005
    #3
  4. Rsrany

    Rsrany Guest

    Thanks Tim, that resolved it.
    Rsrany, Nov 23, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?R21hbjk5Nw==?=

    Hotkey save functionality in DataGrid

    =?Utf-8?B?R21hbjk5Nw==?=, May 25, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    362
    =?Utf-8?B?R21hbjk5Nw==?=
    May 25, 2004
  2. Replies:
    1
    Views:
    632
  3. Luc The Perverse

    Hotkey Manager

    Luc The Perverse, Oct 12, 2006, in forum: Java
    Replies:
    5
    Views:
    438
    Luc The Perverse
    Oct 14, 2006
  4. Replies:
    9
    Views:
    708
    Paul Boddie
    Dec 10, 2006
  5. Jack

    Eclipse hotkey

    Jack, Jul 17, 2007, in forum: Java
    Replies:
    3
    Views:
    581
    Hendrik Maryns
    Jul 17, 2007
Loading...

Share This Page