How to Send Keys using PowerShell and WScript (Simulate Keystrokes)steemCreated with Sketch.

in programming •  2 years ago 

Sometimes, you want to emulate keystrokes (key presses) for example, you want to automate some tasks which are easier to do by repeating keystrokes than looking up APIs and write a decent script/program. For example, you want to open any chat and type in "Hello!" 100 times, you can write a script using Powershell, which first sleeps a few seconds, and then simulate keystrokes:

You can do this via Creating a Wscript.Shell COM (Component Object Model) object on Windows using Powershell script.

$WShell = New-Object -com "Wscript.Shell"
$WShell.sendkeys("Hello!")

To repeat 100 times, simply do this in Powershell (FOR loop):

$WShell = New-Object -com "Wscript.Shell"
for (($i = 0), ($j = 0); $i -lt 10; $i++)
{
    $WShell.sendkeys("Hello!")
    $WShell.sendkeys("{ENTER}")
}

Powershell Script to Stay Online/Available in Chat Software using Keystrokes


Based on this, we can send Scroll-Lock Keystrokes every few interval until script is terminated/killed. Some keyboards don't even have this scroll-lock keys. The idea is to emulate the keystrokes so that your chat applications (Microsoft Teams, Slack, Discord, Google Chats, Telegram etc) assume you are still online - and thus will not make your status to "Away" (AFK - Away From Keyboard).

$WShell = New-Object -com "Wscript.Shell"

while ($true)
{
  $WShell.sendkeys("{SCROLLLOCK}")
  Start-Sleep -Milliseconds 100
  $WShell.sendkeys("{SCROLLLOCK}")
  Start-Sleep -Seconds 120
}

--EOF (The Ultimate Computing & Technology Blog) --

Post: How to Send Keys using PowerShell and WScript (Simulate Keystrokes)

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  
  ·  2 years ago 

!test !thumbsup

Hello @justyy! You are amazing!


command: !thumbsup is powered by witness @justyy and his contributions are: https://steemyy.com
More commands are coming!