471,066 Members | 1,005 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,066 software developers and data experts.

Problems with threaded Hotkey application

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

Nov 23 '05 #1
3 2444
One obvious point is that, according to:

http://msdn.microsoft.com/library/de...fWM_HOTKEY.asp

the WM_HOTKEY message is posted to the queue *of the thread which
registered the hotkey*. I haven't yet tried it myself to see, but in
your example the main thread registers the hotkey, and the KeyCatch
thread is waiting to receive it.

TJG

Nov 23 '05 #2
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.

Nov 23 '05 #3
Thanks Tim, that resolved it.

Nov 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Rob Durant | last post: by
20 posts views Thread by zapov | last post: by
5 posts views Thread by jcrouse | last post: by
3 posts views Thread by =?Utf-8?B?QmlsbHkgWmhhbmc=?= | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.