-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype-in-window.ps1
146 lines (125 loc) · 4.9 KB
/
type-in-window.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<#
Copyright (C) 2024 Ali Almahdi
This script is part of Ali's powershell scripts repository on GitHub
Licensed under GNU AGPL-3.0 with Commons Clause License Condition v1.0
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version, with Commons Clause License Condition v1.0.
For commercial licensing inquiries, contact: https://www.ali.ac/contact
#>
# Import required assemblies
Add-Type -AssemblyName System.Windows.Forms
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class Win32 {
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
}
"@
function Get-WindowTitles {
$windows = Get-Process | Where-Object {$_.MainWindowTitle -ne ""} |
Select-Object MainWindowTitle, MainWindowHandle
return $windows
}
function Set-ActiveWindow {
param([IntPtr]$WindowHandle)
try {
[Win32]::SetForegroundWindow($WindowHandle)
Start-Sleep -Milliseconds 100
return $true
} catch {
Write-Warning "Failed to activate window: $_"
return $false
}
}
function Send-KeyStrokes {
param([string]$Text)
try {
[System.Windows.Forms.SendKeys]::SendWait($Text)
return $true
} catch {
Write-Warning "Failed to send keystrokes: $_"
return $false
}
}
function Show-AboutWindow {
$aboutForm = New-Object System.Windows.Forms.Form
$aboutForm.Text = "About"
$aboutForm.Size = New-Object System.Drawing.Size(300,200)
$aboutForm.StartPosition = "CenterParent"
$aboutForm.FormBorderStyle = "FixedDialog"
$aboutForm.MaximizeBox = $false
$aboutForm.MinimizeBox = $false
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,120)
$label.Text = "Type in Window by Ali Almahdi`nhttps://www.ali.ac`n`nVersion 1.0`n`nA simple utility to type text into other windows.`n`nCreated with PowerShell"
$aboutForm.Controls.Add($label)
$aboutForm.ShowDialog()
}
# Create the form
$form = New-Object System.Windows.Forms.Form
$form.Text = "Type in Window"
$form.Size = New-Object System.Drawing.Size(400,240)
$form.StartPosition = "CenterScreen"
$form.FormBorderStyle = "FixedSingle"
$form.MaximizeBox = $false
# Window selection label
$windowLabel = New-Object System.Windows.Forms.Label
$windowLabel.Location = New-Object System.Drawing.Point(10,10)
$windowLabel.Size = New-Object System.Drawing.Size(280,15)
$windowLabel.Text = "Select Window:"
# Window selection combo box
$comboBox = New-Object System.Windows.Forms.ComboBox
$comboBox.Location = New-Object System.Drawing.Point(10,30)
$comboBox.Size = New-Object System.Drawing.Size(280,20)
# Refresh button
$refreshButton = New-Object System.Windows.Forms.Button
$refreshButton.Location = New-Object System.Drawing.Point(300,30)
$refreshButton.Size = New-Object System.Drawing.Size(80,20)
$refreshButton.Text = "Refresh"
$refreshButton.Add_Click({
$comboBox.Items.Clear()
Get-WindowTitles | ForEach-Object {
$comboBox.Items.Add($_)
}
})
# Text input label
$textLabel = New-Object System.Windows.Forms.Label
$textLabel.Location = New-Object System.Drawing.Point(10,60)
$textLabel.Size = New-Object System.Drawing.Size(370,15)
$textLabel.Text = "Text to Type:"
# Text input
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,80)
$textBox.Size = New-Object System.Drawing.Size(370,20)
# Send button
$sendButton = New-Object System.Windows.Forms.Button
$sendButton.Location = New-Object System.Drawing.Point(10,120)
$sendButton.Size = New-Object System.Drawing.Size(370,30)
$sendButton.Text = "Type Text in Selected Window"
$sendButton.Add_Click({
if ($comboBox.SelectedItem -and $textBox.Text) {
$window = $comboBox.SelectedItem
if (Set-ActiveWindow -WindowHandle $window.MainWindowHandle) {
Send-KeyStrokes -Text $textBox.Text
}
} else {
[System.Windows.Forms.MessageBox]::Show("Please select a window and enter text")
}
})
# About button
$aboutButton = New-Object System.Windows.Forms.Button
$aboutButton.Location = New-Object System.Drawing.Point(10,160)
$aboutButton.Size = New-Object System.Drawing.Size(370,30)
$aboutButton.Text = "About"
$aboutButton.Add_Click({ Show-AboutWindow })
# Add controls to form
$form.Controls.AddRange(@($windowLabel, $comboBox, $refreshButton, $textLabel, $textBox, $sendButton, $aboutButton))
# Initial window list population
$refreshButton.PerformClick()
# Show form
$form.ShowDialog()