打字!带有 pyautogui 的字符 [英] Typewrite ! character with pyautogui

查看:36
本文介绍了打字!带有 pyautogui 的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可以模拟击键:

import pyautogui
pyautogui.typewrite('hello world!', interval=0.1)

除了:

  • 它写hello world§(使用FR键盘布局)
  • 它写hello world(使用EN键盘布局)
  • it writes hello world§ (with FR keyboard layout)
  • it writes hello world (with EN keyboard layout)

当然,想要的输出应该是hello world!.

Of course, the desired output should be hello world!.

有解决方法吗?

注意:我认为这与 使用 pyautogui 输入 unicode 字符串不是同一个问题 因为这里它不是非 ASCII 字符,但无论如何,复制/粘贴黑客的主要答案在我的情况下不起作用,因为我真的希望每次按键之间有 100 毫秒的暂停时间.

NB: I don't think it is the same problem than Input unicode string with pyautogui because here it's not a non-ASCII character, but anyway the main answer with a copy/paste hack would not work in my case, as I really want the slow typing with 100ms pause between each keypress.

以下是重现错误的方法:

Here is how to reproduce the error:

  • Windows 7 x64
  • Python 3.6 或 Python 2.7
  • pyautogui 0.9.41 或 0.9.48
  • 开头提到的两行代码

推荐答案

它不向程序发送 ascii char ! - 它向系统发送键盘代码(可能是键 1 的代码) 在标准布局中用于 char !) 并且系统决定向程序发送什么字符.如果您的系统具有非标准布局,则系统可能会发送错误的字符.

It doesn't send ascii char ! to program - it sends keyboard's code to system (probably code for key 1 which in standard layout is used for char !) and system decides what char send to program. If your system has non-standard layout then system may send wrong char.

可能只有使用剪贴板才能正确发送.如果您将使用剪贴板复制单个字符并在字符之间等待 0.1s 那么您可以获得类似的结果.

Probably only using clipboad you can send it correctly. If you will use clipboard to copy single char and wait 0.1s between chars then you can get similar result.

import time
import pyperclip
import pyautogui

time.sleep(2)

for char in 'Hello World!':
    pyperclip.copy(char)
    pyautogui.hotkey('ctrl', 'v', interval=0.1)


顺便说一句:使用 print(pyautogui.__file__) 你可以找到包含源代码的文件夹,在文件 _pyautogui_win.py 中你可以看到什么它在 Windows 中使用的关键代码.


BTW: using print(pyautogui.__file__) you can find folder with source code and in file _pyautogui_win.py you can see what key codes it uses in Windows.

您应该也看到分配给字符的键码

You should see key codes assigned to chars using also

窗口:

print(pyautogui._pyautogui_win.keyboardMapping)

Linux:

print(pyautogui._pyautogui_x11.keyboardMapping)

也许如果您更改 keyboardMapping 中的值,它就会正确发送它,但是对于每个布局,您都必须设置不同的值.

Maybe if you change values in keyboardMapping then it will send it correctly but for every layout you would have to set different values.

例如在 Linux 上

For example on Linux this

import pyautogui

#pyautogui._pyautogui_win.keyboardMapping['!'] = 12
pyautogui._pyautogui_x11.keyboardMapping['!'] = 12

pyautogui.typewrite('!!!')

给我 ### 而不是 !!!

这篇关于打字!带有 pyautogui 的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆