My first thought was using the "USB" action, but as mentioned, this action only picks up Media devices (please, let me know how can I expand that to every USB device or similar, as that will help to simplify everything).
Then, I've tried to run the "Run" action using a simplified script of the current one using exit codes to make the changes accordingly, but it doesn't seem to either run the script or to retrieve the exit codes, so it doesn't work directly running the script in the plugin.
So I've modified the original script a bit to achieve this by exporting the outcome of the exit codes to a file instead of actually generating exit codes (also tried to use variables in the plugin to retrieve the codes but no luck whatsoever).
Right now the logic is the same but exports exit codes 0, 1, 2, 3, or 4 depending on the case, then in the plugin I set the "File" action to match those codes to change the scenes.
Here I add the script and a screenshot of the Macros as they are now.
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class UserActivity
{
[DllImport("user32.dll")]
public static extern int GetAsyncKeyState(Int32 i);
[DllImport("user32.dll")]
public static extern bool GetCursorPos(out POINT lpPoint);
public struct POINT
{
public int X;
public int Y;
}
private static int wasdCount = 0;
private static DateTime lastKeyPressTime = DateTime.Now;
public static bool IsGamingPatternDetected()
{
int[] keys = { 0x57, 0x41, 0x53, 0x44 }; // W, A, S, D key codes
foreach (int key in keys)
{
if (GetAsyncKeyState(key) != 0)
{
wasdCount++;
lastKeyPressTime = DateTime.Now;
break;
}
}
// Reset count if no key press detected for a while
if ((DateTime.Now - lastKeyPressTime).TotalSeconds > 1)
{
wasdCount = 0;
}
// Check if the pattern matches frequent key presses
if (wasdCount > 10) // Adjust this threshold as needed
{
wasdCount = 0; // Reset count after detection
return true;
}
return false;
}
public static bool IsMouseMoved()
{
POINT currentPos;
GetCursorPos(out currentPos);
if (currentPos.X != lastPos.X || currentPos.Y != lastPos.Y)
{
lastPos = currentPos;
return true;
}
return false;
}
private static POINT lastPos;
}
"@
# Define the output file path
$outputFilePath = "C:\temp\exit_code.txt"
# Ensure the directory exists
$outputDir = [System.IO.Path]::GetDirectoryName($outputFilePath)
if (-not (Test-Path -Path $outputDir)) {
New-Item -Path $outputDir -ItemType Directory -Force
}
# Function to get the current exit code from the file
function Get-CurrentExitCode {
if (Test-Path -Path $outputFilePath) {
return Get-Content -Path $outputFilePath -Raw
} else {
return -1
}
}
while ($true) {
# Check for Xbox controller
$xbox = Get-PnpDevice -PresentOnly | Where-Object { $_.Name -like "*Xbox Controller" -and $_.Status -eq "Ok"}
if ($xbox) {
$xbox_status = "Connected"
Write-Output "Xbox controller is connected"
} else {
$xbox_status = "Disconnected"
Write-Output "Xbox controller is not connected"
}
# Check for PS5 controller
$ps5 = Get-PnpDevice -PresentOnly | Where-Object { $_.Name -like "DualSense Wireless Controller" -and $_.Status -eq "Ok"}
if ($ps5) {
$ps5_status = "Connected"
Write-Output "PS5 controller is connected"
} else {
$ps5_status = "Disconnected"
Write-Output "PS5 controller is not connected"
}
# Determine exit code based on controller status
if ($xbox_status -eq "Connected" -and $ps5_status -eq "Connected") {
Write-Output "2 Controllers connected"
$exitCode = 3
} elseif ($xbox_status -eq "Connected" -and $ps5_status -eq "Disconnected") {
Write-Output "Xbox Controller connected"
$exitCode = 2
} elseif ($xbox_status -eq "Disconnected" -and $ps5_status -eq "Connected") {
Write-Output "PS5 Controller connected"
$exitCode = 1
} else {
Write-Output "All Controllers Disconnected"
# Check for keyboard and mouse activity
$activityDetected = $false
$duration = 10 # Duration in seconds
$endTime = (Get-Date).AddSeconds($duration)
while ((Get-Date) -lt $endTime) {
if ([UserActivity]::IsGamingPatternDetected() -and [UserActivity]::IsMouseMoved()) {
$activityDetected = $true
break
}
Start-Sleep -Milliseconds 100
}
if ($activityDetected) {
Write-Output "User is using keyboard and mouse in a gaming pattern"
$exitCode = 4
} else {
Write-Output "No gaming keyboard and mouse activity detected"
$exitCode = 0
}
}
# Get the current exit code
$currentExitCode = Get-CurrentExitCode
# Write the exit code to a file only if it has changed
if ($exitCode -ne $currentExitCode) {
Set-Content -Path $outputFilePath -Value $exitCode
Write-Output "Exit code updated to $exitCode"
} else {
Write-Output "Exit code remains unchanged"
}
# Wait based on keyboard activity detection
$waitTime = if ($activityDetected) { 600 } else { 30 } # 10 minutes or 30 seconds
$elapsedTime = 0
while ($elapsedTime -lt $waitTime) {
# Check for controller connection during wait time
$xbox = Get-PnpDevice -PresentOnly | Where-Object { $_.Name -like "*Xbox Controller" -and $_.Status -eq "Ok"}
$ps5 = Get-PnpDevice -PresentOnly | Where-Object { $_.Name -like "DualSense Wireless Controller" -and $_.Status -eq "Ok"}
if ($xbox -or $ps5) {
Write-Output "Controller connected during wait time"
break
}
Start-Sleep -Seconds 10
$elapsedTime += 10
}
}
View attachment 107497
Thank you in advance.