Skip to content

Instantly share code, notes, and snippets.

@itshaadi
Last active October 7, 2016 19:35
Show Gist options
  • Save itshaadi/c375705e4d20b9bc63f1a96cc64f7593 to your computer and use it in GitHub Desktop.
Save itshaadi/c375705e4d20b9bc63f1a96cc64f7593 to your computer and use it in GitHub Desktop.
increase clipboard memory with python
# When Ctrl+C is pressed, this script will add the current value of
# clipboard into a stack (deque) and when Shift+Alt+V is pressed
# it will pop the leftmost value and paste it on the screen
# until stack is empty
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Gdk', '3.0')
from keybinder.keybinder_gtk import KeybinderGtk #https://github.com/LiuLang/python3-keybinder
from pykeyboard import PyKeyboard #https://github.com/PyUserInput/PyUserInput
from gi.repository import GLib
from collections import deque
import signal
import xerox #https://github.com/kennethreitz/xerox
import time
stack = deque(maxlen=5)
keyboard = PyKeyboard()
# simulate Ctrl+V so when ever Shift+Alt+V is pressd
# it's actualy pressing Ctrl+V
def simulatePaste():
keyboard.press_key(keyboard.control_key)
keyboard.tap_key('v')
keyboard.release_key(keyboard.control_key)
def copy():
content = xerox.paste()
stack.append(content)
# pop the leftmost value and paste it into clipboard
# until stack is empty
def paste():
size = len(stack)
if size != 0 :
content = stack.popleft()
xerox.copy(content)
#sleep for 1 second so you release the buttons and
#then simulate the paste hotkey
time.sleep(1)
simulatePaste()
print(content)
def main():
# use the default gtk main loop in gtk based applications.
loop = GLib.MainLoop.new(None, False)
copy_key = '<Ctrl>C'
copy_cb = lambda *args: copy()
paste_key = '<Shift><lAlt>V'
paste_cb = lambda *args: paste()
keybinder = KeybinderGtk()
keybinder.register(copy_key, copy_cb)
keybinder.register(paste_key, paste_cb)
keybinder.start()
loop.run()
if __name__ == '__main__':
signal.signal(signal.SIGINT, signal.SIG_DFL) #enable ctrl+c to break the program
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment