[kupfer] paste: Implement TypeText and SendKeys



commit 7d1471d67f2fbc3a0bb6d68aead001925c048b3e
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date:   Sat Mar 19 02:20:48 2011 +0100

    paste: Implement TypeText and SendKeys

 kupfer/plugin/paste.py |   63 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 63 insertions(+), 0 deletions(-)
---
diff --git a/kupfer/plugin/paste.py b/kupfer/plugin/paste.py
index 881fef5..62b572d 100644
--- a/kupfer/plugin/paste.py
+++ b/kupfer/plugin/paste.py
@@ -2,14 +2,19 @@
 __kupfer_name__ = _("Paste")
 __kupfer_actions__ = (
 	"CopyAndPaste",
+	"SendKeys",
+	"TypeText",
 	)
 __description__ = _("Copy to clipboard and send Ctrl+V to foreground window")
 __version__ = ""
 __author__ = ""
 
+import string
+
 import gtk
 
 from kupfer.objects import Leaf, Action, Source, OperationError
+from kupfer.objects import TextLeaf
 from kupfer import pretty
 from kupfer import utils
 from kupfer import interface
@@ -38,3 +43,61 @@ class CopyAndPaste (Action):
 		return __description__
 	def get_icon_name(self):
 		return "edit-paste"
+
+class SendKeys (Action):
+	def __init__(self):
+		Action.__init__(self, _("Send Keys"))
+	def activate(self, leaf):
+		text = leaf.object
+		keys, orig_mods = gtk.accelerator_parse(text)
+		m = {
+			gtk.gdk.SHIFT_MASK: "Shift_L",
+			gtk.gdk.CONTROL_MASK: "Control_L",
+			gtk.gdk.SUPER_MASK: "Super_L",
+			gtk.gdk.MOD1_MASK: "Alt_L",
+		}
+		mods_start = []
+		mods_end = []
+		mods = orig_mods
+		for mod in m:
+			if mod & mods:
+				mods_start.append('keydown %s' % (m[mod], ))
+				mods_end.append('keyup %s' % (m[mod], ))
+				mods &= ~mod
+		if mods != 0 or not (31 < keys < 127):
+			raise OperationError(_("Keys not yet implemented: %s") %
+					gtk.accelerator_get_label(keys, orig_mods))
+		key_arg = 'key %s' % (chr(keys), )
+		print mods_start, repr(key_arg), mods_end
+
+		xte_paste_argv = ['xte', 'usleep 300000'] + \
+				mods_start + [key_arg] + list(reversed(mods_end))
+		if not utils.spawn_async(xte_paste_argv):
+			raise OperationError(_("Command '%s' not found!") % ("xte", ))
+	def item_types(self):
+		yield TextLeaf
+	def valid_for_item(self, leaf):
+		text = leaf.object
+		keys, mods = gtk.accelerator_parse(text)
+		return keys
+	def get_description(self):
+		return _("Send keys to foreground window")
+
+class TypeText (Action):
+	rank_adjust = -2 
+	def __init__(self):
+		Action.__init__(self, _("Type Text"))
+	def activate(self, leaf):
+		text = leaf.object
+		xte_paste_argv = ['xte', 'usleep 300000']
+		# replace all newlines with 'key Return'
+		for line in text.splitlines(True):
+			xte_paste_argv.append("str " + line.rstrip("\r\n"))
+			if line.endswith("\n"):
+				xte_paste_argv.append("key Return")
+		if not utils.spawn_async(xte_paste_argv):
+			raise OperationError(_("Command '%s' not found!") % ("xte", ))
+	def item_types(self):
+		yield TextLeaf
+	def get_description(self):
+		return _("Type the text to foreground window")



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]