I'm making a screenshot utility for personal use and I want to add in bounding box screenshots. I want to be able to press insert on the 2 corners of the area and then grab the screenshot.
The issue is that I can't get the keyboard and mouse events to work with each other. I can't seem to get the mouse position.
This is what I have so far:
from PIL import ImageGrab
import time
import pythoncom, pyHook
mospos = None
def OnMouseEvent(event):
print 'MessageName:',event.MessageName
print 'Message:',event.Message
print 'Position:',event.Position
print '---'
mospos = event.Position
return True
def OnKeyboardEvent(event):
print 'KeyID:', event.KeyID#Show KeyID of keypress
if(event.KeyID == 44):#Prntscr
print 'Print Screen'
im = ImageGrab.grabclipboard()
im.save('img'+time.strftime("%d-%m-%y_%H-%M-%S")+'.png','PNG')#save with Day-Month-Year_Hour-Minute_Second format
if(event.KeyID == 45):#insert
print mospos
return True# return True to pass the event to other handlers
hm = pyHook.HookManager()# create a hook manager
hm.KeyDown = OnKeyboardEvent# watch for all key events
hm.MouseAll = OnMouseEvent
hm.HookKeyboard()# set the hook
hm.HookMouse()
pythoncom.PumpMessages()# wait forever
mospos never changes from 'None' even after I cause mouse events.
How do I get the mouse position from the keyboard event handler?
p.s. I'm eternally sorry if this doesn't make sense.