Created
October 21, 2018 21:07
-
-
Save y-ack/9c0f68604522a0132688e91ff44d0562 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add-Type @" | |
using System; | |
using System.Runtime.InteropServices; | |
public class Win32 { | |
[DllImport("user32.dll")] | |
public static extern void mouse_event(int flags, int dx, int dy, int cButtons, int info); | |
[DllImport("user32.dll")] | |
[return: MarshalAs(UnmanagedType.Bool)] | |
public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect); | |
[DllImport("user32.dll")] | |
[return: MarshalAs(UnmanagedType.Bool)] | |
public static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect); | |
[DllImport("user32.dll")] | |
[return: MarshalAs(UnmanagedType.Bool)] | |
public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint); | |
[DllImport("user32.dll")] | |
[return: MarshalAs(UnmanagedType.Bool)] | |
public static extern bool SetForegroundWindow(IntPtr hWnd); | |
} | |
public struct RECT | |
{ | |
public int Left; // x position of upper-left corner | |
public int Top; // y position of upper-left corner | |
public int Right; // x position of lower-right corner | |
public int Bottom; // y position of lower-right corner | |
} | |
"@ | |
$rcWindow = New-Object RECT | |
$rcClient = New-Object RECT | |
$DisplayInfo = (Get-WmiObject -Class Win32_VideoController) | |
[int]$MOUSEEVENTF_ABSOLUTE = 0x8000 | |
[int]$MOUSEEVENTF_LEFTDOWN = 0x0002 | |
[int]$MOUSEEVENTF_LEFTUP = 0x0004 | |
function do_hit { | |
Param([int]$X, [int]$Y) | |
[int]$Absolute_X = $rcWindow.Left + $X * (($rcWindow.Right - $rcWindow.Left)/416) | |
[int]$Absolute_Y = $rcWindow.Top + 51 + $Y * (($rcWindow.Bottom - $rcWindow.Top)/568) | |
Write-Host $X, $Absolute_X, $Y, $Absolute_Y, "`n", $rcWindow.Left, (($rcWindow.Right - $rcWindow.Left)/416) | |
[float]$X_Mickeys = ($Absolute_X / $DisplayInfo.CurrentHorizontalResolution) * 65535 | |
[float]$Y_Mickeys = ($Absolute_Y / $DisplayInfo.CurrentVerticalResolution) * 65535 | |
#0x1 : read movement data | |
[Win32]::mouse_event($MOUSEEVENTF_ABSOLUTE -bor $MOUSEEVENTF_LEFTDOWN + 0x1, | |
$X_Mickeys, $Y_Mickeys, 0, 0) | |
Start-Sleep -Milliseconds 1.5 | |
[Win32]::mouse_event($MOUSEEVENTF_LEFTUP, 0, 0, 0, 0) | |
[Win32]::SetForegroundWindow((Get-Process -id $pid).MainWindowHandle) | |
} | |
function switch_keyboard { | |
Param([String[]]$Keyboard) | |
switch ($Keyboard) { | |
$UpperAlpha { | |
if($CurrentKeyboard -eq $LowerAlpha) { do_hit -X (50) -Y ($top_offset + 176) } | |
elseif ($CurrentKeyboard -eq $UpperAlpha) { } | |
$CurrentKeyboard = $UpperAlpha | |
$CurrentKeyMap = $UpperAlphaKeyMap | |
} | |
$LowerAlpha { | |
if($CurrentKeyboard -eq $UpperAlpha) { do_hit -X (50) -Y ($top_offset + 176) } | |
elseif ($CurrentKeyboard -eq $LowerAlpha) { } | |
$CurrentKeyboard = $LowerAlpha | |
$CurrentKeyMap = $LowerAlphaKeyMap | |
} | |
} | |
} | |
$Handle = (Get-Process |where {$_.mainWindowTitle -match "Citra.*SmileBASIC"})[0].MainWindowHandle | |
[Win32]::GetWindowRect($Handle,[ref]$rcWindow) | |
[Win32]::GetClientRect($Handle,[ref]$rcClient) | |
$UpperAlphaKeyMap = @(("!","`"","#","$","%","&","(",")","-","+","=","Backspace"), | |
("1","2","3","4","5","6","7","8","9","0","[","]","Delete"), | |
("Q","W","E","R","T","Y","U","I","O","P","@","*"), | |
("|","A","S","D","F","G","H","J","K","L",";",":"), | |
("?","Z","X","C","V","B","N","M",",",".","'","Enter") | |
("Shift","**","**","**","**","Space","**","<",">","_","/")) | |
$UpperAlpha = $UpperAlphaKeyMap | % {$_} | |
$LowerAlphaKeyMap = @(("**","**","**","**","**","**","{","}","**","**","Backspace"), | |
("**","**","**","**","**","**","**","^","¥","~","**","**","Delete"), | |
("q","w","e","r","t","y","u","i","o","p","``","**"), | |
("**","a","s","d","f","g","h","j","k","l","**","**"), | |
("**","z","x","c","v","b","n","m","**","**","**","Enter") | |
("Shift","**","**","**","**","Space","**","<",">","_","\")) | |
$LowerAlpha = $LowerAlphaKeyMap | % {$_} | |
$Keyboards = @{$UpperAlpha=$UpperAlphaKeyMap,$LowerAlpha=$LowerAlphaKeyMap} | |
#constants | |
$citra_margin = 41 | |
$top_offset = 57 + 240 | |
$row_shift = 19 | |
$hKeySpace = 25 | |
$vKeySpace = 26 | |
$HalfKey = 13 | |
# disable Caps Lock if not on for user | |
if (-not [Console]::CapsLock){ | |
do_hit -X (50) ` | |
-Y ($top_offset + 176) | |
$CurrentKeyboard = $LowerAlpha | |
$CurrentKeymap = $LowerAlphaKeyMap | |
} else { $CurrentKeyboard = $UpperAlpha; $CurrentKeymap = $LowerAlphaKeyMap } | |
# main loop | |
do { | |
$key = [Console]::ReadKey() | |
[String]$keyChar | |
# keys without characters (not working) | |
if (@("Enter","Backspace","Space","Delete") -contains $key.Key){ | |
$keyChar = $key.Key | |
} else { | |
$keyChar = $key.KeyChar.ToString() | |
} | |
# entering a key that isn't available on the current keyboard (not working) | |
if (-not ($CurrentKeyboard -contains $keyChar)) { | |
foreach($keyboard in $Keyboards.Keys) { | |
if ($keyboard -contains $keyChar) { | |
switch_keyboard -Keyboard $keyboard | |
} | |
} | |
} | |
# search and hit the key | |
for($row = 1; $row -le $CurrentKeymap.Count; $row++){ | |
$col = $CurrentKeymap[$row - 1].IndexOf($keyChar) | |
if ($col -ge 0) { | |
do_hit -X ($citra_margin + ($row_shift * ($row % 2)) + $hKeySpace * ($col) + $HalfKey) ` | |
-Y ($top_offset + $vKeySpace * ($row -1) + $HalfKey) | |
break | |
} | |
} | |
} until($key.Key -eq "Escape") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment