I want to make a global hotkey, with alt+1, 2, ..., that paste some string in the clipboard.
开发者_高级运维How can I do that?
The pyhook
module provides a reasonably easy way of using Windows Keyboard hooks.
As an alternative, I would recommend using each tool for what it was designed to do. That is, use AutoHotkey for setting the hot-keys and for other types of Windows automation. AutoHotkey (AHK) connects with Python very well in several ways (many are discussed in the AHK forums). One interesting way is using autohotkey.dll - AHK's functionality wrapped into a DLL which you can call with ctypes
.
AHK was designed for Windows automation, and tasks like this are trivial for it. Much more complex tasks are, too. You can just save yourself hours of digging the API docs. AHK can be compiled into an .exe weighing a few hundred of KBs so you can distribute it with any Windows application.
I believe you need to work at the Windows API level (no higher-level functions do that), with RegisterHotKey (first argument a null pointer) to associate the global hotkey to the current thread's message queue -- said thread must then go on to serving its normal Windows message loop. I suspect you'll want to run the whole thing as a Windows service -- e.g. per this recipe, which uses the Python for Windows Extensions -- to make sure it's always running while the system is up.
精彩评论