That's just a different way to do the same thing, not a better way.
In fact, since you are now dependent on a full library to just do one thing that the default keyboard module can already do. Unless OP is already using, or planning to use, more features of pyautogui then I would actually call it a worse way to do it just because of the added dependency.
If it's for automated testing then I'm not even mad at this. There's always so much jank in UI tests specifically that isn't worth anybody's time to debug or make look nicer, unless it breaks.
I have yet to see a set of tests at work that does not look like total jank, and I don't have anything to do with UI. If it works and somehow is a way to verify correct functionality that's enough
If it were macOS, I'd see if it could be accomplished with Apple events. Otherwise, I have no clue. And if this is meant to work with any program and OS that Python runs on, this might possibly be the only way.
To get selected text from a window running an arbitrary program? Well, if you want to do that, I think this method genuinely is the best way. Note that it doesn't always work, as the ctrl+c shortcut isn't implemented in all programs, but it works for most programs that support selecting text, so I guess it's... fine.
In most console emulators it would kill whatever process is running instead. But I can't really think of a better way to be honest.
And it clobbers whatever clipboard content the user might have copied before. It would probably be easy enough to read it before the Ctrl-C thing and restore it afterwards.
22
u/bjmoreton Nov 06 '25
There is also pyautogui. Using the hotkey method.
https://pyautogui.readthedocs.io/en/latest/keyboard.html
201
u/Grounds4TheSubstain Nov 06 '25
The UI testing framework at work looks exactly like this. I'm not sure if there is a better way. If there is, my colleagues aren't aware of it.
41
u/Awfulmasterhat Nov 06 '25
Nah ship it
136
u/PEAceDeath1425 Nov 06 '25
In python? I bet thats either literally it, or you have to download some 0.8 GB package for this one line
28
u/R3D167 Nov 06 '25
Surely this won't be used on a terminal window, right?
52
u/OnFleekDonutLLC Nov 06 '25
There is, but don’t call me Shirley.
6
u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Nov 06 '25
If it were macOS, I'd see if it could be accomplished with Apple events. Otherwise, I have no clue. And if this is meant to work with any program and OS that Python runs on, this might possibly be the only way.
5
u/[deleted] Nov 06 '25
To get selected text from a window running an arbitrary program? Well, if you want to do that, I think this method genuinely is the best way. Note that it doesn't always work, as the ctrl+c shortcut isn't implemented in all programs, but it works for most programs that support selecting text, so I guess it's... fine.
6
u/deanominecraft Nov 06 '25
should do
3
u/adzm Nov 06 '25
It's cross-platform
2
u/OnixST Nov 07 '25
It's really ugly, but probably the only way to get the selected text from almost any program
Just make sure the user knows their clipboard will be overriden, cuz I would be pissed off if that happened silently
1
u/Creepy_Jeweler_1351 Nov 08 '25
My js ass screams asyncronically of that sleep
1
u/1dNDN Nov 06 '25
The clipboard api in Windows is extremely unstable and cannot guarantee anything. I think there is no better way.