473,230 Members | 1,294 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,230 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 2559
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Nitesh Jain | last post by:
I want to write a simple utility, so that if I select a text, and press a hot key say (CTRL+ALT+G), then I should be able to open a google search page with the selected text. Could someone tell me...
5
by: Rob Durant | last post by:
Hi, I have a multi-threaded application (have also tried as service - same behaviour) that runs fine on XP, but not on 2003. Symptoms are: Threads are started normally, locks acquired and...
20
by: zapov | last post by:
Hi! I'm having some wierd problems with this exception (error). If I use sql commands to insert data into sql server i get strange behaviour from my application. First I used a single...
5
by: jcrouse | last post by:
I have an application thats a viewer. I also have a form that is fired from a Tools/Options menu selection. In the Options form the user can select a hotkey to use to exit the main viewer...
0
by: BergRD | last post by:
Salutations! New to the forums but have gotten many an idea from lurking over the past few months but alas it's time to begin positing; posting to a problem I cannot seem to resolve. This is a...
0
by: yasker | last post by:
I register a hotkey following this article: http://www.dotnet2themax.com/ShowContent.aspx?ID=103cca7a-0323-47eb-b210-c2bb7075ba78 Using windows api to archive it. But when I use it as a part of my...
11
by: Steve Smith | last post by:
I have written a winforms application that launches approximately 150 threads with Thread.ThreadStart() Each thread uses CDO 1.21 to logon to a different Exchange mailbox and send/receive a...
1
by: Rune Jacobsen | last post by:
Hi all, I have some often-performed tasks in my applications that I want users to be able to specify hotkeys for. However, I don't want to hard code these hotkeys, as sooner or later some other...
3
by: =?Utf-8?B?QmlsbHkgWmhhbmc=?= | last post by:
I want to have following hotkey used in asp.net application, how to do this especially for F5? ESC F3 F4 F5 F6 F9 F10
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.