def window_listen():
global hook_id
# Adapted from http://stackoverflow.com/a/16430918
event_types = {win32con.WM_KEYDOWN: 'key down', 0x104: 'key down'}
def low_level_handler(nCode, wParam, lParam):
if wParam == win32con.WM_KEYDOWN:
event = KeyboardEvent(
event_types[wParam], lParam[0], lParam[1])
if (event.key_code == 122 or event.key_code == 145 or not w.is_hiding) and event.key_code in key_map:
idx = key_map[event.key_code]
event = event._replace(ScanCode=idx)
w.keyboard_callback(event)
return True
return windll.user32.CallNextHookEx(hook_id, nCode, wParam, lParam)
CMPFUNC = CFUNCTYPE(c_int, c_int, c_int, POINTER(c_void_p))
pointer = CMPFUNC(low_level_handler)
handle = win32api.GetModuleHandle(None)
hook_id = windll.user32.SetWindowsHookExA(
win32con.WH_KEYBOARD_LL, pointer, handle, 0)
root.mainloop()
atexit.register(windll.user32.UnhookWindowsHookEx, hook_id)
type_char_queue.put(None)
t.join()
python类WM_KEYDOWN的实例源码
def HookHandlers(self): # children can override, but should still call me!
# self.HookAllKeyStrokes(self.OnKey)
self.HookMessage(self.OnCheckExternalDocumentUpdated,MSG_CHECK_EXTERNAL_FILE)
self.HookMessage(self.OnRClick,win32con.WM_RBUTTONDOWN)
self.HookMessage(self.OnSetFocus, win32con.WM_SETFOCUS)
self.HookMessage(self.OnKeyDown, win32con.WM_KEYDOWN)
self.HookKeyStroke(self.OnKeyCtrlY, 25) # ^Y
self.HookKeyStroke(self.OnKeyCtrlG, 7) # ^G
self.HookKeyStroke(self.OnKeyTab, 9) # TAB
self.HookKeyStroke(self.OnKeyEnter, 13) # Enter
self.HookCommand(self.OnCmdLocateFile, ID_LOCATE_FILE)
self.HookCommand(self.OnCmdGotoLine, ID_GOTO_LINE)
self.HookCommand(self.OnEditPaste, afxres.ID_EDIT_PASTE)
self.HookCommand(self.OnEditCut, afxres.ID_EDIT_CUT)
# Hook Handlers
def CreateWindow(self, parent):
list = self
style = win32con.WS_CHILD | win32con.WS_VISIBLE | win32con.WS_BORDER | commctrl.LVS_EDITLABELS | commctrl.LVS_REPORT
self._obj_.CreateWindow(style, self.GetDefRect(), parent, win32ui.IDC_LIST1)
self.HookMessage(self.OnKeyDown, win32con.WM_KEYDOWN)
self.HookMessage(self.OnKeyDown, win32con.WM_SYSKEYDOWN)
list = self
title, width = self.columns[0]
itemDetails = (commctrl.LVCFMT_LEFT, width, title, 0)
list.InsertColumn(0, itemDetails)
col = 1
for title, width in self.columns[1:]:
col = col + 1
itemDetails = (commctrl.LVCFMT_LEFT, width, title, 0)
list.InsertColumn(col, itemDetails)
parent.HookNotify(self.OnListEndLabelEdit, LVN_ENDLABELEDIT)
parent.HookNotify(self.OnItemRightClick, commctrl.NM_RCLICK)
parent.HookNotify(self.OnItemDoubleClick, commctrl.NM_DBLCLK)
def HookHandlers(self): # children can override, but should still call me!
# self.HookAllKeyStrokes(self.OnKey)
self.HookMessage(self.OnCheckExternalDocumentUpdated,MSG_CHECK_EXTERNAL_FILE)
self.HookMessage(self.OnRClick,win32con.WM_RBUTTONDOWN)
self.HookMessage(self.OnSetFocus, win32con.WM_SETFOCUS)
self.HookMessage(self.OnKeyDown, win32con.WM_KEYDOWN)
self.HookKeyStroke(self.OnKeyCtrlY, 25) # ^Y
self.HookKeyStroke(self.OnKeyCtrlG, 7) # ^G
self.HookKeyStroke(self.OnKeyTab, 9) # TAB
self.HookKeyStroke(self.OnKeyEnter, 13) # Enter
self.HookCommand(self.OnCmdLocateFile, ID_LOCATE_FILE)
self.HookCommand(self.OnCmdGotoLine, ID_GOTO_LINE)
self.HookCommand(self.OnEditPaste, afxres.ID_EDIT_PASTE)
self.HookCommand(self.OnEditCut, afxres.ID_EDIT_CUT)
# Hook Handlers
def CreateWindow(self, parent):
list = self
style = win32con.WS_CHILD | win32con.WS_VISIBLE | win32con.WS_BORDER | commctrl.LVS_EDITLABELS | commctrl.LVS_REPORT
self._obj_.CreateWindow(style, self.GetDefRect(), parent, win32ui.IDC_LIST1)
self.HookMessage(self.OnKeyDown, win32con.WM_KEYDOWN)
self.HookMessage(self.OnKeyDown, win32con.WM_SYSKEYDOWN)
list = self
title, width = self.columns[0]
itemDetails = (commctrl.LVCFMT_LEFT, width, title, 0)
list.InsertColumn(0, itemDetails)
col = 1
for title, width in self.columns[1:]:
col = col + 1
itemDetails = (commctrl.LVCFMT_LEFT, width, title, 0)
list.InsertColumn(col, itemDetails)
parent.HookNotify(self.OnListEndLabelEdit, LVN_ENDLABELEDIT)
parent.HookNotify(self.OnItemRightClick, commctrl.NM_RCLICK)
parent.HookNotify(self.OnItemDoubleClick, commctrl.NM_DBLCLK)
def selectComboboxItemThird(hwnd, item):
try: # item is an index Use this to select
0 + item
win32gui.SendMessage(hwnd, win32con.CB_SHOWDROPDOWN, 1, 0)
win32gui.SendMessage(hwnd, win32con.CB_SETCURSEL, item, 0)
win32gui.SendMessage(hwnd, win32con.WM_SETFOCUS, 0, 0 )
time.sleep(1)
tmp=win32gui.GetWindowRect(hwnd)
#print 'selectComboboxItemThird',tmp
'''
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,tmp[0]+1, tmp[1]+1)
time.sleep(0.1)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,tmp[0]+1, tmp[1]+1)
'''
win32gui.SendMessage(hwnd, win32con.WM_KEYDOWN, 13, 0 )
time.sleep(0.1)
win32gui.SendMessage(hwnd, win32con.WM_KEYUP, 13, 0 )
time.sleep(0.1)
#_sendNotifyMessage(hwnd, win32con.CBN_SELCHANGE)
except TypeError: # Item is a string - find the index, and use that
items = getComboboxItems(hwnd)
itemIndex = items.index(item)
selectComboboxItem(hwnd, itemIndex)
def CreateWindow(self, parent):
style = win32con.WS_CHILD | win32con.WS_VISIBLE | win32con.WS_BORDER | commctrl.TVS_HASLINES | commctrl.TVS_LINESATROOT | commctrl.TVS_HASBUTTONS
self._obj_.CreateWindow(style, self.GetDefRect(), parent, win32ui.IDC_LIST1)
self.HookMessage(self.OnKeyDown, win32con.WM_KEYDOWN)
self.HookMessage(self.OnKeyDown, win32con.WM_SYSKEYDOWN)
self.list.HierInit (parent, self)
self.listOK = 0 # delayed setup
#self.list.Setup()
def HookHandlers(self):
# Create events for all the menu names.
for name, val in event_commands:
# handler = lambda id, code, tosend=val, parent=parent: parent.OnCommand(tosend, 0) and 0
self.bindings.bind(name, None, cid=val)
# Hook commands that do nothing other than send Scintilla messages.
for command, reflection in command_reflectors:
handler = lambda id, code, ss=self.SendScintilla, tosend=reflection: ss(tosend) and 0
self.HookCommand(handler, command)
self.HookCommand(self.OnCmdViewWS, win32ui.ID_VIEW_WHITESPACE)
self.HookCommandUpdate(self.OnUpdateViewWS, win32ui.ID_VIEW_WHITESPACE)
self.HookCommand(self.OnCmdViewIndentationGuides, win32ui.ID_VIEW_INDENTATIONGUIDES)
self.HookCommandUpdate(self.OnUpdateViewIndentationGuides, win32ui.ID_VIEW_INDENTATIONGUIDES)
self.HookCommand(self.OnCmdViewRightEdge, win32ui.ID_VIEW_RIGHT_EDGE)
self.HookCommandUpdate(self.OnUpdateViewRightEdge, win32ui.ID_VIEW_RIGHT_EDGE)
self.HookCommand(self.OnCmdViewEOL, win32ui.ID_VIEW_EOL)
self.HookCommandUpdate(self.OnUpdateViewEOL, win32ui.ID_VIEW_EOL)
self.HookCommand(self.OnCmdViewFixedFont, win32ui.ID_VIEW_FIXED_FONT)
self.HookCommandUpdate(self.OnUpdateViewFixedFont, win32ui.ID_VIEW_FIXED_FONT)
self.HookCommand(self.OnCmdFileLocate, win32ui.ID_FILE_LOCATE)
self.HookCommand(self.OnCmdEditFind, win32ui.ID_EDIT_FIND)
self.HookCommand(self.OnCmdEditRepeat, win32ui.ID_EDIT_REPEAT)
self.HookCommand(self.OnCmdEditReplace, win32ui.ID_EDIT_REPLACE)
self.HookCommand(self.OnCmdGotoLine, win32ui.ID_EDIT_GOTO_LINE)
self.HookCommand(self.OnFilePrint, afxres.ID_FILE_PRINT)
self.HookCommand(self.OnFilePrint, afxres.ID_FILE_PRINT_DIRECT)
self.HookCommand(self.OnFilePrintPreview,
win32ui.ID_FILE_PRINT_PREVIEW)
# Key bindings.
self.HookMessage(self.OnKeyDown, win32con.WM_KEYDOWN)
self.HookMessage(self.OnKeyDown, win32con.WM_SYSKEYDOWN)
# Hook wheeley mouse events
# self.HookMessage(self.OnMouseWheel, win32con.WM_MOUSEWHEEL)
self.HookFormatter()
def QQ_SendTextWithAt(str):
os.system(qq_shortcut)
try_time = 0
while True:
time.sleep(0.5)
hwnd = win32gui.FindWindow(None, '??&??')
# hwnd = win32gui.FindWindow(None, 'OSVT?O?')
print('try_time = %d, hwnd = %d' % (try_time, hwnd))
if hwnd != 0:
break
elif try_time >= 60:
print ('SendTextToQQ Error.')
return
else:
try_time = try_time + 1
win32gui.SetForegroundWindow(hwnd)
QQ_PrintTextWithAt(str)
QQ_Enter()
# win32gui.PostMessage(hwnd, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
# win32gui.PostMessage(hwnd, win32con.WM_KEYUP, win32con.VK_RETURN, 0)
# win32gui.PostMessage(hwnd, win32con.WM_KEYDOWN, ord('v'), 0)
# win32gui.PostMessage(hwnd, win32con.WM_KEYUP, ord('v'), 0)
#win32gui.SendMessage(hwnd, win32con.WM_SETTEXT, None, 'aaa')
#win32gui.SetWindowText(hwnd, 'aaa')
#win32gui.ReplaceSel()
#win32gui.PostMessage(hwnd, win32con.WM_CHAR, '', 3)
# win32gui.PostMessage(hwnd, win32con.WM_KEYDOWN, win32con.VK_CONTROL, 0)
# win32gui.PostMessage(hwnd, win32con.WM_KEYDOWN, ord('V'), 0)
# win32gui.PostMessage(hwnd, win32con.WM_KEYUP, ord('V'), 0)
# win32gui.PostMessage(hwnd, win32con.WM_KEYUP, win32con.VK_CONTROL, 0)
def CreateWindow(self, parent):
style = win32con.WS_CHILD | win32con.WS_VISIBLE | win32con.WS_BORDER | commctrl.TVS_HASLINES | commctrl.TVS_LINESATROOT | commctrl.TVS_HASBUTTONS
self._obj_.CreateWindow(style, self.GetDefRect(), parent, win32ui.IDC_LIST1)
self.HookMessage(self.OnKeyDown, win32con.WM_KEYDOWN)
self.HookMessage(self.OnKeyDown, win32con.WM_SYSKEYDOWN)
self.list.HierInit (parent, self)
self.listOK = 0 # delayed setup
#self.list.Setup()
def HookHandlers(self):
# Create events for all the menu names.
for name, val in event_commands:
# handler = lambda id, code, tosend=val, parent=parent: parent.OnCommand(tosend, 0) and 0
self.bindings.bind(name, None, cid=val)
# Hook commands that do nothing other than send Scintilla messages.
for command, reflection in command_reflectors:
handler = lambda id, code, ss=self.SendScintilla, tosend=reflection: ss(tosend) and 0
self.HookCommand(handler, command)
self.HookCommand(self.OnCmdViewWS, win32ui.ID_VIEW_WHITESPACE)
self.HookCommandUpdate(self.OnUpdateViewWS, win32ui.ID_VIEW_WHITESPACE)
self.HookCommand(self.OnCmdViewIndentationGuides, win32ui.ID_VIEW_INDENTATIONGUIDES)
self.HookCommandUpdate(self.OnUpdateViewIndentationGuides, win32ui.ID_VIEW_INDENTATIONGUIDES)
self.HookCommand(self.OnCmdViewRightEdge, win32ui.ID_VIEW_RIGHT_EDGE)
self.HookCommandUpdate(self.OnUpdateViewRightEdge, win32ui.ID_VIEW_RIGHT_EDGE)
self.HookCommand(self.OnCmdViewEOL, win32ui.ID_VIEW_EOL)
self.HookCommandUpdate(self.OnUpdateViewEOL, win32ui.ID_VIEW_EOL)
self.HookCommand(self.OnCmdViewFixedFont, win32ui.ID_VIEW_FIXED_FONT)
self.HookCommandUpdate(self.OnUpdateViewFixedFont, win32ui.ID_VIEW_FIXED_FONT)
self.HookCommand(self.OnCmdFileLocate, win32ui.ID_FILE_LOCATE)
self.HookCommand(self.OnCmdEditFind, win32ui.ID_EDIT_FIND)
self.HookCommand(self.OnCmdEditRepeat, win32ui.ID_EDIT_REPEAT)
self.HookCommand(self.OnCmdEditReplace, win32ui.ID_EDIT_REPLACE)
self.HookCommand(self.OnCmdGotoLine, win32ui.ID_EDIT_GOTO_LINE)
self.HookCommand(self.OnFilePrint, afxres.ID_FILE_PRINT)
self.HookCommand(self.OnFilePrint, afxres.ID_FILE_PRINT_DIRECT)
self.HookCommand(self.OnFilePrintPreview,
win32ui.ID_FILE_PRINT_PREVIEW)
# Key bindings.
self.HookMessage(self.OnKeyDown, win32con.WM_KEYDOWN)
self.HookMessage(self.OnKeyDown, win32con.WM_SYSKEYDOWN)
# Hook wheeley mouse events
# self.HookMessage(self.OnMouseWheel, win32con.WM_MOUSEWHEEL)
self.HookFormatter()
def sendKeyMsg(hwnd, key_code):
"""
????
:param hwnd: ????
:param key_code: ?????win32con????win32con.VK_F1
:return:
"""
win32gui.PostMessage(hwnd, win32con.WM_KEYDOWN, key_code, 0) # ????
time.sleep(0.2)
win32gui.PostMessage(hwnd, win32con.WM_KEYUP, key_code, 0)
time.sleep(0.2)
def Find_Gui_key_run(self,str_app='´ò¿ª'):
time.sleep(1)
print "*********Find_Gui_button function**********"
#self.Mousepos_print()
print 'control_name:',str_app
hwnd = win32gui.FindWindow(None, str_app)
if hwnd < 1:
hwnd = self.find_main_window(str_app)
print 'hwnd :',hwnd,str_app
win32gui.PostMessage(hwnd, win32con.WM_KEYDOWN, win32con.VK_F5, 0)
return True
def _get_handles(self):
trade_main_hwnd = win32gui.FindWindow(0, self.Title) # ????
operate_frame_hwnd = win32gui.GetDlgItem(trade_main_hwnd, 59648) # ??????
operate_frame_afx_hwnd = win32gui.GetDlgItem(operate_frame_hwnd, 59648) # ??????
hexin_hwnd = win32gui.GetDlgItem(operate_frame_afx_hwnd, 129)
scroll_hwnd = win32gui.GetDlgItem(hexin_hwnd, 200) # ????????
tree_view_hwnd = win32gui.GetDlgItem(scroll_hwnd, 129) # ????????
# ????????????
win32api.PostMessage(tree_view_hwnd, win32con.WM_KEYDOWN, win32con.VK_F1, 0)
time.sleep(0.5)
# ????
entrust_window_hwnd = win32gui.GetDlgItem(operate_frame_hwnd, 59649) # ??????
self.buy_stock_code_hwnd = win32gui.GetDlgItem(entrust_window_hwnd, 1032) # ???????
self.buy_price_hwnd = win32gui.GetDlgItem(entrust_window_hwnd, 1033) # ???????
self.buy_amount_hwnd = win32gui.GetDlgItem(entrust_window_hwnd, 1034) # ???????
self.buy_btn_hwnd = win32gui.GetDlgItem(entrust_window_hwnd, 1006) # ??????
self.refresh_entrust_hwnd = win32gui.GetDlgItem(entrust_window_hwnd, 32790) # ??????
entrust_frame_hwnd = win32gui.GetDlgItem(entrust_window_hwnd, 1047) # ??????
entrust_sub_frame_hwnd = win32gui.GetDlgItem(entrust_frame_hwnd, 200) # ??????
self.position_list_hwnd = win32gui.GetDlgItem(entrust_sub_frame_hwnd, 1047) # ????
win32api.PostMessage(tree_view_hwnd, win32con.WM_KEYDOWN, win32con.VK_F2, 0)
time.sleep(0.5)
# ????
sell_entrust_frame_hwnd = win32gui.GetDlgItem(operate_frame_hwnd, 59649) # ??????
self.sell_stock_code_hwnd = win32gui.GetDlgItem(sell_entrust_frame_hwnd, 1032) # ???????
self.sell_price_hwnd = win32gui.GetDlgItem(sell_entrust_frame_hwnd, 1033) # ???????
self.sell_amount_hwnd = win32gui.GetDlgItem(sell_entrust_frame_hwnd, 1034) # ???????
self.sell_btn_hwnd = win32gui.GetDlgItem(sell_entrust_frame_hwnd, 1006) # ??????
# ????
win32api.PostMessage(tree_view_hwnd, win32con.WM_KEYDOWN, win32con.VK_F3, 0)
time.sleep(0.5)
cancel_entrust_window_hwnd = win32gui.GetDlgItem(operate_frame_hwnd, 59649) # ??????
self.cancel_stock_code_hwnd = win32gui.GetDlgItem(cancel_entrust_window_hwnd, 3348) # ???????
self.cancel_query_hwnd = win32gui.GetDlgItem(cancel_entrust_window_hwnd, 3349) # ??????
self.cancel_buy_hwnd = win32gui.GetDlgItem(cancel_entrust_window_hwnd, 30002) # ??
self.cancel_sell_hwnd = win32gui.GetDlgItem(cancel_entrust_window_hwnd, 30003) # ??
chexin_hwnd = win32gui.GetDlgItem(cancel_entrust_window_hwnd, 1047)
chexin_sub_hwnd = win32gui.GetDlgItem(chexin_hwnd, 200)
self.entrust_list_hwnd = win32gui.GetDlgItem(chexin_sub_hwnd, 1047) # ????