JReesW
JReesW

Reputation: 31

python pyautogui.write() accelerating and messing up what it is supposed to write

A friend of mine came across this conundrum.
He basically has a giant string and is trying to use pyautogui's .write() function to write this string to any textfield (Windows' Notepad in this case).
The code he is using is as follows:

import pyautogui
import time

# The string to which all numbers are being added
x = ""

# Some time to switch to notepad before pyautogui spams a bunch of characters
time.sleep(5)

# Add numbers to the string along with a space
for i in range(1, 2001):
    x += str(i) + " "

# "Simulate" the keyboard writing out the entire string
pyautogui.write(x)

Note that pyautogui.write() has an optional parameter interval which is set to 0 by default. pyautogui's source code uses time.sleep(interval) to create an interval.

Now there are two interesting anomalies that show:

A sample of the chaotic mess looks like this:

1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 12   66662662621   728822111  1  3  599933331   21612222333111111  73444433111 24686066663971117777773331413311111  3599944441   11111111  802222444623444411144441111179511155555754444444444111     2    68024      024  68  0    6 02    680324  65511111155511155555555555555555555666466772468088868     16000009135     111666661663666344446668     1111166666666766677357   111111116666699924911771777007111111216 1177722777333335   111111777440   111111176357779   11788828888 1777999903 188811111111168   1111888228833335 1118 4148 468 41 511828 85 51 588 5108128141 61 68 718 28 715 768 719 8188 8138 158 8888 991118 381 91781900111913901 017 0199 19 1291194111691118 11021 23915997

Everything typed before the chaos is as you'd expect it, just the natural numbers ascending.

Does anyone have any idea why it devolves into chaos? Thanks in advance!

Edit: I know this can be easily circumvented by adding something of an interval, but our question concerns why this happens in the first place.

Upvotes: 1

Views: 1437

Answers (2)

Basem Ayad
Basem Ayad

Reputation: 37

In regards your question on why this error happened I don't know behavior.

I don't know what happens that cause things like:

91781900111913901 017 0199 19 1291194111691118 

But I wanna ask is it meant to start from the beginning each time it ends a run? I mean I modified ur friend's code a bit to test it out like this:

from time import sleep
import keyboard
import pyautogui

x = ""

# Some time to switch to notepad before pyautogui spams a bunch of characters
sleep(5)

# Add numbers to the string along with a space
while True:
    if keyboard.is_pressed('1'):
        for i in range(1, 2001):
            x += str(i) + " "
            if keyboard.is_pressed('2'):
                break

# "Simulate" the keyboard writing out the entire string
            pyautogui.write(x, interval=0.2)

And the output is in the first time I press 1 to active it prints the nothing that's inside "" and finishes and needs me to press 1 again to activate it and this time it goes like this sample:

1 #as u guess this is the first time that print nothing# 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 11 1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12 

I mean if you want to print out from 0 to 2000 why not do this:

if keyboard.is_pressed('r'):
        for i in range(1, 2001):
            newI=str(i).rjust(6)
            keyboard.write(newI,delay=0.0002)

It gives out like this :

996   997   998   999  1000  1001  1002  1003  1004  1005  1006  1007  1008  1009  1010  1011  1012  1013  1014  1015  1016  1017  1018  1019  1020  1021  1022  1023  1024  1025  1026  1027  1028  1029  1030  1031  1032  1033  1034  1035  1036  1037  1038  1039  1040  1041  1042  1043  1044  1045  1046  1047  1048  1049  1050  1051  1052  1053  1054  1055  1056  1057  1058  1059  1060  1061  1062  1063  1064  1065  1066  1067  1068  1069  1070  1071  1072  1073  1074  1075  1076  1077  1078  1079  1080  1081  1082  1083  1084  1085  1086  1087  1088  1089  1090  1091  1092  1093  1094  1095  1096  1097  1098  1099  1100  1101  1102  1103  1104  1105  1106  1107  1108  1109  1110  1111  1112  1113  1114  1115  1116  1117  1118  1119  1120  1121  1122  1123  1124  1125  1126  1127  1128  1129  1130  1131  1132  1133  1134  1135  1136  1137  1138  1139  1140  1141  1142  1143  1144  1145  1146  1147  1148  1149  1150  1151  1152  1153  1154  1155  1156  1157  1158  1159  1160  1161  1162  1163  1164  1165  1166  1167  1168  1169  1170  1171  1172  1173  1174  1175  1176  1177  1178  1179  1180  1181  1182  1183  1184  1185  1186  1187  1188  1189  1190  1191  1192  1193  1194  1195  1196  1197  1198  1199  1200  1201  1202  1203  1204  1205  1206  1207  1208  1209  1210  1211  1212  1213  1214  1215  1216  1217  1218  1219  1220  1221  1222  1223  1224  1225  1226  1227  1228  1229  1230  1231  1232  1233  1234  1235  1236  1237  1238  1239  1240  1241  1242  1243  1244  1245  1246  1247  1248  1249  1250  1251  1252  1253  1254  1255  1256

And keeps working normally I just didn't copy that much but regards your question on why this error happened I don't know the behavior.

Upvotes: 1

Kelsey
Kelsey

Reputation: 483

I recommend using:

    pyautogui.write(x, interval=0.25)

This way, the typing speed is consistent across the board.

I suspect what is happening in your friend's case is that pyautogui is typing too quickly and the characters get lost. Adding a delay interval should fix the problem; I had the same issue.

I recommend following their docs page to learn more about it's usage: https://pyautogui.readthedocs.io/en/latest/keyboard.html

Upvotes: 2

Related Questions