Skip to content

Instantly share code, notes, and snippets.

@bengalih
Last active August 31, 2024 16:51
Show Gist options
  • Save bengalih/d3ec0d618142e699e91367a6e917cfee to your computer and use it in GitHub Desktop.
Save bengalih/d3ec0d618142e699e91367a6e917cfee to your computer and use it in GitHub Desktop.
# scrcpy-pin-unlock.ps1
# v.0.1 - 8/30/24
# bengalih
# SETUP THE BELOW VALUES FOR YOUR SYSTEM
###########################################################
# Name of your scrcpy window
$windowTitle = "Pixel 3 XL"
# Full path to scrcpy's adb.exe
$adbPath = "C:\Program Files\scrcpy-win64-v2.6.1\adb.exe"
# Your phone PIN to unlock
$PIN = "1234" # THIS IS YOUR PHONE PIN
# The coordinates to simulate a swipe. Format is {startY startY endX endY}
# If you need to change, most likely it is the Y points depending on your resolution
$swipeCoordinates = "800 1800 800 200"
# Function to bring the window to the foreground
###########################################################
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);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
public const int SW_RESTORE = 9;
}
"@
# Find the window handle by title
$hwnd = [Win32]::FindWindow([NullString]::Value, $windowTitle)
if ($hwnd -eq [IntPtr]::Zero) {
Write-Host "Window '$windowTitle' not found."
exit 1
}
# Restore the window if minimized and bring it to the foreground
[Win32]::ShowWindow($hwnd, [Win32]::SW_RESTORE) | Out-Null
[Win32]::SetForegroundWindow($hwnd) | Out-Null
# Execute the ADB commands
& $adbPath shell input keyevent KEYCODE_WAKEUP
Start-Sleep -Milliseconds 100
& $adbPath shell input swipe $swipeCoordinates
Start-Sleep -Milliseconds 100
& $adbPath shell input text $PIN
& $adbPath shell input keyevent KEYCODE_ENTER
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment