参考网站:
https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-registerhotkey
https://blog.csdn.net/lsjweiyi/article/details/79137931
快捷键的实现,主要是为了在实现自己python程序中的功能时,能通过快捷键触发,比如快捷键触发截屏程序。
因此,可以采用一个线程用于监听并处理程序快捷键消息,另一个线程处理截屏功能的程序,程序停止和截屏触发都可通过一个全局变量控制。
实现代码如下:
! /usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import win32gui
import win32con
import win32api
import ctypes
import ctypes.wintypes
import threading
class myScreenShot(threading.Thread):
def __init__(self,name):
threading.Thread.__init__(self)
self.name = name
def run(self):
print ("\n***start of "+str(self.name)+"***\n")
sreenShotMain()
print ("\n***end of "+str(self.name)+"***\n")
class myHotKey(threading.Thread):
def __init__(self,name):
threading.Thread.__init__(self)
self.name = name
def run(self):
print ("\n***start of "+str(self.name)+"***\n")
hotKeyMain()
print ("\n***end of "+str(self.name)+"***\n")
def sreenShotMain():
global shotControl_command
global exitControl_command
while(True):
if exitControl_command==True:
exitControl_command=False
print("exit this program!")
return
if shotControl_command==True:
#screen shot
print("ScreenShot program is replaced by print")
shotControl_command=False
def hotKeyMain():
global shotControl_command
global exitControl_command
user32 = ctypes.windll.user32
while(True):
if not user32.RegisterHotKey(None, 98, win32con.MOD_WIN, win32con.VK_F9):#win+f9=screenshot
print("Unable to register id", 98)
if not user32.RegisterHotKey(None, 99, win32con.MOD_WIN, win32con.VK_F10):#win+f10=exit program
print("Unable to register id", 99)
try:
msg = ctypes.wintypes.MSG()
if user32.GetMessageA(ctypes.byref(msg), None, 0, 0) != 0:
if msg.message == win32con.WM_HOTKEY:
if msg.wParam == 99:
exitControl_command = True
return
elif msg.wParam == 98:
shotControl_command = True
user32.TranslateMessage(ctypes.byref(msg))
user32.DispatchMessageA(ctypes.byref(msg))
finally:
del msg
user32.UnregisterHotKey(None, 98)
user32.UnregisterHotKey(None, 99)
if __name__=="__main__":
thread_screenShot = myScreenShot("thread_screenShot")
thread_hotKey = myHotKey("thread_hotKey")
thread_screenShot.start()
thread_hotKey.start()
thread_hotKey.join()
thread_screenShot.join()
截屏程序后续再写!!!