skip to Main Content

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 )

2

Answers


  1. 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.NETFrameworkvbc.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
    
    Login or Signup to reply.
  2. 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 %%.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search