Skip to content

Instantly share code, notes, and snippets.

@tigerhawkvok
Last active August 29, 2015 14:01
Show Gist options
  • Save tigerhawkvok/9c662901d25a3d7c5424 to your computer and use it in GitHub Desktop.
Save tigerhawkvok/9c662901d25a3d7c5424 to your computer and use it in GitHub Desktop.
Infinite screenshots
## Taken from http://code.activestate.com/recipes/134892/
## Brought to Github for better versioning / editing / updating
## Returns a string (as input() or raw_input() )
## Use:
# import getch
# c = getch.getch()
#
# https://gist.github.com/tigerhawkvok/9542594
class _Getch:
"""Gets a single character from standard input. Does not echo to the
screen."""
def __init__(self):
try:
self.impl = _GetchWindows()
except ImportError:
try:
self.impl = _GetchUnix()
except ImportError:
self.impl = _GetchMacCarbon()
def __call__(self):
# Return as a string
return self.impl().decode('utf-8')
class _GetchUnix:
def __init__(self):
import tty, sys
def __call__(self):
import sys, tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
class _GetchWindows:
def __init__(self):
import msvcrt
def __call__(self):
import msvcrt
return msvcrt.getch()
class _GetchMacCarbon:
"""
A function which returns the current ASCII key that is down;
if no ASCII key is down, the null string is returned. The
page http://www.mactech.com/macintosh-c/chap02-1.html was
very helpful in figuring out how to do this.
"""
def __init__(self):
import Carbon
Carbon.Evt #see if it has this (in Unix, it doesn't)
def __call__(self):
import Carbon
if Carbon.Evt.EventAvail(0x0008)[0]==0: # 0x0008 is the keyDownMask
return ''
else:
#
# The event contains the following info:
# (what,msg,when,where,mod)=Carbon.Evt.GetNextEvent(0x0008)[1]
#
# The message (msg) contains the ASCII char which is
# extracted with the 0x000000FF charCodeMask; this
# number is converted to an ASCII character with chr() and
# returned
#
(what,msg,when,where,mod)=Carbon.Evt.GetNextEvent(0x0008)[1]
return chr(msg & 0x000000FF)
getch = _Getch()
# Uses http://wxpython.org/download.php
# From https://stackoverflow.com/a/10089645/1877527
fileDirectory = "" # Your directory here
def doExit():
import os,sys
print("\n")
os._exit(0)
sys.exit(0)
try:
import wx
import time
except ImportError:
import yn
if yn.yn("WX isn't installed. Do you want to install WX?"):
print("Launching browser. Re run this script once you've installed WX")
import webbrowser
webbrowser.open("http://wxpython.org/download.php")
doExit()
wx.App() # Need to create an App instance before doing anything
try:
screen = wx.ScreenDC()
size = screen.GetSize()
bmp = wx.EmptyBitmap(size[0], size[1])
mem = wx.MemoryDC(bmp)
mem.Blit(0, 0, size[0], size[1], screen, 0, 0)
del mem # Release bitmap
time = '%Y_%m_%d_%H_%M_%S'
fileName = fileDirectory+"Screenshot_"+time.strftime(time)
bmp.SaveFile(fileName, wx.BITMAP_TYPE_PNG)
except KeyboardInterrupt:
doExit()
python infinite_screenshots.py
pause
# Format the yes/no input.
# To use:
# import yn
# var = yn.yn("string")
# Automatically appends the [Y/N] prompt to the end.
#
# https://gist.github.com/tigerhawkvok/9542594
class _yn:
"""Give a prompt for [Y/N] and returns the appropriate boolean"""
def __init__(self):
try:
import getch
except ImportError:
print("This package requires getch to function. Be sure it's in the load path!")
def __call__(self,*args):
return self.yn_input(*args)
def yn_input(self,*args):
string = None
first_piece = True
for piece in args:
if not first_piece:
string+=" "
else:
first_piece = False
string = ""
string+=str(piece)
if string is not None:
try:
print(string+" [Y/N]")
except TypeError:
pass
import getch
a = getch.getch()
a = a.lower()
b = a.encode('utf-8')
while b is not "y".encode('utf-8') and b is not "n".encode('utf-8'):
print("Please enter 'Y' or 'N'.")
a = getch.getch()
a = a.lower()
b = a.encode('utf-8')
return b is "y".encode('utf-8')
yn = _yn()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment