I'm trying to achieve something very simple - with a script I would like to:
- Launch a software
- Fill out username
- Press tab
- Fill out password
- Press enter
I cannot get steps 3 and 5 to work. Here is my code:
import win32api
import win32gui
import win32con
def send_keys(hwnd, username, password):
for c in username:
win32api.PostMessage(hwnd, win32con.WM_CHAR, ord(c), 0)
win32api.keybd_event(win32con.VK_TAB, 0, win32con.KEYEVENTF_EXTENDEDKEY, 0) # DOES NOT WORK
for c in password:
win32api.PostMessage(hwnd, win32con.WM_CHAR, ord(c), 0)
win32api.keybd_event(win32con.VK_RETURN, 0, win32con.KEYEVENTF_EXTENDEDKEY, 0) # DOES NOT WORK
def main():
[...] # Run software, it pops as foreground window
hwnd = win32gui.GetForegroundWindow()
send_keys(hwnd, 'some_username', 'some_password')
if __name__ == '__main__':
main()
When I run the key presses part of script (send_keys
) in notepad, it sends tab and return/enter keys properly, however, it does not work for the intended software. Any suggestions?