-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPSQuakeify.psm1
71 lines (59 loc) · 1.91 KB
/
PSQuakeify.psm1
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
function Install-QuakeHotkey {
[CmdletBinding()]
Param(
[int]$WindowHandle = (Get-Process -Id $PID).MainWindowHandle
)
$ErrorActionPreference = "Stop"
Write-Verbose "Reading hotkey handler code"
$HotkeyHandlerCS = Get-Content (Join-Path $PSScriptRoot "HotkeyHandler.cs") -Raw
Write-Verbose "Starting hotkey handler job"
try {
$JobInfo = Start-Job -Name "Quakeify" -ArgumentList @([int]$WindowHandle, $HotkeyHandlerCS) -ScriptBlock {
Param(
[int]$TargetWindowHandle,
[string]$HotkeyHandlerWindowCode
)
$ErrorActionPreference = "Stop"
if($TargetWindowHandle -eq $null) { throw "No target window handle" }
Add-Type -TypeDefinition $HotkeyHandlerWindowCode -ReferencedAssemblies System.Windows.Forms
$form = New-Object WindowsApplication1.Form1
$form.TargetConsoleWindow = $TargetWindowHandle
if ($form.RegisterHotkey()){
Write-Output "Registered" # Let the main script know we're running
$form.Show();
while ($true) {
Start-Sleep -Milliseconds 50
[System.Windows.Forms.Application]::DoEvents()
}
} else {
Write-Output "Failed"
}
}
# Wait for output from the job
Write-Verbose "Waiting for job output"
for ($i = 0; $i -lt 10; $i++) {
if($JobInfo.HasMoreData){
$JobOutput = Receive-Job -Id $JobInfo.Id
Write-Verbose "Received job output: $JobOutput"
# For some reason you can receive empty results - ignore those
if([string]::IsNullOrEmpty($JobOutput)){
Write-Verbose "Received empty output - still waiting"
} else {
break
}
}
Write-Verbose "Still waiting for job to return something..."
Start-Sleep -Milliseconds 200
}
if ($JobOutput -ne "Registered"){
throw "Failed to register hotkey"
}
Write-Verbose "Hotkey installed!"
} catch {
Remove-Job -Id $JobInfo.Id -Force
Write-Error "Failed to register hotkey"
}
}
function Remove-QuakeHotkey {
Get-Job -Name "Quakeify" | Remove-Job -Force
}