From d3942256e936ac18e4406da5c9cd3fad67746894 Mon Sep 17 00:00:00 2001 From: Kiritow <1362050620@qq.com> Date: Mon, 31 Dec 2018 02:20:51 +0800 Subject: [PATCH] Add PrtSc --- programs.info | 11 +++++++++ prtsc.lua | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 prtsc.lua diff --git a/programs.info b/programs.info index d8ea833..ff0588e 100644 --- a/programs.info +++ b/programs.info @@ -126,5 +126,16 @@ requires={ "libevent","checkarg","util","simple_data_structure" } + }, + ["prtsc"]={ + title="Print Screen", + info="Save screen to file when PrtSc key is pressed.", + author="Kiritow", + files={ + "prtsc.lua" + }, + requires={ + "libevent" + } } } \ No newline at end of file diff --git a/prtsc.lua b/prtsc.lua new file mode 100644 index 0000000..d683728 --- /dev/null +++ b/prtsc.lua @@ -0,0 +1,66 @@ +-- Print Screen : Save screen to file +-- Author: Kiritow + +require("libevent") +local shell=require("shell") +local filesystem=require("filesystem") +local component=require('component') + +local args,options=shell.parse(...) + +if(#args<1) then + print("Usage: prtsc ") +elseif(args[1]=="start") then + if(filesystem.exists('/tmp/.prtsc.evid')) then + print("PrtSc service already started.") + else + local f=io.open("/tmp/.prtsc.evid","w") + if(not f) then + print("Failed to open record file.") + else + local evid=AddEventListener("key_down",function(e) + if(e.code==183) then + local gpu=component.list('gpu')() + if(gpu) then + gpu=component.proxy(gpu) + local name=os.tmpname() + local p=io.open(name,"w") + if(p) then + local w,h=gpu.getResolution() + for i=1,h do + for j=1,w do + p:write((gpu.get(j,i))) + end + p:write('\n') + end + print("[PrtSc] Screen saved to " .. name) + p:close() + else + print("[PrtSc] Unable to open file: " .. name) + end + end + end + end) + f:write(evid) + f:close() + print("PrtSc service started.") + end + end +elseif(args[1]=="stop") then + if(not filesystem.exists('/tmp/.prtsc.evid')) then + print("PrtSc service not started.") + else + local f=io.open("/tmp/.prtsc.evid","r") + if(not f) then + print("Failed to open record file.") + else + local evid=f:read("n") + f:close() + RemoveEventListener(evid) + filesystem.remove("/tmp/.prtsc.evid") + print("PrtSc service stopped.") + end + end +else + print("Unknown command: " .. args[1]) +end \ No newline at end of file