From 824c33100c383cc9a1405260bc86f193b1b26e68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Sat, 30 Jan 2021 23:37:46 +0100 Subject: Add hotkey to print screen to file in clipboard --- lib/Misc.ahk | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ scripts.ahk | 7 +++++++ 2 files changed, 56 insertions(+) create mode 100644 lib/Misc.ahk diff --git a/lib/Misc.ahk b/lib/Misc.ahk new file mode 100644 index 0000000..3ebaca7 --- /dev/null +++ b/lib/Misc.ahk @@ -0,0 +1,49 @@ +; by "just me" at AutoHotkey forums +ClipboardSetFiles(FilesToSet, DropEffect := "Copy") { + ; FilesToSet - list of fully qualified file pathes separated by "`n" or "`r`n" + ; DropEffect - preferred drop effect, either "Copy", "Move" or "" (empty string) + Static TCS := A_IsUnicode ? 2 : 1 ; size of a TCHAR + Static PreferredDropEffect := DllCall("RegisterClipboardFormat", "Str", "Preferred DropEffect") + Static DropEffects := {1: 1, 2: 2, Copy: 1, Move: 2} + ; ------------------------------------------------------------------------------------------------------------------- + ; Count files and total string length + TotalLength := 0 + FileArray := [] + Loop, Parse, FilesToSet, `n, `r + { + If (Length := StrLen(A_LoopField)) + FileArray.Push({Path: A_LoopField, Len: Length + 1}) + TotalLength += Length + } + FileCount := FileArray.Length() + If !(FileCount && TotalLength) + Return False + ; ------------------------------------------------------------------------------------------------------------------- + ; Add files to the clipboard + If DllCall("OpenClipboard", "Ptr", A_ScriptHwnd) && DllCall("EmptyClipboard") { + ; HDROP format --------------------------------------------------------------------------------------------------- + ; 0x42 = GMEM_MOVEABLE (0x02) | GMEM_ZEROINIT (0x40) + hDrop := DllCall("GlobalAlloc", "UInt", 0x42, "UInt", 20 + (TotalLength + FileCount + 1) * TCS, "UPtr") + pDrop := DllCall("GlobalLock", "Ptr" , hDrop) + Offset := 20 + NumPut(Offset, pDrop + 0, "UInt") ; DROPFILES.pFiles = offset of file list + NumPut(!!A_IsUnicode, pDrop + 16, "UInt") ; DROPFILES.fWide = 0 --> ANSI, fWide = 1 --> Unicode + For Each, File In FileArray + Offset += StrPut(File.Path, pDrop + Offset, File.Len) * TCS + DllCall("GlobalUnlock", "Ptr", hDrop) + DllCall("SetClipboardData","UInt", 0x0F, "UPtr", hDrop) ; 0x0F = CF_HDROP + ; Preferred DropEffect format ------------------------------------------------------------------------------------ + If (DropEffect := DropEffects[DropEffect]) { + ; Write Preferred DropEffect structure to clipboard to switch between copy/cut operations + ; 0x42 = GMEM_MOVEABLE (0x02) | GMEM_ZEROINIT (0x40) + hMem := DllCall("GlobalAlloc", "UInt", 0x42, "UInt", 4, "UPtr") + pMem := DllCall("GlobalLock", "Ptr", hMem) + NumPut(DropEffect, pMem + 0, "UChar") + DllCall("GlobalUnlock", "Ptr", hMem) + DllCall("SetClipboardData", "UInt", PreferredDropEffect, "Ptr", hMem) + } + DllCall("CloseClipboard") + Return True + } + Return False +} diff --git a/scripts.ahk b/scripts.ahk index 301a46a..e8a838b 100644 --- a/scripts.ahk +++ b/scripts.ahk @@ -1,4 +1,5 @@ #Include ../lib/Acc.ahk +#Include ../lib/Misc.ahk ; Directives ------------------------------------------------------------------ @@ -48,6 +49,12 @@ return #ScrollLock::Media_Play_Pause #Pause::Media_Next +*~^PrintScreen:: +Run, "C:\Program Files\ImageMagick\magick.exe" clipboard: scr.jpg, % env("TEMP"), Hide +Sleep, 200 +ClipboardSetFiles(env("TEMP") "\scr.jpg") +return + #e::Run, % "explorer /n, ::{20D04FE0-3AEA-1069-A2D8-08002B30309D}" ; open my computer ; Enable/disable proxy server -- cgit v1.2.3