I'm looking for a way to create a PowerShell script that can send modifier keys like Ctrl, Shift, Alt, or Win for a duration of 800ms. The tricky part is that it should stop immediately if the Esc key is pressed by the user. I'd like it to work alongside other keyboard or mouse inputs, for example, sending Ctrl along with scrolling the mouse to zoom in and out.
Previously, I tried using ChatGPT for help, but I got lost in the explanations. I'm considering sharing my code if that would help.
I've also asked around on AutoHotkey, and I was told that modifying keys might not be able to be sent alone. Is that a universal limitation or just specific to AutoHotkey? In my experiments with F-Key Sender using certain virtual key codes, I found that it is possible to send Ctrl and Shift alone, and that worked for zoom and scrolling.
I have two versions of the script I'm working on, which I would be happy to share for any insights or improvements!
4 Answers
It seems like you're dealing with a classic XY problem. Before diving deep into coding, can you clarify what you're trying to achieve with these key presses? Sometimes, PowerShell isn't the ideal tool for certain tasks, and depending on your end goal, there might be better solutions!
From what I know, PowerShell is not the best fit for sending modifier keys alone. You get better results with AutoHotkey. It allows for more robust keyboard and mouse interactions. You could set up a simple macro that would accomplish what you want much faster than scripting it in PowerShell.
Just a heads up, there are macro functions in many gaming keyboards that can do what you're trying to achieve in a second. It might be worth looking into those instead of coding from scratch!
Very true! Sometimes the simplest solution is just using the tools we already have.
Looks like you're missing the point of mixing modifier keys and mouse actions. PowerShell isn't built for this kind of automation as effectively as others. You might want to check out AutoHotkey instead; it's specifically designed for tasks like sending key presses along with mouse actions. Plus, you should try to format your code properly when asking questions—makes it easier for others to help!
Totally! Proper formatting can change how people perceive your question and help them assist you better.

I agree! Knowing the actual goal can help suggest better tools or methods.