Graham3D
Graham3D

Reputation: 1

Printscreen keystroke script . ^{PRTSC} not doing anything

I am trying to create a batch script which puts a screenshot on the clipboard for me to save in another application.

I am using the "^{PRTSC}" and have copied code from another posting here (I would ask/comment there but the listing is closed and I do not have enough points to post there.)

When I run the following line line I get no errors:

powershell -c "$wshell = New-Object -ComObject wscript.shell; $wshell.SendKeys("%{PRTSC}")

But when I create a new document in Photoshop and select Paste ctrl+v, nothing is pasted from the clipboard.

Clearly the code is not putting a screenshot in the clipboard.

( I do not want to use Navcmd )

Upvotes: 0

Views: 1513

Answers (3)

MoonLord
MoonLord

Reputation: 61

/*
@echo off & cls

timeout /T 3 /nobreak >nul

for /F %%i in ('dir /B /S ^"%WinDir%\Microsoft.NET\Framework\vbc.exe^"') do set "vbc=%%i" && echo %%i
if /i "%vbc%"=="" cls & echo This script needs the Microsoft .NET Framework & pause & Exit

set /A randNum=%random%
findstr "'VB" "%~f0" | findstr /V "This line should be skipped"> "%tmp%\%~n0_%randNum%.vb"
%vbc% /nologo /out:"%tmp%\%~n0_%randNum%.exe" "%tmp%\%~n0_%randNum%.vb"
"%tmp%\%~n0_%randNum%.exe"

del "%tmp%\%~n0_%randNum%.vb" >nul 2>&1
del "%tmp%\%~n0_%randNum%.exe" >nul 2>&1

exit
*/

Imports System.Windows.Forms 'VB
Imports System.Drawing 'VB
Module ModulePrintscreen 'VB
    Sub Main() 'VB
        Dim MaDate As String = Format(Now, "yyyyMMddHHmmss") 'VB
        Dim screenBounds As Rectangle = Screen.PrimaryScreen.Bounds 'VB
        Using bitmap As New Bitmap(screenBounds.Width, screenBounds.Height) 'VB
            Using g As Graphics = Graphics.FromImage(bitmap) 'VB
                g.CopyFromScreen(Point.Empty, Point.Empty, screenBounds.Size) 'VB
                bitmap.Save(MaDate & ".png", System.Drawing.Imaging.ImageFormat.Png) 'VB
            End Using 'VB
        End Using 'VB
    End Sub 'VB
End Module 'VB

Upvotes: 0

Anon Coward
Anon Coward

Reputation: 10832

As far as I know, windows keyboard supports two shortcuts to capture the screen:

  • PrtScn Captures the entire desktop
  • Alt+PrtScn Captures the active window.

Per the title and your description, ^{PRTSC} should act as if it's pressing Ctrl+PrtScn, which will perform the same action as PrtScn.

Assuming you just want to capture the active window, running the following command from a batch file will work:

powershell -c "Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.SendKeys]::SendWait('%%{PRTSC}')"

There are a few differences:

  • Using the SendWait implementation from .NET. The COM SendKeys implementation has some issues, notably around how it reacts to console windows and special keys.
  • I'm using single quotes to quote the string inside the command, otherwise the double quote will end the quoted string being sent to PowerShell.
  • Using % to simulate the Alt key, and more, since it's in a batch file, escaping the % with %%.

Upvotes: 0

Hackoo
Hackoo

Reputation: 18857

Here is an old batch script that use a module in VB.net and save a screenshot as jpeg image with system date:


/*
@echo off & cls & color FC
Mode 30,3
::Autor Delmar Grande
::http://bbat.forumeiro.com/t248-bat-exe-printscreen-usando-vb-net
::Data  Qui 11 Jul 2013
:: Modified by Hackoo on 09/09/2016 to save image with system date
Title PrintScreen by Delmar Grande and modified by Hackoo
Rem Just adding a little timeout to organise our screenshot
Timeout /T 4 /Nobreak>nul
findstr "'%skip%VB" "%~f0">"%tmp%\%~n0.vb"
for /F %%i in ('dir /B /S ^"%WinDir%\Microsoft.NET\Framework\vbc.exe^"') do set "vbc=%%i"
if /i "%vbc%"=="" cls & color 1c & echo This script needs the framework & pause & Exit
cls
%vbc% /nologo /out:"%tmp%\%~n0.exe" "%tmp%\%~n0.vb"
"%tmp%\%~n0.exe"
del "%tmp%\%~n0.vb" >NUL 2>&1
del "%tmp%\%~n0.exe" >NUL 2>&1
exit
*/
Imports System.Windows.Forms 'VB
Module ModulePrintscreen 'VB
  Sub Main() 'VB
  Dim MaDate As String 'VB
  SendKeys.SendWait("{%}({PRTSC})") 'VB
  If My.Computer.Clipboard.ContainsImage() Then 'VB
  MaDate = Format(Now,"dd-MM-yyyy_hh-mm-ss") 'VB
  My.Computer.Clipboard.GetImage.Save(MaDate & ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg) 'VB
  End If 'VB
  End Sub 'VB
End Module 'VB

Upvotes: 1

Related Questions