Answered by:
Script using Windows PowerShell ISE to move mouse and control keyboard

Question
-
Hello,
I am new to PowerShell scripting, and fairly new to scripting in general.
I would like to create a script that would move the mouse to a certain location, double click, enter a number, click tab 3 three times, click spacebar, close window.
I started with: [System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point(230,175);
But I couldn't find how to double click.
I would appreciate your help very much.
Wednesday, October 10, 2012 5:33 PM
Answers
-
Have a look at WASP
[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
- Marked as answer by wolf80 Wednesday, October 10, 2012 9:10 PM
Wednesday, October 10, 2012 7:57 PM
All replies
-
Maybe something like this. If the button your trying to push has a hotkey, even better.
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}{ENTER}")
Don't forget to mark your posts as answered so they drop off the unanswered post filter. If I've helped you and you want to show your gratitude, just click that green thingy.
- Edited by Knuckle-Dragger Wednesday, October 10, 2012 5:52 PM
Wednesday, October 10, 2012 5:51 PM -
That would be useful for the next steps, but it doesn't simulate a mouse double-click.
Thanks.
Wednesday, October 10, 2012 6:09 PM -
Have a look at the vb code here
http://www.vbforums.com/showthread.php?620353-Simulate-Mouse-Click
it references an event in a library which I believe you should be able to impliment in powershell...
that should get you started (and may expose other interesting mouse related events that you could use to help you finish your task)
Cheers
Jamie Courtes MCTS - SCCM 2007 MCTS - SCCM 2012
Wednesday, October 10, 2012 6:37 PM -
I'm not exactly sure how to integrate that. As I said, I'm new to Powershell...
Isn't there a simple command to left-click?
Another option I can use is to select a specific window by entering its name into the script.
Wednesday, October 10, 2012 7:50 PM -
Hi wolf,
there is no native command that i know of, and if you are strictly looking at automating mouse/kb input, there is a plethora of macro recording programs out there, powershell is not your best choice...
If you are adamant about using powershell, then I believe what I suggested might be your best course of attack
Hope that helps!
Jamie
Jamie Courtes MCTS - SCCM 2007 MCTS - SCCM 2012
Wednesday, October 10, 2012 7:56 PM -
Have a look at WASP
[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
- Marked as answer by wolf80 Wednesday, October 10, 2012 9:10 PM
Wednesday, October 10, 2012 7:57 PM -
Thanks. WASP was very useful.
Wednesday, October 10, 2012 9:10 PM -
##In order to move the mouse position you could use something like this:
[system.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | out-null
[System.Windows.Forms.Cursor]::Position = New-Object System.Drawing.Point(500,500)##In order to simulate a mouse click you could use something like this:
function Click-MouseButton
{
param(
[string]$Button,
[switch]$help)
$HelpInfo = @'
Function : Click-MouseButton
Purpose : Clicks the Specified Mouse Button
Usage : Click-MouseButton [-Help][-Button x]
where
-Help displays this help
-Button specify the Button You Wish to Click {left, middle, right}
'@
if ($help -or (!$Button))
{
write-host $HelpInfo
return
}
else
{
$signature=@'
[DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
'@
$SendMouseClick = Add-Type -memberDefinition $signature -name "Win32MouseEventNew" -namespace Win32Functions -passThru
if($Button -eq "left")
{
$SendMouseClick::mouse_event(0x00000002, 0, 0, 0, 0);
$SendMouseClick::mouse_event(0x00000004, 0, 0, 0, 0);
}
if($Button -eq "right")
{
$SendMouseClick::mouse_event(0x00000008, 0, 0, 0, 0);
$SendMouseClick::mouse_event(0x00000010, 0, 0, 0, 0);
}
if($Button -eq "middle")
{
$SendMouseClick::mouse_event(0x00000020, 0, 0, 0, 0);
$SendMouseClick::mouse_event(0x00000040, 0, 0, 0, 0);
}
}
}##To use this function in your script, implement a command like: Click-MouseButton -Button left
##Or you could just use WASP..
Monday, June 9, 2014 11:30 AM