Moving the Mouse

Standard

As a first post, I thought I would start with something fun – moving the mouse! To be noted – this is not the same as simulating user input. I’ll save that for a later post.

More features may get added later – timers, sound alert, maybe others. For now, here is a simple script that will either move the mouse from left to right, or top to bottom.

All comments welcome!

<#
.Synopsis
Moves the mouse cursor either from left-to-right
or top-to-bottom indefinitely.
.EXAMPLE
.\Move-Mouse_v1_0.ps1 -MotionType LeftToRight
.EXAMPLE
.\Move-Mouse_v1_0.ps1 -MotionType TopToBottom
.EXAMPLE
Press Ctrl+C to stop the script
#>

[CmdletBinding()]
Param
(
    [Parameter(Mandatory=$True,Position=1)]
    [ValidateNotNullOrEmpty()]
[ValidateSet("LeftToRight","TopToBottom")]
    [string[]]$MotionType = @("LeftToRight","TopToBottom")
)

BEGIN
{
    Function Load-Assemblies
    {
        [System.Reflection.Assembly]::LoadWithPartialName(‘System.Windows.Forms’) | Out-Null
    }

    Function Get-MonitorResolution
    {
        $script:screen = [System.Windows.Forms.SystemInformation]::VirtualScreen
        $script:monitorHeight = $script:screen.Height
        $script:monitorWidth = $script:screen.Width
    }

    Function Move-Mouse
    {
        $script:startHeight = [int]$script:monitorHeight/2
        $script:startWidth = [int]$script:monitorWidth/2

        if($MotionType -eq "LeftToRight")
        {
            Move-LeftToRight
        }

        if($MotionType -eq "TopToBottom")
        {
            Move-TopToBottom
        }
    }

    Function Move-LeftToRight
    {
        $xPos = 0

        # Initial Cursor Position
        [System.Windows.Forms.Cursor]::Position = "$($xPos),$($script:startHeight)"

        # Keep moving right until at the edge of the screen
        do
        {
            $xPos++
            [System.Windows.Forms.Cursor]::Position ="$($xPos),$($script:startHeight)"
Start-Sleep -Milliseconds .5
        }
        until ($xPos -eq $script:monitorWidth)

        # Keep moving left until at the edge of the screen
        do
        {
            $xPos--
            [System.Windows.Forms.Cursor]::Position ="$($xPos),$($script:startHeight)"

            # Adjust to increase or decrease mouse speed
            Start-Sleep -Milliseconds .5
        }
        until ($xPos -eq 0)

        # Repeat indefinitely
        Move-LeftToRight
    }

    Function Move-TopToBottom
    {
        $yPos = 0

        # Initial Cursor Position
        [System.Windows.Forms.Cursor]::Position = "$($script:startWidth),$($yPos)"

        # Keep moving down until at the edge of the screen
        do
        {
            $yPos++
            [System.Windows.Forms.Cursor]::Position ="$($script:startWidth),$($yPos)"
            Start-Sleep -Milliseconds .5
        }
        until ($yPos -eq $script:monitorHeight)

        # Keep moving up until at the edge of the screen
        do
        {
            $yPos--
            [System.Windows.Forms.Cursor]::Position ="$($script:startWidth),$($yPos)"

            # Adjust to increase or decrease mouse speed
            Start-Sleep -Milliseconds .5
        }
        until ($yPos -eq 0)

        # Repeat indefinitely
        Move-TopToBottom
    }
}

PROCESS
{
    Load-Assemblies
    Get-MonitorResolution
    Move-Mouse
}

END
{

}

2 thoughts on “Moving the Mouse

  1. hello my friend , i found you on your username in stackoverflow website , i need to talk with you for powershell problem and i have a question , can you help me for that ? , please tell me your phone number Socail media like ( whatsapp or any other app ) or email address for tell i question , my phone number is +989173371057 and i use whatsapp , I tried very hard to be able to post to your personal account and finally I succeeded in this place thanks bro , regards

    Like

Leave a comment