Actualmente estoy haciendo un script muy básico que toma una captura de pantalla de la computadora cada 10 minutos. Sin embargo, ahora me han pedido que agregue también la opción de hacer clic en una tecla de acceso rápido para activarla manualmente.
Encontré esto, que esencialmente me ayuda en parte:
Add-Type -TypeDefinition ' using System; using System.IO; using System.Diagnostics; using System.Runtime.InteropServices; using System.Windows.Forms; namespace KeyLogger { public static class Program { private const int WH_KEYBOARD_LL = 13; private const int WM_KEYDOWN = 0x0100; private static HookProc hookProc = HookCallback; private static IntPtr hookId = IntPtr.Zero; private static int keyCode = 0; [DllImport("user32.dll")] private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll")] private static extern bool UnhookWindowsHookEx(IntPtr hhk); [DllImport("user32.dll")] private static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hMod, uint dwThreadId); [DllImport("kernel32.dll")] private static extern IntPtr GetModuleHandle(string lpModuleName); public static int WaitForKey() { hookId = SetHook(hookProc); Application.Run(); UnhookWindowsHookEx(hookId); return keyCode; } private static IntPtr SetHook(HookProc hookProc) { IntPtr moduleHandle = GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName); return SetWindowsHookEx(WH_KEYBOARD_LL, hookProc, moduleHandle, 0); } private delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam); private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN) { keyCode = Marshal.ReadInt32(lParam); Application.Exit(); } return CallNextHookEx(hookId, nCode, wParam, lParam); } } } ' -ReferencedAssemblies System.Windows.Forms DO { $IsTrue = $true while ($IsTrue -eq $true) { $key = [System.Windows.Forms.Keys][KeyLogger.Program]::WaitForKey() if ($key -eq "F2") { #Do something $IsTrue = $false } } } Until ($SomeValue -eq $true)El problema es, ¿cómo hago para que esto funcione, de modo que si no se presiona la tecla en 10 minutos, la secuencia de comandos ejecutará el resto de la secuencia de comandos? Porque tal como está ahora, espera hasta que se presiona una tecla, antes de continuar.
¡Gracias por adelantado!
vas a necesitar lo siguiente en el ciclo while. El contador estallará a los 10 min.
DO { #$i makes a timer / counter $i = 0 $IsTrue = $true while ($IsTrue -eq $true) { $key = [System.Windows.Forms.Keys][KeyLogger.Program]::WaitForKey() #at 600 seconds or 10 mins the $i counter will break this loop. if ($key -eq "F2" -or $i -eq 600) { #Do something $IsTrue = $false } #Sleep waits a second Start-Sleep -Seconds 1 #Counter $i adds 1 $I++ } } Until ($SomeValue -eq $true)Lo siguiente funciona para el tiempo y F2
#$i makes a timer / counter $i = 0 $IsTrue = $true while ($IsTrue -eq $true) { If ([Console]::KeyAvailable -or $i -eq 30) { [Console]::ReadKey('F2') | Out-Null $IsTrue = $false } Start-Sleep -Seconds 1 #Counter $i adds 1 $I++ }