Python PyAutoGUI无法输入@符号,邮件发送程序异常求助
Hey there! I totally get the frustration here—your Scandinavian keyboard uses AltGr+2 to type @, but PyAutoGUI’s default typewrite() method assumes the standard Shift+2 combo that doesn’t work for your layout. That’s why your @ symbols are vanishing when you run the script. Let’s fix this with two straightforward solutions:
Solution 1: Manually Simulate AltGr+2 for Each @
Instead of relying on typewrite() to handle the @ symbol, we’ll explicitly send the key combination your keyboard needs. Since AltGr is essentially a combination of Ctrl + Alt (or the right Alt key, altright in PyAutoGUI), we can use pyautogui.hotkey() to trigger it.
Here’s your modified code:
import pyautogui import time pyautogui.hotkey('win', 's') pyautogui.typewrite('Outlook 2013') pyautogui.press('enter') time.sleep(25) pyautogui.press('esc') pyautogui.hotkey('alt', 'h', 'n') # Input first email with explicit @ pyautogui.typewrite('americussmile') pyautogui.hotkey('ctrl', 'alt', '2') # Simulates AltGr+2 pyautogui.typewrite('gmail.com; ') # Input second email with explicit @ pyautogui.typewrite('phuongduyen210492') pyautogui.hotkey('ctrl', 'alt', '2') pyautogui.typewrite('gmail.com')
If ctrl+alt+2 doesn’t work for you, try using the right Alt key directly with pyautogui.hotkey('altright', '2')—some keyboard layouts map AltGr strictly to the right Alt.
Solution 2: Create a Helper Function for Easier Text Input
If you have longer text with multiple @ symbols, a helper function will save you from repeating the hotkey code every time. This function will iterate through each character and replace @ with the correct key combo:
import pyautogui import time def type_with_scandinavian_at(text): for char in text: if char == '@': pyautogui.hotkey('ctrl', 'alt', '2') # Or 'altright', '2' if needed else: pyautogui.typewrite(char) # Rest of your original code pyautogui.hotkey('win', 's') pyautogui.typewrite('Outlook 2013') pyautogui.press('enter') time.sleep(25) pyautogui.press('esc') pyautogui.hotkey('alt', 'h', 'n') # Use the helper function to input your emails type_with_scandinavian_at('americussmile@gmail.com; phuongduyen210492@gmail.com')
Why This Works
PyAutoGUI’s typewrite() function converts characters to key presses based on a standard QWERTY layout. Since your keyboard uses a non-standard combo for @, the function can’t map it correctly. By manually sending the exact key combination your keyboard requires, we bypass this layout mismatch and ensure the @ symbol is typed correctly.
内容的提问来源于stack exchange,提问作者Phuong Duyen Huynh Ngoc

