Files
keypresser/KeyPresser.ps1
2020-11-09 15:37:49 -06:00

116 lines
3.4 KiB
PowerShell

#region Header
$Version = "1.0.2"
#Dependencies
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName PresentationFramework
Hide Console Window
Add-Type -Name Window -Namespace Console -MemberDefinition '
[DllImport("Kernel32.dll")]
public static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
'
$consolePtr = [Console.Window]::GetConsoleWindow()
[Console.Window]::ShowWindow($consolePtr, 0) > $null
#Talking across runspaces
$Sync = [Hashtable]::Synchronized(@{})
#endregion Header
#region Functions
Function Keypress {
Param ([string]$Keys)
$script = [Powershell]::Create().AddScript({
Param ($Keys)
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class UserWindows {
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
}
"@
$keys = $keys -split ","
while($sync["x"] -eq 1){
$ActiveHandle = [UserWindows]::GetForegroundWindow()
$Process = Get-Process | ? {$_.MainWindowHandle -eq $activeHandle} | Select-Object -ExpandProperty ProcessName
Write-Output $Process
Foreach ($key in $keys){
if ($sync["x"] -eq 1 -and $Process -eq "sro_client"){
#Start-Sleep -Milliseconds 50
[System.Windows.Forms.SendKeys]::SendWait("{$key}")
}
}
}
}).AddArgument($Keys)
$runspace = [RunspaceFactory]::CreateRunspace()
$runspace.ApartmentState = "STA"
$runspace.Threadoptions = "ReuseThread"
$runspace.open()
$runspace.sessionStateProxy.SetVariable("sync", $sync)
$script.runspace = $runspace
$script.beginInvoke()
}
#endregion Functions
#region Form
$form = New-Object System.Windows.Forms.Form
$form.text = "Keypresser Version $Version"
$Form.Size = "450,110"
$Form.FormBorderStyle = 'FixedDialog'
$form.TopMost = $True
$Form.Add_FormClosing({Get-Process -id $pid | Stop-Process})
$KeysLabel = New-Object 'System.Windows.Forms.Label'
$KeysLabel.text = "Enter in the keys you wish to be pressed seperated by a comma `",`""
$KeysLabel.Width = '425'
$KeysLabel.Location = New-Object System.Drawing.Point(5,5)
$form.controls.add($KeysLabel)
$KeysTextBox = New-Object 'System.Windows.Forms.TextBox'
$KeysTextBox.Location = New-Object System.Drawing.Point(5,25)
$KeysTextBox.Width = '425'
$form.controls.add($KeysTextBox)
$KeysButton = New-Object 'System.Windows.Forms.Button'
$KeysButton.Location = New-Object System.Drawing.Point(5,45)
$KeysButton.text = "Start"
$KeysButton.add_click({
if ($KeysButton.text -ne "Stop"){
$KeysButton.text = "Stop"
$sync["x"] = 1
$Keys = $KeysTextBox.text
Keypress $Keys
}
Else{
$KeysButton.text = "Start"
$sync["x"] = 0
}
})
$form.controls.add($KeysButton)
#endregion Form
[Windows.Forms.Application]::Run($form)