Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analysis of the process of simulating keyboard input and switching keyboard layout by python

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "python simulated keyboard input and the process of switching keyboard layout analysis". In daily operation, I believe many people have doubts about the process of python simulated keyboard input and switching keyboard layout. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts of "python simulated keyboard input and the process of switching keyboard layout". Next, please follow the editor to study!

PostMessage ()

Def keyHwnd (hwndEx, char): "" enter the value to the specified control: param hwndEx: control handle: param char: string: return: True or Flase "" try: for _ in char: print ('key:%s ascii:%d'% (_, ord (_)) win32api.PostMessage (hwndEx, win32con.WM_CHAR, ord (_)) " 0) time.sleep (random.uniform (00.20)) except Exception as e: print (e) return False return True hwnd = win32gui.FindWindow (None,'a.txt-notepad') print (hwnd) win32gui.SetForegroundWindow (hwnd) hwndex = win32gui.FindWindowEx (hwnd,None,'Edit', None) keyHwnd (hwndex,' spread local SDFkof Ldsojfdfdsjfd;slkjfdlksjfkldsjflkdsjlkfjkldsljkfjdssj')

Toggle keyboard layout

This function is packaged into a decorator, and the original keyboard layout is restored after the function is executed.

Import win32confrom win32con import WM_INPUTLANGCHANGEREQUESTimport win32guiimport win32apidef setKeyboardLayout_en (inner): def wrapper (* args, * * kwargs): if win32api.LoadKeyboardLayout ('0x0409keyboard, win32con.KLF_ACTIVATE) = = None: return Exception (' failed to load keyboard') # language code # https://msdn.microsoft.com/en-us/library/cc233982.aspx LID = {0x0804: "Chinese (Simplified) (People's Republic of China)" 0x0409: 'English (United States)'} # get foreground window handle hwnd = win32gui.GetForegroundWindow () # get foreground window title title = win32gui.GetWindowText (hwnd) # get keyboard layout list im_list = win32api.GetKeyboardLayoutList () im_list = list (map (hex) Im_list) print (im_list) oldKey = hex (win32api.GetKeyboardLayout ()) # set the keyboard layout to English result = win32api.SendMessage (hwnd, WM_INPUTLANGCHANGEREQUEST, 0, 0x4090409) if result = 0: print ('set English keyboard successfully!') Inner (* args,*kwargs) result = win32api.SendMessage (hwnd, WM_INPUTLANGCHANGEREQUEST, 0, oldKey) if result = 0: print ('keyboard restored successfully!') Return wrapper

Ascii standard code

Keybd_event ()

Win32api.keybd_event (65,0,0,0) time.sleep (random.uniform (0,0.5)) win32api.keybd_event (65,0,0,0)

Microsoft Keyboard Code reference

At this point, on the "python simulation of keyboard input and switch keyboard layout of the process analysis" is over, I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report